设为首页 加入收藏

TOP

Oracle日期校验函数
2015-07-24 10:07:15 来源: 作者: 【 】 浏览:1
Tags:Oracle 日期 校验 函数
使用背景:公司有一个存储过程,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;
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OracleFGA细粒度审计 下一篇Oracle管道函数(PipelinedTableFu..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)