如题;
有一个字段存储的是字符串 2015年04月 想转成 2015-04-01.
谢谢

解决方案 »

  1.   

    select to_date(substr('2015年04月', 1, 4) || substr('2015年04月', -3, 2),
                   'yyyymm')
      from dual;这种情况下,数据库仍然保留date数据类型,日期输入年月日。显示的时候用to char函数,格式是只显示年月即可。
      

  2.   


    select replace(replace('2015年04月', '年', '-'),'月','-')+'01'
      from dual;
      

  3.   


    -- 凑个人数
    select translate('2015年04月','#年月','#--') || '01' from dual 
      

  4.   

    select replace(replace('2015年04月', '年', '-'),'月','-')||'01'
      from dual;
      

  5.   

    根据楼主描述的“转换”有两种情况:
    ①当你查询时,需要将已经插入的2015年04月 显示为2015-04-01,如下:
    SQL> select concat(replace(replace('2015年04月', '年', '-'), '月', '-'), '01') from dual;
    CONCAT(REPLACE(REPLACE('2015年
    ------------------------------
    2015-04-01其中concat()函数相当于“||”连接符号,故可以使用如下方式:
    SQL> select replace(replace('2015年04月', '年', '-'), '月', '-') || '01' from dual;
    REPLACE(REPLACE('2015年04月','
    ------------------------------
    2015-04-01②就是楼主如果希望改变原来插入值的格式为2015-04-01,则就需要使用修改的方式解决如下:
    SQL> update 表名 set 字段名 = to_date('2015-04-01', 'yyyy-mm-dd') where 更改条件;
    1 row updated
      

  6.   

    select  to_char(
            to_date(
           replace( replace('2015年04月','年',''),'月','')
            ,'yyyyMM'
            ),'yyyy-mm-dd'
                    ) from dual