Oracle按身份证号得到省市、性别、年龄的示例代码

所属分类: 数据库 / oracle 阅读数: 1760
收藏 0 赞 0 分享

1、通过身份证号查询所在省市

SELECT
count(*) as total,
 case substr(t.CERTNO,0,2)
  when '11' then '北京市'
  when '12' then '天津市'
  when '13' then '河北省'
  when '14' then '山西省'
  when '15' then '内蒙古自治区'
  when '21' then '辽宁省'
  when '22' then '吉林省'
  when '23' then '黑龙江省'
  when '31' then '上海市'
  when '32' then '江苏省'
  when '33' then '浙江省'
  when '34' then '安徽省'
  when '35' then '福建省'
  when '36' then '江西省'
  when '37' then '山东省'
  when '41' then '河南省'
  when '42' then '湖北省'
  when '43' then '湖南省'
  when '44' then '广东省'
  when '45' then '广西壮族自治区'
  when '46' then '海南省'
  when '50' then '重庆市'
  when '51' then '四川省'
  when '52' then '贵州省'
  when '53' then '云南省'
  when '54' then '西藏自治区'
  when '61' then '陕西省'
  when '62' then '甘肃省'
  when '63' then '青海省'
  when '64' then '宁夏回族自治区'
  when '65' then '新疆维吾尔自治区'
  when '71' then '台湾省'
  when '81' then '香港特别行政区'
  when '82' then '澳门特别行政区'
  else '未知'
  end AS province
 FROM uip_bjt_userinfo t 
 group by case substr(t.CERTNO,0,2)
    when '11' then '北京市'
    when '12' then '天津市'
    when '13' then '河北省'
    when '14' then '山西省'
    when '15' then '内蒙古自治区'
    when '21' then '辽宁省'
    when '22' then '吉林省'
    when '23' then '黑龙江省'
    when '31' then '上海市'
    when '32' then '江苏省'
    when '33' then '浙江省'
    when '34' then '安徽省'
    when '35' then '福建省'
    when '36' then '江西省'
    when '37' then '山东省'
    when '41' then '河南省'
    when '42' then '湖北省'
    when '43' then '湖南省'
    when '44' then '广东省'
    when '45' then '广西壮族自治区'
    when '46' then '海南省'
    when '50' then '重庆市'
    when '51' then '四川省'
    when '52' then '贵州省'
    when '53' then '云南省'
    when '54' then '西藏自治区'
    when '61' then '陕西省'
    when '62' then '甘肃省'
    when '63' then '青海省'
    when '64' then '宁夏回族自治区'
    when '65' then '新疆维吾尔自治区'
    when '71' then '台湾省'
    when '81' then '香港特别行政区'
    when '82' then '澳门特别行政区'
    else '未知'end order by province desc

2、通过身份证号得到性别(第17位为奇数为男,偶数为女)

select 
  decode(mod(to_number(substr(t.useridcardnum, 17, 1)), 2),0,'女','男') as sex
 from uip_ca_userinfo t

3、通过身份证号得到年龄

select to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) as age from uip_ca_userinfo t

4、通过身份证号统计所在年龄段的人数

select count(t.id),
  case
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 1 and 20 then
   '1-20岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 21 and 30 then
   '21-30岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 31 and 40 then
   '31-40岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 41 and 50 then
   '41-50岁'
   else
   '50岁以上'
  end as 年龄段
 from uip_ca_userinfo t
 group by case
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 1 and 20 then
    '1-20岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 21 and 30 then
    '21-30岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 31 and 40 then
    '31-40岁'
   when to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 41 and 50 then
    '41-50岁'
   else
    '50岁以上'
   end
 order by 年龄段 asc

5、通过身份证号统计男女数量

select count(t.id),
  decode(mod(to_number(substr(t.useridcardnum, 17, 1)), 2),0,'女','男') as sex
 from uip_ca_userinfo t
 where to_char(sysdate, 'yyyy') - substr(t.useridcardnum, 7, 4) between 1 and 26
 group by decode(mod(to_number(substr(t.useridcardnum, 17, 1)), 2),0,'女','男')

更多精彩内容其他人还在看

VS2015连接Oracle数据库的详细步骤

这篇文章主要介绍了VS2015连接Oracle数据库的详细步骤,需要的朋友可以参考下
收藏 0 赞 0 分享

常用的Oracle doc命令(收藏)

这篇文章主要介绍了常用的Oracle doc命令(收藏)的相关资料,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

ORACLE SQL语句优化技术要点解析

这篇文章主要介绍了ORACLE SQL语句优化技术的相关内容,小编觉得挺不错的,在这里分享给大家,需要的朋友可以参考下。
收藏 0 赞 0 分享

ORACLE多条件统计查询的简单方法

这篇文章主要介绍了ORACLE多条件统计查询的简单方法,具有一定参考价值。这里给大家分享下,希望对大家有所帮助。
收藏 0 赞 0 分享

ORACLE批量导入图片到BLOB字段代码示例

这篇文章主要介绍了ORACLE批量导入图片到BLOB字段代码示例,此代码示例是文章作者的项目源码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

PLSQL Developer登录的默认密码介绍

这篇文章主要介绍了PLSQL Developer登录的默认密码介绍,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Oracle数据行拆分多行方法示例

oracle数据库使用过程中,怎样将一行或者多行数据分割成需要的多行数据,本文我们就来看看具体方法,需要的朋友可以参考。
收藏 0 赞 0 分享

Oracle表空间不足的两种解决办法

这篇文章主要介绍了Oracle表空间不足的两种解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

巧妙解决Oracle NClob读写问题(经验分享)

下面小编就为大家带来一篇巧妙解决Oracle NClob读写问题(经验分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

oracle执行update语句时卡住问题分析及解决办法

这篇文章主要介绍了oracle执行update语句时卡住问题分析及解决办法,涉及记录锁等相关知识,具有一定参考价值,需要的朋友可以了解。
收藏 0 赞 0 分享
查看更多