sql根据省份证提取生日、性别

SELECT
    p.idcard,
    p.`gender`,
IF (LENGTH(p.idcard) = 18,
    CAST(SUBSTRING(p.idcard, 7, 8) AS DATETIME),
    CAST(CONCAT('19',SUBSTRING(p.idcard, 7, 6)) AS DATETIME)) AS Birthday,  
IF (SUBSTR(p.idcard, LENGTH(p.idcard) - 1,1) % 2 = 1, '男', '女') AS sex
FROM `a_community_people_5520` p 
WHERE
 p.idcard REGEXP '[1-8][0-8][0-9]{16}|[1-8][0-8][0-9]{15}X|[1-8][0-8][0-9]{13}'
AND (
    LENGTH(p.idcard) = 18
    OR LENGTH(p.idcard) = 15
)
LIMIT 1000000

你可能感兴趣的:(sql根据省份证提取生日、性别)