我會一開始就不會合並像date()
這樣的每行功能。你最好測試實際領域NULL如下面的例子:
create table a (podate varchar(8));
insert into a (podate) values ('20090101');
insert into a (podate) values ('20090102');
insert into a (podate) values ('20090103');
insert into a (podate) values (null);
commit;
select
case when podate is null
then date('2000-01-01')
else date(substr(podate,1,4) || '-' ||
substr(podate,5,2) || '-' ||
substr(podate,7,2))
end as mydate
from a;
drop table a;
commit;
,輸出:
---------
MYDATE
----------
2009-01-01
2009-01-02
2009-01-03
2000-01-01
DSNE610I NUMBER OF ROWS DISPLAYED IS 4
在你的情況下,代碼會看起來像:
select case when podate is null
then date('12/31/99')
else date (substr (podate,1,2) || char ('/') ||
substr (podate,3,2) || char ('/') ||
substr (podate,5,2)
end
我沒有測試過你的具體情況,因爲我的DB/2沒有設置美國日期格式。這是別的東西,以及注意。在不使用美國日期格式的平臺上,這可能不太適用。
理想情況下,日期應該作爲日期存儲在數據庫中,而不是字符串。換句話說,轉換成本應該是一旦當數據進入一個表格,而不是百萬倍你要提取它。然後你的代碼變得更簡單,更快:
select case when podate is null
then date('12/31/99')
else podate
end
,或者更好,如果你永遠不會有一個哨兵日期作爲一個真正的日期,不要存放NULL
s的一切。取而代之的是,將它們轉換爲date('12/31/99')
,因爲他們去到表中,並使用:
select podate
它沒有什麼比這更簡單或更快(假設你認爲合適的非NULL定點)。
PODATE列的類型是什麼? – 2009-08-05 11:10:22
podate列是創建數字字段的傳統列。 – 2009-08-05 13:47:27