oracle case when 满足多个条件后可以对字段进行格式转换吗??

想要更新table1的时间字段time1 date,从table2中查出时间字段time2插入time1,现有问题,time2的部分数据为 "08月16日 10时"这种格式的,部分为标准格式"2015-08-16",想把所有time2格式变为标准 "2015-08-16",直接使用to_date和to_char不行,决定拼接字符串.所以需要分别判断原来是什么格式的,然后进行处理,用的是case when then,先把time2换为标准格式,

select
(case
when length(time2)=10&&substr(time2,0,1)!='2'
then to_date((to_char(sysdate,'yyyy')||'-'||substr(time2,0,2)||'-'||substr(time2,3,2)),'yyyy-MM-dd')
else 'not time'
end)
from table2 d


现在报错,ora-00905:缺失关键字,各位大侠谁帮忙解决一下???小女子谢过啦
...全文
439 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LLH1103 2019-08-13
  • 打赏
  • 举报
回复
ZhanYunQI 2015-08-16
  • 打赏
  • 举报
回复
create table table1(a date); create table table2(b varchar2(100)); insert into table2(b) values ('08月16日 10时'); insert into table2(b) values ('2015-08-16'); COMMIT; INSERT INTO table1(a) SELECT to_date(CASE SUBSTR(b,3,1) WHEN '月' THEN '2015-'||SUBSTR(b,1,2)||'-'||SUBSTR(b,4,2) ELSE b END,'yyyy-mm-dd') AS col1 FROM table2; COMMIT;
ksdy138141314 2015-08-12
  • 打赏
  • 举报
回复

select 
(case 
when length(sysdate)=10 and substr(sysdate,0,1)!='2' 
then to_date((to_char(sysdate,'yyyy')||'-'||substr(sysdate,0,2)||'-'||substr(sysdate,3,2)),'yyyy-MM-dd') 
else sysdate
end)
from dual d
应该这样才行,then和else的结果类型应该要一致。不能一个是date,一个是字符。
mayanzs 2015-08-12
  • 打赏
  • 举报
回复
条件只需一个即可,同时else后应是 to_date(time2,'yyyy-mm-dd')

select 
(case 
when substr(time2,0,1)!='2' 
then to_date((to_char(sysdate,'yyyy')||'-'||substr(time2,0,2)||'-'||substr(time2,3,2)),'yyyy-MM-dd') 
else to_date(time2,'yyyy-mm-dd')
end)
from table2 d
SirGch 2015-08-12
  • 打赏
  • 举报
回复
&&怎么在shell中 这么熟悉。。
shiyiwan 2015-08-11
  • 打赏
  • 举报
回复
把 && 替换成 AND

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧