2009年4月2日星期四

日期合法性校验函数

校验日期字符串,如果是合法的日期,则返回Date类型,否则返回null。

写法一:
--=========================================
--Usage:判断日期是否合法
--Param:字符串日期形式
--Return:
-- 如果日期合法,返回合法的Date型日期
-- 否则返回null值
--Auth:Jazz
--Date:2009-03-27
--=========================================
create or replace function CheckDate(date_in Varchar2) return Date is

begin
Return to_date(date_in,'yyyy-MM-DD');
exception
when others then
return (null);
end;


写法二:这种写法返回的是字符串,即传入什么返回什么,如果非法日期返回null。

create or replace function CheckDate(date_in Varchar2) return varchar2 is
chk_date varchar2(10);
begin
select to_date(date_in,'yyyy-mm-dd') Into chk_date from dual;
return(date_in);
exception
when others then
return (null);
end;

没有评论: