The following are some of the sql function to write function processes used in date processing. . .
asciistr () to convert characters to ASCII code, Chinese character will turn into "\ XXXX" format, can be used to exclude characters.
ltrim (rtrim (strdate)) spaces around removed.
ltrim (rtrim (replace (strdate, chr (10), ''))) and to remove the spaces before and after the line feed
to_char (to_date (ltrim (rtrim (strdate)), 'yY-mm-dd hH: mi: ss'), 'yyyy'); conversion to date format, and taken in and to be taken month, day, hour (the MM, DD, HH24, MI, ss)
length (strdate) - length (replace (strdate, '-', '')) to view the location of a character in the string (the string length - replace a character string length)
-------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 the LENGTH (strDate) = 0 or the UPPER (strDate) = ' NULL ' or LTRIM ( RTRIM (strDate)) = ' / ' ) the then - negative characters comprising, a value of 0 or is null, and some special characters Results: = 0 ; the else IF (InStr (strDate, ' . ' ) > 0 or the LENGTH ( LTRIM ( RTRIM ( Replace (strDate, CHR ( 10 ), '' )))) =. 4 or length (strDate) - length ( Replace (strDate, ' - ' , '' )) = . 1 ) the then - on the date data is not converted, intercepts the IF (dateType = . 1 ) THEN Results: = substr ( LTRIM ( RTRIM ( Replace (strDate, CHR ( 10 ), '' ))), 0 , . 4 ); the END the IF ; the 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