sql 身份证计算年龄和性别

IdentityNumber 是身份证号

年龄:

 FLOORDATEDIFFDY, substringb.IdentityNumber,7,4), GETDATE)) / 365.25)   as '推荐人年龄',

15位的身份证计算年龄:

case 
 when b.IdentityNumber IS null  or b.IdentityNumber=''  then ''
 when lenb.IdentityNumber)=18 and dbo.IsvalidIDCardb.IdentityNumber)=1 then CASTDATEDIFFYEAR, substringb.IdentityNumber,7,4), GETDATE)) AS nvarchar20))
 when lenb.IdentityNumber)=15 and dbo.IsvalidIDCardb.IdentityNumber)=1  then CASTDATEDIFFYEAR,'19'+substringb.IdentityNumber,7,2), GETDATE)) AS nvarchar20))
 else '' end  as '推荐人年龄',

性别:

case when lenb.IdentityNumber)=18 and castsubstringb.IdentityNumber,17,1) as int)%2=1)
                 or lenb.IdentityNumber)=15 and castrightb.IdentityNumber,1) as int)%2=1) then  ''
          when lenb.IdentityNumber)=18 and castsubstringb.IdentityNumber,17,1) as int)%2=0)
                 or lenb.IdentityNumber)=15 and castrightb.IdentityNumber,1) as int)%2=0) then  ''
            else '' end   as '推荐人性别',

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注