数据库

通过身份证分析出生年月日、性别、年龄的SQL语句

2014-03-03

update [表名] set [性别]=(case when (left(right([身份证],2),1) % 2 = 0) then '女' else '男' end ) where len([身份证])=18 and left(right([身份证],2),1)<>'X'
update [表名] set [性别]=(case when (right([身份证],1) % 2 = 0) then '女' else '男' end ) where len([身份证])=15 and right([身份证],1)<>'X'
update [表名] set [出生日期]=(right(left([身份证],10),4)+'-'+right(left([身份证],12),2)+'-'+right(left([身份证],14),2)) where len([身份证])=18
update [表名] set [出生日期]=('19'+right(left([身份证],8),2)+'-'+right(left([身份证],10),2)+'-'+right(left([身份证],12),2)) where len([身份证])=15
update [表名] set [年龄]=year(getdate())-(cast(left([出生日期],4) as int)) where [出生日期]<>''