让我们创建一个测试表来说明问题:
CREATE TABLE "TMP1" (
"X" VARCHAR2(256 BYTE) NOT NULL
) LOGGING NOCOMPRESS NOCACHE;
INSERT INTO "TMP1" VALUES ('XxYyyyZzzWww_123456_0106201395810_0106201395810.csv');
INSERT INTO "TMP1" VALUES ('XxYyyyZzzWww_123456_31052013155754_31052013155754.csv');
INSERT INTO "TMP1" VALUES ('XxYyyyZzzWww_123456_02062013130000_02062013130000.csv');
现在提出请求:
select
TMP2.X
, TMP2.STARTDATETIME
, to_char(TMP2.STARTDATETIME, 'DDMMYYYYHH24MISS') "to_char(StartDateTime)"
, TMP2.ENDDATETIME
, to_char(TMP2.ENDDATETIME, 'DDMMYYYYHH24MISS') "to_char(EndDateTime)"
from (
select
"X"
, to_date(case length(regexp_substr("X", '\d+', 1, 2, 'i'))
when 14 then regexp_substr("X", '\d+', 1, 2, 'i')
when 13 then substr(regexp_substr("X", '\d+', 1, 2, 'i'), 0, 8)
|| '0' || substr(regexp_substr("X", '\d+', 1, 2, 'i'), 9)
end, 'DDMMYYYYHH24MISS') StartDateTime
, to_date(case length(regexp_substr("X", '\d+', 1, 3, 'i'))
when 14 then regexp_substr("X", '\d+', 1, 3, 'i')
when 13 then substr(regexp_substr("X", '\d+', 1, 3, 'i'), 0, 8)
|| '0' || substr(regexp_substr("X", '\d+', 1, 3, 'i'), 9)
end, 'DDMMYYYYHH24MISS') EndDateTime
from
"TMP1"
) TMP2;
在此查询中,我们剪切了一个子字符串并将其转换为日期。然后我们尝试做反向转换,但不起作用。
结果:
to_date
功能有效,但功能to_char
无效。