以下是在写对日期处理的函数过程中用到的一些sql 函数。。。
asciistr()把字符转换为ASCII码,汉字会转成"XXXX"格式,可以用来排除汉字.
ltrim(rtrim(strdate)) 去掉前后的空格。
ltrim(rtrim(replace(strdate,chr(10),'')))去掉换行符和前后的空格
to_char(to_date( ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'yyyy'); 转换成日期格式,并取年,也可以取月日时分秒(MM,DD,HH24 ,MI,ss)
length(strdate) - length(replace(strdate,'-',''))查看某个字符在字符串中的位置(字符串长度-替换掉某个字符的字符串长度)
-------oracle CREATE OR REPLACE FUNCTION RegexpDate(strDate in varchar2,dateType in number) RETURN number is results number; l_new_string VARCHAR2(255); l_new_date VARCHAR2(255); BEGIN if(instr(asciistr(strdate),'')>0 or ltrim(rtrim(strdate))='NaN-aN-aN' or LENGTH(strdate) = 0 or UPPER(strdate) ='NULL' or ltrim(rtrim(strdate))='/' ) then--排除包含汉字的,值为0的,或者为null,及一些特殊字符 results:=0; else if(instr(strdate,'.')>0 or LENGTH(ltrim(rtrim(replace(strdate,chr(10),''))))=4 or length(strdate) - length(replace(strdate,'-',''))=1) then--对不能转为日期的数据,进行截取 IF( dateType = 1 ) THEN results:=substr(ltrim(rtrim(replace(strdate,chr(10),''))),0,4); END IF; IF( dateType = 2) THEN results:=substr(ltrim(rtrim(replace(strdate,chr(10),''))),6,1); END IF; IF( dateType = 3) THEN results:=0; END IF; IF( dateType = 4) THEN results:=0; END IF; IF( dateType = 5) THEN results:=0; END IF; IF( dateType = 6) THEN results:=0; END IF; else IF( dateType = 1 ) THEN results:=to_char(to_date( ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'yyyy'); END IF; IF( dateType = 2) THEN results:=to_char(to_date(ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'MM'); END IF; IF( dateType = 3 ) THEN results:=to_char(to_date(ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'DD'); END IF; IF( dateType = 4 ) THEN results:=to_char(to_date(ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'HH24'); END IF; IF( dateType = 5 ) THEN results:=to_char(to_date(ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'MI'); END IF; IF( dateType = 6 ) THEN results:=to_char(to_date(ltrim(rtrim(strdate)),'yY-mm-dd hH:mi:ss'),'ss'); END IF; end if; end if; RETURN (results); END RegexpDate; ----sql server CREATE FUNCTION RegexpDate(@strDate nvarchar(50), @dateType int) Returns int as BEGIN DECLARE @result nvarchar(50); IF( LEN(@strDate) = 0 or UPPER(@strDate) ='NULL' or ascii(@strDate)>127 or left(@strDate,5)='0-0-0' or CHARINDEX('.-',@strDate)>0 or right(@strDate,6)='- :1:1') begin SET @result= 0; end else begin if(CHARINDEX('年',@strDate)>0) begin SET @strDate = REPLACE(REPLACE( REPLACE(@strDate, '年', '-' ), '月', '-' ), '日', '' ); end if( CHARINDEX('星期',@strDate)>0) begin set @strDate=LEFT(@strDate,LEN(@strDate)-3); end if(LEN(@strDate) - LEN(REPLACE(@strDate, '-', ''))=4 and RIGHT(@strDate,2)<>'--') begin set @strDate=stuff(@strDate, 11, 1, ' '); set @strDate=stuff(@strDate, 14, 1, ':'); end if(right(@strDate,4)=':0:0') begin set @strDate=ltrim(rtrim(LEFT(@strDate,LEN(@strDate)-6))); end if (right(@strDate,2)='-0' or right(@strDate,2)='--'or RIGHT(@strDate,2)='::' or right(@strDate,1)='-') begin if(@dateType=1) begin set @result=LEFT(@strDate,4); end if(@dateType=2) begin set @result=SUBSTRING(@strDate,6,1); end if(@dateType=3) begin set @result=0 end if(@dateType=4) begin set @result=0 end if(@dateType=5) begin set @result=0 end if(@dateType=6) begin set @result=0 end end else begin set @strDate=CONVERT(varchar(100), @strDate, 20); if(@dateType=1) begin set @result=Datename(year, @strDate); end if(@dateType=2) begin set @result=Datename(Month, @strDate); end if(@dateType=3) begin set @result=Datename(Day, @strDate); end if(@dateType=4) begin set @result=Datename(Hour, @strDate); end if(@dateType=5) begin set @result=Datename(Minute, @strDate); end if(@dateType=6) begin set @result=Datename(Second, @strDate); end end if(@result='--'or @result='-') begin set @result=0; end end set @result=CAST(@result AS int ); RETURN @result; END; go --------------mysql CREATE DEFINER=`root`@`localhost` FUNCTION `RegexpDate`(`strDate` varchar(255),`dateType` int) RETURNS int(11) BEGIN DECLARE l_new_string VARCHAR ( 255 ); DECLARE l_new_date VARCHAR ( 255 ); DECLARE result INT; IF( LENGTH( strdate ) = 0 or UPPER(strdate) ='NULL' ) THEN SET result = 0; else if(locate('年',strDate)>0) then SET strDate = REPLACE(REPLACE( REPLACE( strDate, '年', '-' ), '月', '-' ), '日', '' ); end if; if(str_to_date(strDate, '%Y-%m-%d %H:%i:%s')<>'null') then set l_new_date=str_to_date(strDate, '%Y-%m-%d %H:%i:%s'); end if; if(str_to_date(strDate, '%Y.%m.%d %H:%i:%s')<>'null') then set l_new_date=STR_TO_DATE(strDate,'%Y.%m.%d %H:%i:%s'); end if; if(str_to_date(strDate, '%Y/%m/%d %H:%i:%s')<>'null') then set l_new_date=STR_TO_DATE(strDate,'%Y/%m/%d %H:%i:%s'); end if; SET l_new_string = l_new_date; /* IF(locate('年',strDate)>0 ) THEN SET l_new_string = REPLACE(REPLACE( REPLACE( strDate, '年', '-' ), '月', '-' ), '日', '' ); ELSE SET l_new_string = l_new_date; END IF;*/ IF( dateType = 1 ) THEN SET result = YEAR( l_new_string ); END IF; IF( dateType = 2 ) THEN SET result = MONTH( l_new_string ); END IF; IF( dateType = 3 ) THEN SET result = DAY( l_new_string ); END IF; IF( dateType = 4 ) THEN SET result = HOUR(l_new_string ); END IF; IF( dateType = 5 ) THEN SET result = MINUTE(l_new_string); END IF; IF( dateType = 6 ) THEN SET result = SECOND(l_new_string); END IF; end if; RETURN result; END