像下面這樣的東西應該工作:
select case when to_char(COL2, 'yyyy') < 2001
then 'OLD ' || nvl(COL1, '')
else COL1 end case
from TAB1
例如:
insert into TAB1 values ('testpost2001', trunc(sysdate));
insert into TAB1 values ('testpre2001', '01/Jul/2000');
insert into TAB1 values (null, trunc(sysdate));
insert into TAB1 values (null, '01/Jul/2000');
select COL1, COL2,
case when to_char(COL2, 'yyyy') < 2001
then 'OLD ' || nvl(COL1, '')
else COL1 end case
from TAB1;
返回:
COL1 COL2 CASE
testpost2001 15/12/2011 testpost2001
testpre2001 1/07/2000 OLD testpre2001
15/12/2011
1/07/2000 OLD
你可能會考慮使用`case when when_char(col2,'yyyy')<'2001'then'OLD'end || col1結束「,以便只有一次對」col「的明確引用。 – 2011-12-15 05:11:39
不確定是否需要to_char COL2,將日期與日期進行比較會更有效。 `col2
2011-12-15 09:04:35