wangcomputer00 2012-07-06
个人写的一个判断是否是正确日期字符串的函数,请大家点评/*
函数参数str:需要转换为日期的字符串,fmt:日期格式,nls:日期字符串的语言
返回值:类型为date,如果字符串不符合转换格式的要求或是非法的日期字符串,返回值为null,反之,返回转换后的date值
*/
CREATEORREPLACEFUNCTIONisdate(strvarchar2,fmtvarchar2defaultnull,nlsvarchar2defaultnull)
RETURNdate
IS
v_datedate;
v_fmtvarchar2(100)defaultfmt;
v_nlsvarchar2(100)defaultnls;
BEGIN
IFfmtISNULLTHEN
SELECTvalue
INTOv_fmt
FROMv$nls_parameters
WHEREparameter='NLS_DATE_FORMAT';
ENDIF;
IFnlsISNULLTHEN
SELECT'NLS_DATE_LANGUAGE='''||value||''''
INTOv_nls
FROMv$nls_parameters
WHEREparameter='NLS_DATE_LANGUAGE';
ENDIF;
v_date:=to_date(str,v_fmt,v_nls);
RETURNv_date;
EXCEPTION
WHENOTHERSTHEN
/*如果你希望看到报错,就把下面的注释行打开*/
--raise;
RETURNNULL;
END;
/
-------------------
以下是函数isdate的使用示例SQL>COLUMNPARAMETERFORMATA30
SQL>COLUMNVALUEFORMATA30
SQL>setnull[null]
SQL>select*fromv$nls_parameterswhereparameterin('NLS_DATE_FORMAT','NLS_DATE_LANGUAGE');
PARAMETERVALUE
------------------------------------------------------------
NLS_DATE_FORMATDD-MON-RR
NLS_DATE_LANGUAGESIMPLIFIEDCHINESE
SQL>selectisdate('1-1月-06')asdayfromdual;
DAY
----------
01-1月-06
SQL>selectisdate('01-jan-06')asdayfromdual;
DAY
----------
[null]
SQL>altersessionsetnls_date_language=american;
会话已更改。
SQL>selectisdate('01-jan-06')asdayfromdual;
DAY
---------
01-JAN-06
SQL>selectisdate('2006-01-01','yyyy-mm-dd')asdayfromdual;
DAY
---------
01-JAN-06
SQL>selectisdate('1-1月-06','dd-mon-yy','nls_date_language=simplifiedchinese')asdayfromdual;
DAY
---------
[null]
SQL>selectisdate('1-1月-06','dd-mon-yy','nls_date_language=''simplifiedchinese''')asdayfromdual;
DAY
---------
01-JAN-06
SQL>