日期:2014-05-17  浏览次数:22078 次

新手求一根据身份证得到性别及年龄的函数
身份证包含15位及18位,谢谢各位

------解决方案--------------------
SELECT ceil(months_between(sysdate,birthday)/12) AS age,
DECODE(mod(sex,2),0,'女',1,'男','其它') AS sex
FROM
(SELECT to_date(DECODE(LENGTH(a),15,'19' || SUBSTR(a,7,6),18,SUBSTR(a,7,8)),'yyyy-mm-dd') AS birthday,
SUBSTR(a,LENGTH(a)) AS sex
FROM
( SELECT '111222199010103339' AS a FROM dual
UNION ALL
SELECT '111222198010103334' FROM dual
UNION ALL
SELECT '111222651010333' FROM dual
)
)

AGE SEX 
---------------------- --- 
22 男
32 女
47 男