oracle中往varchar2格式的字段中插入date时间格式的值会发生什么?

来源:互联网 发布:网上商城美工招聘 编辑:程序博客网 时间:2024/06/04 17:57

 

--建立表test1

create table TEST1
(
  ID    VARCHAR2(40) default sys_guid(),
  TDATE VARCHAR2(200)
)
tablespace APP_TX_DATA
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

--插入date时间格式数据

insert into test1 values(sys_guid(),sysdate+10);、

--查询数据

select * from test1  -- tdate值为 01-7月 -15

--Q:如何将现有数据01-7月 -15格式刷成正常的YYYYMMDD格式?

update test1 t set t.tdate = '20'||substr(t.tdate,instr(t.tdate, '-', 1, 2) + 1,2)
       || case instr(t.tdate, '月', 1, 1) - (instr(t.tdate, '-', 1, 1) + 1)
          when 1 then '0' || substr(t.tdate,
                             instr(t.tdate, '-', 1, 1) + 1,
                             (instr(t.tdate, '月', 1, 1)) - (instr(t.tdate, '-', 1, 1) + 1))
          when 2 then substr(t.tdate,
                             instr(t.tdate, '-', 1, 1) + 1,
                             (instr(t.tdate, '月', 1, 1)) - (instr(t.tdate, '-', 1, 1) + 1))
          else t.tdate end
        || substr(t.tdate,0,2)
  where t.tdate like '%月%' order by t.tdate

 

0 0
原创粉丝点击