Create Function [dbo].[fn_SJ](@ID Varchar(15))
Returns Varchar(18)
As
Begin
If Len(@ID)!=15
Return Null
Declare @SFZ Varchar(18)
Set @SFZ=Left(@ID,6)+'19'+Right(@ID,9)
Declare @Q Varchar(17),@YZM Varchar(1)
Set @Q='1,0,X,9,8,7,6,5,4,3,2,'
Declare @X Varchar(40)
Set @X='7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,'
Declare @S int,@I int
Set @S=0
Set @I=1
While @I<18
Begin
Set @S=@S+Convert(int,SUBSTRING(@SFZ,@I,1))*Left(@X,CharIndex(',',@X)-1)
Set @X=Substring(@X,CharIndex(',',@X)+1,40)
Set @I=@I+1
End
Set @S=@S%11
Set @YZM=Substring(@Q,CharIndex(',' ,@Q ,@S*2)+1 ,1)
Return @SFZ+@YZM
End