sql 身份证计算年龄和性别

IdentityNumber 是身份证号

年龄:

 FLOOR(DATEDIFF(DY, substring(b.IdentityNumber,7,4), GETDATE()) / 365.25)   as '推荐人年龄',

15位的身份证计算年龄:

case 
 when b.IdentityNumber IS null  or b.IdentityNumber=''  then ''
 when len(b.IdentityNumber)=18 and dbo.IsvalidIDCard(b.IdentityNumber)=1 then CAST(DATEDIFF(YEAR, substring(b.IdentityNumber,7,4), GETDATE()) AS nvarchar(20))
 when len(b.IdentityNumber)=15 and dbo.IsvalidIDCard(b.IdentityNumber)=1  then CAST(DATEDIFF(YEAR,'19'+substring(b.IdentityNumber,7,2), GETDATE()) AS nvarchar(20))
 else '' end  as '推荐人年龄',

性别:

case when (len(b.IdentityNumber)=18 and cast(substring(b.IdentityNumber,17,1) as int)%2=1)
                 or( len(b.IdentityNumber)=15 and cast(right(b.IdentityNumber,1) as int)%2=1) then  ''
          when (len(b.IdentityNumber)=18 and cast(substring(b.IdentityNumber,17,1) as int)%2=0)
                 or( len(b.IdentityNumber)=15 and cast(right(b.IdentityNumber,1) as int)%2=0) then  ''
            else '' end   as '推荐人性别',

Published by

风君子

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

发表回复

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