ALTER FUNCTION [dbo].[Birthday]
(
@IDCardNo NVARCHAR(50)
)
RETURNS DATETIME
AS
BEGIN
DECLARE @Birthday DATETIME
if (LEN(@IDCardNo)=15 or LEN(@IDCardNo)=16) and SUBSTRING(@IDCardNo,9,2) between 1 and 12 and SUBSTRING(@IDCardNo,11,2) between 1 and 31
SET @Birthday= CONVERT(VARCHAR(10),'19'+SUBSTRING(@IDCardNo,7,2)+'-'+SUBSTRING(@IDCardNo,9,2)+'-'+SUBSTRING(@IDCardNo,11,2),120)
else if LEN(@IDCardNo)=18 and SUBSTRING(@IDCardNo,7,2)>=19 and SUBSTRING(@IDCardNo,11,2) between 1 and 12 and SUBSTRING(@IDCardNo,13,2) between 1 and 31
SET @Birthday= CONVERT(VARCHAR(10),SUBSTRING(@IDCardNo,7,4)+'-'+SUBSTRING(@IDCardNo,11,2)+'-'+SUBSTRING(@IDCardNo,13,2),120)
else
SET @Birthday=null
RETURN(@Birthday)
END
|