在oracle中选择无效的日期字符串

Fer*_*ito 1 sql oracle date

nvarchar2在数据库表中有一个列,它存储表示日期的字符串,例如'2016-12-05'.

例如,有'2012-19-34'什么方法可以检测不包含有效日期字符串的行?


我想有一个语句设置任何无效的日期字符串null.

sch*_*rik 6

你可以编写一个试图将字符串转换为日期的函数:

create function check_date(p_date_str  in nvarchar2) return number as
v_result NUMBER(1,0);
v_date    DATE;
begin 
  v_result := 1;
  begin 
    v_date := to_date( p_date_str, 'YYYY-MM-DD');
  exception  when others then 
   v_result := 0;
  end;
return v_result;  
end;
/
Run Code Online (Sandbox Code Playgroud)

然后用它来查找无效记录:

select * from my_table where check_date(column_with_date_as_str) = 0;
Run Code Online (Sandbox Code Playgroud)