Go
--Create function (function from CSDN, author unknown)
Create function [dbo]. [Get_birthday]
(
@idcardno nvarchar (50)
)
Returns varchar (10)
As
Begin
DECLARE @birthday datetime
if (Len (@idcardno) =15 or Len (@idcardno) =16) and substring (@idcardno, 9,2) between 1 and Andsubstring (@idcardno, 11,2) b Etween 1 and 31
Set @birthday = CONVERT (varchar), ' +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 Andsubstrin G (@idcardno, 13,2) between 1 and 31
Set @birthday = CONVERT (varchar), substring (@idcardno, 7,4) + '-' +substring (@idcardno, 11,2) + '-' +substring (@ idcardno,13,2), 120)
Else
Set @birthday =null
return (varchar ($), @birthday, 120))
End
--Test example
SELECT dbo. [Get_birthday] (' 222222198306043213 ')
--Running results
/*
1983-06-04
*/
Get Birthday function based on ID card