检查日期是否格式正确,如果日期之前或之后有空格则忽略

时间:2015-11-18 21:21:41

标签: sql oracle date

我正在尝试确定SQL中的日期是否格式不正确。

我希望它是DD/MM/YYYY,如果在日期之后有前导空格或空格,我不希望日期被视为格式不正确。

目前,我的代码正在考虑在格式不正确的日期之后带有空格的日期。

我的代码如下:

\\other stuff here 

when REGEXP_LIKE(replace(column_name, ' '), '^[0-1][0-9]/[0-3][0-9]/[1-2][0-9][0-9][0-9]')

    then 1

\\other stuff here

1 个答案:

答案 0 :(得分:1)

您的正则表达式不起作用。例如,19/39/2015将被视为有效。

我建议使用一个功能:

create or replace function String2Date(str in vahrchar2) as integer is
   d DATE;
begin
   if REGEXP_LIKE(TRIM(str), '^\d{2}/\d{2}/\d{4}$') then
      d := TO_DATE(str, 'DD/MM/YYYY');
      return 1;
   else
      return 0;
   end if;
exception
   when others then
       return 0;
end;