如何根据身份证信息查出性别,并且进行插入操作?

本白 2018-03-01 05:54:24
如题,问题有两部分
第一部分,需要根据一列身份证信息查出性别,但是这一列身份证信息有18位,也有15位(表名为Test,身份证信息列为xinxi)。
需要根据不同的情况进行判断,判断规则为15位身份证最后一位单数为男,双数为女,如果是X,忽略不管;18位身份证倒数第二位单数为男,双数为女。
将判断出性别的信息根据Test表中的xinxi列,插入到Test表中(新增一列)

请教大家,该如何操作呢?
...全文
683 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
本白 2018-03-02
  • 打赏
  • 举报
回复
引用 2 楼 mvobyjdyuan 的回复:
update TEST t set t.gender = CASE WHEN LENGTH(t.xinxi)=15 AND to_char(SUBSTR(t.xinxi,15,1))<>'X' AND MOD(SUBSTR(t.xinxi,15,1),2)=1 THEN '男' WHEN LENGTH(t.xinxi)=15 AND to_char(SUBSTR(t.xinxi,15,1))<>'X' AND MOD(SUBSTR(t.xinxi,15,1),2)=0 THEN '女' WHEN LENGTH(t.xinxi)=18 AND MOD(SUBSTR(t.xinxi,17,1),2)=1 THEN '男' WHEN LENGTH(t.xinxi)=18 AND MOD(SUBSTR(t.xinxi,17,1),2)=0 THEN '女' ELSE NULL END ----突然发现update写的好别扭,本来想创建一张临时查询的表来做,结果不行,估摸着应该创建一张历史表,用merge into 更好一些。
谢谢你的帮助!
本白 2018-03-02
  • 打赏
  • 举报
回复
引用 1 楼 qq_36611253 的回复:
update test set newcolumn=( case when length(xinxi)=15 and mod(substr(xinxi,15,1),2)=1 then '男' when length(xinxi)=15 and mod(substr(xinxi,15,1),2)=0 then '女' when length(xinxi)=18 and mod(substr(xinxi,17,1),2)=1 then '男' when length(xinxi)=18 and mod(substr(xinxi,17,1),2)=0 then '女' else '' end ) 瞎写下
谢谢你的帮助!
  • 打赏
  • 举报
回复
update TEST t set t.gender = CASE WHEN LENGTH(t.xinxi)=15 AND to_char(SUBSTR(t.xinxi,15,1))<>'X' AND MOD(SUBSTR(t.xinxi,15,1),2)=1 THEN '男' WHEN LENGTH(t.xinxi)=15 AND to_char(SUBSTR(t.xinxi,15,1))<>'X' AND MOD(SUBSTR(t.xinxi,15,1),2)=0 THEN '女' WHEN LENGTH(t.xinxi)=18 AND MOD(SUBSTR(t.xinxi,17,1),2)=1 THEN '男' WHEN LENGTH(t.xinxi)=18 AND MOD(SUBSTR(t.xinxi,17,1),2)=0 THEN '女' ELSE NULL END ----突然发现update写的好别扭,本来想创建一张临时查询的表来做,结果不行,估摸着应该创建一张历史表,用merge into 更好一些。
qq_36611253 2018-03-01
  • 打赏
  • 举报
回复
update test set newcolumn=( case when length(xinxi)=15 and mod(substr(xinxi,15,1),2)=1 then '男' when length(xinxi)=15 and mod(substr(xinxi,15,1),2)=0 then '女' when length(xinxi)=18 and mod(substr(xinxi,17,1),2)=1 then '男' when length(xinxi)=18 and mod(substr(xinxi,17,1),2)=0 then '女' else '' end ) 瞎写下

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧