当前位置: 移动技术网 > IT编程>数据库>Oracle > Oracle日期校验函数

Oracle日期校验函数

2019年04月25日  | 移动技术网IT编程  | 我要评论
使用背景:公司有一个存储过程,insert 总是不成功,之后debug,看到insert语句中有对日期处理的函数,
1,,如何使用本函数
(1), 在sql语句中使用:
sql> select fn_isdate(replace('2015-05-12','-','')) from dual; fn_isdate(replace('2015-05-12' ------------------------------ 1 sql> select fn_isdate(replace('2015-05-32','-','')) from dual; fn_isdate(replace('2015-05-32' ------------------------------ 0 sql>
create or replace procedure is begin if fn_isdate(slotdate)=1 then insert into pesk.r_hr_slot(....)values(......); commit; end if; end
2,存储函数内容如下:
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;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网