Mysql 根据身份证号码操作

前言

主要是针对一些针对身份证号码进行数据分析的场景,进行一些SQL的备忘记录。

年龄

(YEAR(CURDATE()) - SUBSTRING(身份证字段项,7,4)) AS 年龄,

性别

-- 1 男 2 女 
IF (MOD(SUBSTRING(身份证字段项,17,1),2),1,2) AS 性别
-- 汉字
IF (MOD(SUBSTRING(身份证字段项,17,1),2),'男','女') AS 性别

出生日期

substring(身份证字段项,7,8) as 出生日期

归属地

CASE LEFT(身份证字段项,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 '新疆' END AS 所属省份

脱敏

INSERT(身份证字段项,7,6,'****') as 身份证号码脱敏

15位升18位

感谢 SuperBoy_Liang 博主提供sql
原文链接 https://blog.csdn.net/superboy_liang/article/details/76326618

UPDATE tableA SET ID = CONCAT
    (
    SUBSTRING(ID,1,6),'19',SUBSTRING(ID,7,9),SUBSTRING('10X98765432',
        (CAST(SUBSTRING(ID,1,1)AS SIGNED)*7+
        CAST(SUBSTRING(ID,2,1)AS SIGNED)*9+
        CAST(SUBSTRING(ID,3,1)AS SIGNED)*10+
        CAST(SUBSTRING(ID,4,1)AS SIGNED)*5+
        CAST(SUBSTRING(ID,5,1)AS SIGNED)*8+
        CAST(SUBSTRING(ID,6,1)AS SIGNED)*4+
        1*2+
        9*1+
        CAST(SUBSTRING(ID,7,1)AS SIGNED)*6+
        CAST(SUBSTRING(ID,8,1)AS SIGNED)*3+
        CAST(SUBSTRING(ID,9,1)AS SIGNED)*7+
        CAST(SUBSTRING(ID,10,1)AS SIGNED)*9+
        CAST(SUBSTRING(ID,11,1)AS SIGNED)*10+
        CAST(SUBSTRING(ID,12,1)AS SIGNED)*5+
        CAST(SUBSTRING(ID,13,1)AS SIGNED)*8+
        CAST(SUBSTRING(ID,14,1)AS SIGNED)*4+
        CAST(SUBSTRING(ID,15,1)AS SIGNED)*2)%11+1,1))
        WHERE LENGTH(ID)=15;

你可能感兴趣的:(数据)