create or replace function FN_ISDATE |
( |
v_datestr VARCHAR2 --日期入参 |
) |
return number -- 返回1为正确,0为错误。 |
as |
/* ------------------------------------------------------------------------ |
公用函数:日期检查函数 |
调用范例: select FN_ISDATE( '20140501' ) from dual; |
------------------------------------------------------------------------*/ |
i_year number; --年 |
i_month number; --月 |
i_day number; --日 |
d_tjrq date ; --日期类型的日期 |
begin |
|
if v_datestr is null then |
return 0; |
end if; |
|
if length(trim(v_datestr)) <> 10 then |
return 0; |
end if; |
|
-- 判断日期由数字组成 |
if regexp_substr(trim(v_datestr), '[[:digit:]]+' ) is null then |
return 0; |
end if; |
|
-- 截取出年份 |
i_year:=to_number(substr(rtrim(v_datestr),1,4)); |
|
-- 截取出月份 |
i_month:=to_number(substr(rtrim(v_datestr),6,2)); |
|
-- 截取出日期 |
i_day:=to_number(substr(rtrim(v_datestr),9,2)); |
|
-- 对月份进行判断,必须在1月到12月范围之内 |
if i_month not between 1 and 12 then |
begin |
return 0; |
end ; |
end if; |
|
-- 对日期的判断,1,3,5,7,8,10,12月最大日为31,4,6,9,11月最大日为30,2月若为闰年则为29,其它年则为28. |
if i_day between 1 and 31 then |
begin |
if i_day=31 and i_month not in (1,3,5,7,8,10,12) then |
begin |
return 0; |
end ; |
end if; |
if i_month=2 then |
begin |
-- Rules 1:普通年能被4整除且不能被100整除的为闰年。 |
-- Rules 2:世纪年能被400整除的是闰年。 |
-- Rules 3:对于数值很大的年份,这年如果能整除3200,并且能整除172800则是闰年。如172800年是闰年,86400年不是闰年。 |
if ((mod(i_year,4)=0 and mod(i_year,100)<>0) |
or mod(i_year,400)=0 |
or (mod(i_year,3200)=0 and mod(i_year,172800)=0)) then |
begin |
--若为闰年,则2月份最大日为29 |
if i_day>29 then |
begin |
return 0; |
end ; |
end if; |
end ; |
else |
begin |
--若不为闰年,则2月份最大日为28 |
if i_day>28 then |
begin |
return 0; |
end ; |
end if; |
end ; |
end if; |
end ; |
end if; |
return 1; |
end ; |
else |
return 0; |
end if; |
end ; |