执行以下语句
update b_c12_b1
 set B2=to_char(to_date(B2,'yyyy-mm-dd'),'yyyy/mm/dd') 
 where to_char(b2) like '%-%-%';提示ORA-01830:日期格式图片在转换整个输入字符串之前结束。B2为 varchar2(20)
B2 在库中存的格式例如以下:
1998-3-8 0:00:00
1998-12-31 
1998-12-1 
1998年12月31日
1998年



SQL的目的就是把里面的类似1998-12-1的都转换为10位的1998-12-01(月,日是单数的前面补0),格式乱的不管。

解决方案 »

  1.   

    有的数据不是'yyyy-mm-dd'吧,还有'yyyy-mm-dd hh:MM:ss'的数据
      

  2.   

    select to_date('1998-3-8 0:00:00','yyyy-MM-dd') from dual
    这样就是错的而这样select to_date('1998-3-8','yyyy-MM-dd') from dual可以通过
      

  3.   

    update tt1 t1 set createtime1 = (select to_char(to_date(createtime1,'yyyy-mm-dd'),'yyyy-mm-dd')   from tt1 t2 where t2.createtime1 = t1.createtime1)
      

  4.   

    update tt1 t1 set createtime1 = (select to_char(to_date(createtime1,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd')   
    from tt1 t2 where t2.createtime1 = t1.createtime1)
      

  5.   


    update b_c12_b1
     set B2=to_char(to_date(regexp_substr(B2,'\d{4}\-\d{1,2}\-\d{1,2}'),'yyyy-mm-dd'),'yyyy/mm/dd') 
     where regexp_like(B2,'\d{4}\-\d{1,2}\-\d{1,2}');
      

  6.   

    正则王道啊
    哪位高手能贴一些常用的正则表达式吗?ORACLE10或11版本的
      

  7.   

    to_date函数要求比较严格
    源和目标的格式要一致
    '2009-9-01'          --> 'yyyy-MM-dd'  or  'yyyy-MM-dd hh24:mi:ss'
    '2009-9-01 00:00:00' --> 'yyyy-MM-dd'  产生ORA-01830错误源非日前格式to_Date时,产生Error