varchar轉化date型別(錯誤返回預設值)的fuction
阿新 • • 發佈:2019-01-07
create or replace function verifydate(verdate in varchar2) return date
as
/* TITLE : <P>varchar轉date方法</P>
* DESCRIPTION: <P>適合字串長度為8的varchar型別進行校驗</P>
* COPYRIGHT : <P>COPYRIGHT (C) 2017</P>
* COMPANY :
* AUTHOR : zZBoring
* VERSION : 1.0
* DATE : 2017-05-16
*/
begin
--最大時間為99991231 不符合時間格式的返回null
if 4 <= length(verdate) and '9999' = substr(verdate,0,4)
then return to_date('99991231','yyyymmdd');
else if 8 = length(verdate)
then return to_date(verdate,'yyyymmdd');
else return null;
end if;
end if;
exception
WHEN OTHERS THEN
return null;
as
/* TITLE : <P>varchar轉date方法</P>
* DESCRIPTION: <P>適合字串長度為8的varchar型別進行校驗</P>
* COPYRIGHT : <P>COPYRIGHT (C) 2017</P>
* COMPANY :
* AUTHOR : zZBoring
* VERSION : 1.0
* DATE : 2017-05-16
*/
begin
--最大時間為99991231 不符合時間格式的返回null
if 4 <= length(verdate) and '9999' = substr(verdate,0,4)
then return to_date('99991231','yyyymmdd');
else if 8 = length(verdate)
then return to_date(verdate,'yyyymmdd');
else return null;
end if;
end if;
exception
WHEN OTHERS THEN
return null;
end verifydate;
別名與關鍵字相同時可以加引號
SELECT verifydate('99990231') "date" FROM dual;