如何处理 Oracle 中的无效日期 SQL
how to handle invalid date in Oracle SQL
有一栏显示一些交易的报表
例如:'date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843'
而且我只从声明中取出了日期。在某些情况下,存在无效日期,例如 date-05-032-2020(月份中的三个数字而不是两个数字)
因此,当我执行查询时出现错误,因为我在使用 TO_DATE 函数将 varchar 从上一条语句中提取后转换为日期
所以我需要的是 excel sheet 但在 oracle SQL
中类似 IfError 的函数
例如:如果 DATE_x 无效则将其替换为 DATE_x2
with x as(
select TO_DATE('01/20/2021','DD-MM-YYYY') DATE_x,
TO_DATE('01/02/2021','DD-MM-YYYY') DATE_x2
from dual
)
select
DATE_x2
from x;
请指教
一种选择是使用 regexp_like
检查日期格式。您发布的示例表明它是 'dd-mm-yyyy'
,这意味着 [2 位数字 - 2 位数字 - 4 位数字]。当然,[43-85-0123]
“匹配”这样的格式但不代表有效日期。无论如何,看看它是否有帮助。在我的示例中,我用今天的日期替换了无效的日期值。阅读代码中的注释。
SQL> with test (col) as
2 -- sample data; the 2nd row is invalid
3 (select 'date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843' from dual union all
4 select 'date-05-032-2020-Tran-100002345-W.44321-CR-9999eu843' from dual
5 ),
6 exdate as
7 -- extract "date" substrings (values between 1st and 4th "-" character)
8 (select col,
9 substr(col, instr(col, '-') + 1,
10 instr(col, '-', 1, 4) - instr(col, '-') - 1
11 ) datum
12 from test
13 )
14 select col,
15 case when regexp_like(datum, '[0-9]{2}-[0-9]{2}-[0-9]{4}') then
16 to_date(datum, 'dd-mm-yyyy')
17 else trunc(sysdate)
18 end as result
19 from exdate
20 /
COL RESULT
---------------------------------------------------- ----------
date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843 05-03-2020
date-05-032-2020-Tran-100002345-W.44321-CR-9999eu843 01-08-2021
SQL>
to_date中有一个default... on conversion error
参数:https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/TO_DATE.html#GUID-D226FA7C-F7AD-41A0-BB1D-BD8EF9440118
所以你可以写TO_DATE('01/20/2021' default '01-01-2020' on conversion error, 'DD-MM-YYYY')
或TO_DATE('01/20/2021' default null on conversion error ,'DD-MM-YYYY')
。
还有validate_conversion
函数:https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/VALIDATE_CONVERSION.html#GUID-DC485EEB-CB6D-42EF-97AA-4487884CB2CD
有一栏显示一些交易的报表
例如:'date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843'
而且我只从声明中取出了日期。在某些情况下,存在无效日期,例如 date-05-032-2020(月份中的三个数字而不是两个数字)
因此,当我执行查询时出现错误,因为我在使用 TO_DATE 函数将 varchar 从上一条语句中提取后转换为日期
所以我需要的是 excel sheet 但在 oracle SQL
中类似 IfError 的函数例如:如果 DATE_x 无效则将其替换为 DATE_x2
with x as(
select TO_DATE('01/20/2021','DD-MM-YYYY') DATE_x,
TO_DATE('01/02/2021','DD-MM-YYYY') DATE_x2
from dual
)
select
DATE_x2
from x;
请指教
一种选择是使用 regexp_like
检查日期格式。您发布的示例表明它是 'dd-mm-yyyy'
,这意味着 [2 位数字 - 2 位数字 - 4 位数字]。当然,[43-85-0123]
“匹配”这样的格式但不代表有效日期。无论如何,看看它是否有帮助。在我的示例中,我用今天的日期替换了无效的日期值。阅读代码中的注释。
SQL> with test (col) as
2 -- sample data; the 2nd row is invalid
3 (select 'date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843' from dual union all
4 select 'date-05-032-2020-Tran-100002345-W.44321-CR-9999eu843' from dual
5 ),
6 exdate as
7 -- extract "date" substrings (values between 1st and 4th "-" character)
8 (select col,
9 substr(col, instr(col, '-') + 1,
10 instr(col, '-', 1, 4) - instr(col, '-') - 1
11 ) datum
12 from test
13 )
14 select col,
15 case when regexp_like(datum, '[0-9]{2}-[0-9]{2}-[0-9]{4}') then
16 to_date(datum, 'dd-mm-yyyy')
17 else trunc(sysdate)
18 end as result
19 from exdate
20 /
COL RESULT
---------------------------------------------------- ----------
date-05-03-2020-Tran-100002345-W.44321-CR-9999eu843 05-03-2020
date-05-032-2020-Tran-100002345-W.44321-CR-9999eu843 01-08-2021
SQL>
to_date中有一个default... on conversion error
参数:https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/TO_DATE.html#GUID-D226FA7C-F7AD-41A0-BB1D-BD8EF9440118
所以你可以写TO_DATE('01/20/2021' default '01-01-2020' on conversion error, 'DD-MM-YYYY')
或TO_DATE('01/20/2021' default null on conversion error ,'DD-MM-YYYY')
。
还有validate_conversion
函数:https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/VALIDATE_CONVERSION.html#GUID-DC485EEB-CB6D-42EF-97AA-4487884CB2CD