Oracle根據身份證號碼判斷性別,年齡

2021-08-08 07:16:32 字數 2989 閱讀 3451

一、oracle根據身份證判斷性別:

女生身份證:

431382198103246985

男生身份證:

150921197208173492

sql語句如下:

select decode(mod (to_number(substr('110228197802199547' ,17, 1)),2 ),0, '女','男' ) as *** from dual ;

select decode(mod (to_number(substr('530323197503252610' ,17, 1)),2 ),0, '女','男' ) as *** from dual ;

select decode(mod (to_number(substr('110228197802199547' ,17, 1)),2 ),0, '2','1' ) as *** from dual ;

select decode(mod (to_number(substr('530323197503252610' ,17, 1)),2 ),0, '2','1' ) as *** from dual ;

二、oracle根據身份證判斷年齡:

方法一

select (to_char(sysdate, 'yyyy') - substr('430426199303014475', 7, 4)) age from dual;

方法二

select trunc(to_number(to_char(sysdate,'yyyymmdd'))/10000 - to_number(to_char(to_date('1982-8-28','yyyy-mm-dd'),'yyyymmdd'))/10000) from dual

方法三

select trunc((to_char(sysdate,'yyyymmdd')-to_char(to_date(substr('430426199303014475',7,8),'yyyy-mm-dd'),'yyyymmdd'))/10000)  from dual;

三、根據15、18位身份證獲取年齡性別

select 

case

when length(idcard) = 18 then

ceil((to_char(sysdate, 'yyyymmdd') -

to_char(to_date(substr(idcard, 7, 8), 'yyyy-mm-dd'),

'yyyymmdd')) / 10000)

when length(idcard) = 15 then

ceil((to_char(sysdate, 'yyyymmdd') -

to_char(to_date('19' || substr(idcard, 7, 6),

'yyyy-mm-dd'),

'yyyymmdd')) / 10000)

end as age,

case

when length(idcard) = 18 then

decode(mod(to_number(substr(idcard, 17, 1)), 2),

0,'2','1')

when length(idcard) = 15 then

decode(mod(to_number(idcard), 2), 0, '2', '1')

end as ***

from usr where guid='230e20a6ffa1b41ce050ae0ac684959f'

上面獲取的格式是"1990" 只包含了年沒有包含出生日期,如果要包含出生日期"1990-01-12",就得使用如下格式:

select

case

when length(idcard) = 18 then

ceil((to_char(sysdate, 'yyyymmdd') -

to_char(to_date(substr(idcard, 7, 8), 'yyyy-mm-dd'),

'yyyymmdd')) / 10000)

when length(idcard) = 15 then

ceil((to_char(sysdate, 'yyyymmdd') -

to_char(to_date('19' || substr(idcard, 7, 6),

'yyyy-mm-dd'),

'yyyymmdd')) / 10000)

end as age,

case

when length(idcard) = 18 then

decode(mod(to_number(substr(idcard, 17, 1)), 2),

0,'2','1')

when length(idcard) = 15 then

decode(mod(to_number(idcard), 2), 0, '2', '1')

end as ***,

case

when length(idcard)=18 then

to_char(to_date(substr(idcard, 7, 8), 'yyyy-mm-dd'),'yyyy-mm-dd')

when length(idcard)= 15 then

to_char(to_date('19'||substr(idcard, 7,6), 'yyyy-mm-dd'),'yyyy-mm-dd')

end as birthday,id as mobile,name,guid

from usr where guid='174bd3d5879c3bf0e050007f010077dc'

身份證號碼

每乙個人自出生後都會有乙個身份證號碼。根據我國有關部門規定,公民身份號碼是特徵組合碼,由十七位數字本體碼和一位數字校驗碼組成。排列順序從左至右依次為 六位數字位址碼,八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。居民身份證是國家法定的證明公民個人身份的有效證件。例如 對於身份證號碼330719...

Oracle 身份證號碼校驗

今天來了身份證校驗的需求,想在oracle中校驗後將錯誤資料查詢出來。於是我在網上查到了身份證校驗的oracle函式。但是發現一些正確的身份證號也未校驗通過!後來經過千辛萬苦的努力終於發現了,網上給的函式中判斷是否是閏年的 substrb 函式應該從第7為開始擷取,而不是第6位!還有要注意最後一位校...

身份證號碼驗證

說明 驗證身份證的有效性 引數 cid 身份證號碼 返回 地方 出生時間 性別 private string checkcidinfo string cid if cid.length 18 char c cid cid.length 1 bool last false if c 0 c 9 c a...