| how to handle null in date time [message #574593] |
Sun, 13 January 2013 23:55  |
 |
anniepeteroracle
Messages: 36 Registered: March 2012 Location: hyderabad
|
Member |
|
|
create table top22 (timed timestamp);
insert into top22(timed)
values (current timestamp),(null);
select timed from top22;
2013-01-14 10:50:22.17
NULL
select 'no is '||coalesce("num",'') from "bifrost"."top22"
getting error in DB2
if i remove this coalesce function
select 'no is '||timed from "bifrost"."top22"
no is 2013-01-14 10:50:22.17
NULL
but i need the second result is no is null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: how to handle null in date time [message #574607 is a reply to message #574605] |
Mon, 14 January 2013 01:59  |
 |
Michel Cadot
Messages: 54195 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:but you must NOT use CAST but TO_CHAR.
I should add: if you want a specific format otherwise it is not necessary:
SQL> drop table t;
Table dropped.
SQL> create table t (v date);
Table created.
SQL> insert into t values (null);
1 row created.
SQL> insert into t values (to_date('13/11/1956','DD/MM/YYYY'));
1 row created.
SQL> commit;
Commit complete.
SQL> select 'Date is: '||nvl(v, sysdate) r from t;
R
----------------------------
Date is: 14/01/2013 09:18:48
Date is: 13/11/1956 00:00:00
2 rows selected.
SQL> select 'Date is: '||to_char(nvl(v, sysdate),'DD-MON-YYYY') r from t;
R
--------------------
Date is: 14-JAN-2013
Date is: 13-NOV-1956
SQL> select 'Date is: '||nvl(to_char(v,'DD-MON-YYYY'),'Unknown') r from t;
R
--------------------
Date is: Unknown
Date is: 13-NOV-1956
You can replace NVL by COALESCE in the same way.
Regards
Michel
[Updated on: Mon, 14 January 2013 02:19] Report message to a moderator
|
|
|
|