由于身份证号中带有X 所以一般使用varchar或string类型存储 所以需要先转换成 int类型再进行判断
通过身份证的第十七位数字的余数来判断第十七位数是奇数还是偶数 奇为男 偶为女
select sfzh,
case
when cast(substr(sfzh,17,1)as int) % 2=1 then '男'
when cast(substr(sfzh,17,1)as int) % 2=0 then '女'
else '身份证号错误!'
end as 性别
from A_table
where length(sfzh)=18 and sfzh is not null;
其中对身份证号做了条件过滤 身份证号位数不足十八位或身份证号为空的不会算。
60岁及以上老人更新标签为关爱人群
update A_table
set rylx ='关爱人群'
where length(sfzh)=18 and 2023 - cast(substr(sfzh,7,4)as int)>59