--建立表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
本文创建了一个名为TEST1的表,其中TDATE字段为VARCHAR2类型,并尝试插入日期格式的数据。在查询时,发现日期显示为不寻常格式(01-7月 -15)。为了解决这个问题,提供了SQL更新语句来转换TDATE字段的格式,将其从01-7月 -15格式转换为正常的YYYYMMDD格式。
1397

被折叠的 条评论
为什么被折叠?



