首页 » ORACLE » Script: oracle 判断字符是否是日期格式

Script: oracle 判断字符是否是日期格式

如果在原数据中表中数据是以字符串形式保存,现在要转换为date日期类型时有部分数据格式会出错,这就需要查出那些异常数据

1,可以用正则,这个比较麻烦每月天数不一样,闰年2月天数又不一样等 等

2,自己写一funcation ,捕获异常判断

sql如下

CREATE OR REPLACE FUNCTION isdate (datestr VARCHAR2, format VARCHAR2)
RETURN INT
IS
p_date   DATE;
BEGIN
   SELECT TO_DATE (datestr, format)
   INTO p_date
FROM DUAL;

   RETURN 1;
EXCEPTION
  WHEN OTHERS
THEN
   RETURN 0;
END;

/

————–

调用比如

select * from 表名 where isdate(colname,’yyyy-mm-dd’)=0;

打赏

目前这篇文章有1条评论(Rss)评论关闭。

  1. Marguerite Clack | #1
    2011-12-21 at 07:31

    I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to create my own blog and would like to find out where u got this from. thanks