-- 写个函数,然后根据函数去判断:
create or replace function is_date(i_str varchar2)
return number
is
v_date date;
begin
-- to_date(i_str,'yyyymmdd') 中的 'yyyy-mm-dd' 是你的字符类型存储日期的格式!(你可以根据你的数据进行修改其格式)
select to_date(i_str,'yyymmdd') into v_date from dual;
if v_date is not null then
return 1;
else
return 0;
end if;
EXCEPTION
WHEN OTHERS
THEN
return 0;
end;
/
-- 再用 is_date()查一下,看是否存在其他非法(或日期格式不一样)的数据!
select * from tb_name where is_date(a2)=0;
-- 或者(排除空值)
select * from tb_name
where a2 is not null
and is_date(a2)=0;
-- 如果表中数据很多,且执行上面的查询效率很低的话,可以考虑在a2字段创建一个函数索引:
create index tb_idx on tb_name(is_date(a2));
-- 为什么:这样的问题,天天有人问呢?