根據提供的身份證號碼信息驗證身份證號碼是否符合二代身份證規范,其中區域編碼網上可下載。
使用數據庫為DB2,但目測可以通用身份證號碼第18位驗證算法從網上查得,具體驗證算法如下:
1、將前面的身份證號碼17位數分別乘以不同的系數。第i位對應的數為[2^(18-i)]mod11。從第一位到第十七位的系數分別為:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ;
2、將這17位數字和系數相乘的結果相加;
3、用加出來和除以11,看余數是多少?;
4、余數只可能有0 1 2 3 4 5 6 7 8 9 10這11個數字。其分別對應的最后一位身份證的號碼為1 0 X 9 8 7 6 5 4 3 2;
相關mysql視頻教程推薦:《mysql教程》
select
/*pspt_id為用戶身份證號碼*/
a.pspt_id
/*判斷用戶身份證是否符合規則*/
,case
/*判斷身份證號碼是否為18位*/
when length(a.pspt_id)<>'18' then '身份證號碼位數不對'
/*判斷身份證號碼前17位是否含除數字外的字符*/
when translate(substr(a.pspt_id,1,17),'','0123456789') <>'' then '身份證號碼前17位格式不正確'
/*判斷身份證的年份是否在合理范圍內*/
when substr(a.pspt_id,7,4) not between '1900' and '2014' then '身份證年份錯誤'
/*判斷身份證的月份是否在合理范圍內*/
when substr(a.pspt_id,11,2) not between '01' and '12' then '身份證月份錯誤'
/*判斷身份證日期是否在合理范圍內*/
when substr(a.pspt_id,13,2) not between 1 and day(to_date(char(substr(a.pspt_id,7,4)||'-'||substr(a.pspt_id,11,2)||'-01',10),'yyyy-mm-dd')+1 month-1 day) then '身份證日期錯誤'
/*判斷身份證號碼的第18位是否符合驗證規則*/
when
mod((
substr(a.pspt_id,1,1)*7+
substr(a.pspt_id,2,1)*9+
substr(a.pspt_id,3,1)*10+
substr(a.pspt_id,4,1)*5+
substr(a.pspt_id,5,1)*8+
substr(a.pspt_id,6,1)*4+
substr(a.pspt_id,7,1)*2+
substr(a.pspt_id,8,1)*1+
substr(a.pspt_id,9,1)*6+
substr(a.pspt_id,10,1)*3+
substr(a.pspt_id,11,1)*7+
substr(a.pspt_id,12,1)*9+
substr(a.pspt_id,13,1)*10+
substr(a.pspt_id,14,1)*5+
substr(a.pspt_id,15,1)*8+
substr(a.pspt_id,16,1)*4+
substr(a.pspt_id,17,1)*2
),11)
<>
(
case
when substr(a.pspt_id,18,1)='1' then '0'
when substr(a.pspt_id,18,1)='0' then '1'
when substr(a.pspt_id,18,1) in ('X','x') then '2'
when substr(a.pspt_id,18,1)='9' then '3'
when substr(a.pspt_id,18,1)='8' then '4'
when substr(a.pspt_id,18,1)='7' then '5'
when substr(a.pspt_id,18,1)='6' then '6'
when substr(a.pspt_id,18,1)='5' then '7'
when substr(a.pspt_id,18,1)='4' then '8'
when substr(a.pspt_id,18,1)='3' then '9'
when substr(a.pspt_id,18,1)='2' then '10'
end
)
then '身份證驗證錯誤'
/*判斷身份證號碼的區域編碼是否符合規則*/
when b.county_sar_code is null then '區縣編碼校驗錯誤'
else '有效實名制客戶'
end
/*用戶信息表,包含所需要查詢的身份證號碼信息*/
from usr_info a
/*身份證的行政區域編碼表,從統計局官網和網上可以下載,設定county_sar_code為6位行政編碼*/
left join csounty_sar b
on substr(a.pspt_id,1,6)=b.county_sar_code