2015-11-02 33 views
-1
update template 
set activedate=(SELECT unique activedate 
       from template 
       where status = 'Draft' 
         and activedate like '%01-JAN-99%') 
where status = 'Draft' 
     and activedate not like '%01-JAN-99%; 

当我使用上面的语句时,日期更新为由JDBC前端更新的01-JAN-2099。 但是,当我使用下面的查询而不是在oracle sql中更新日期列的行为不正常

update template 
set activedate = '01JAN-99' 
where status = 'Draft' 
     and activedate not like '%01-JAN-99%; 

可能有人解释为什么会出现这种情况?

+1

什么activedate列的数据类型? – Boneist

+1

你想做什么? – Moudiz

+1

'LIKE'用于字符串值,**不**用于日期 –

回答

1

当你与日期这样操作,你需要将它们转换为varchar

to_char(sysdate, 'DD-MON-YY') 

和您的查询应该是水木清华这样的:

update template 
set activedate = to_date('01-JAN-99', 'DD-MON-YY') 
where status = 'Draft' 
     and to_char(activedate, 'DD-MON-YY') not like '%01-JAN-99%; 

或更好(因为有人建议使用号码) :

update template 
set activedate = to_date('01-01-1999', 'DD-MM-YYYY') 
where status = 'Draft' 
     and activedate != to_date('01-01-1999', 'DD-MM-YYYY'); 

查看更多资讯

to_char(datetime)

to_date

+3

请注意'to_date('01 -JAN-99','DD-MON-YY')'可能会在非英语计算机上中断(例如,如果客户端计算机使用法语区域设置,它将失败)。最好使用_numbers_几个月,而不是名字。同样''99'也是一个潜在的问题(是1999年还是2099年?)你应该**总是**使用4位年份:'to_char('01 -01-2099','dd-mm-yyyy “)' –