mysql验证身份证号正确_通过SQL校验身份证号码是否正确
根據(jù)提供的身份證號(hào)碼信息驗(yàn)證身份證號(hào)碼是否符合二代身份證規(guī)范,其中區(qū)域編碼網(wǎng)上可下載。
使用數(shù)據(jù)庫為DB2,但目測(cè)可以通用身份證號(hào)碼第18位驗(yàn)證算法從網(wǎng)上查得,具體驗(yàn)證算法如下:
1、將前面的身份證號(hào)碼17位數(shù)分別乘以不同的系數(shù)。第i位對(duì)應(yīng)的數(shù)為[2^(18-i)]mod11。從第一位到第十七位的系數(shù)分別為:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 ;
2、將這17位數(shù)字和系數(shù)相乘的結(jié)果相加;
3、用加出來和除以11,看余數(shù)是多少?;
4、余數(shù)只可能有0 1 2 3 4 5 6 7 8 9 10這11個(gè)數(shù)字。其分別對(duì)應(yīng)的最后一位身份證的號(hào)碼為1 0 X 9 8 7 6 5 4 3 2;
相關(guān)mysql視頻教程推薦:《mysql教程》
select
/*pspt_id為用戶身份證號(hào)碼*/
a.pspt_id
/*判斷用戶身份證是否符合規(guī)則*/
,case
/*判斷身份證號(hào)碼是否為18位*/
when length(a.pspt_id)<>'18' then '身份證號(hào)碼位數(shù)不對(duì)'
/*判斷身份證號(hào)碼前17位是否含除數(shù)字外的字符*/
when translate(substr(a.pspt_id,1,17),'','0123456789') <>'' then '身份證號(hào)碼前17位格式不正確'
/*判斷身份證的年份是否在合理范圍內(nèi)*/
when substr(a.pspt_id,7,4) not between '1900' and '2014' then '身份證年份錯(cuò)誤'
/*判斷身份證的月份是否在合理范圍內(nèi)*/
when substr(a.pspt_id,11,2) not between '01' and '12' then '身份證月份錯(cuò)誤'
/*判斷身份證日期是否在合理范圍內(nèi)*/
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 '身份證日期錯(cuò)誤'
/*判斷身份證號(hào)碼的第18位是否符合驗(yàn)證規(guī)則*/
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 '身份證驗(yàn)證錯(cuò)誤'
/*判斷身份證號(hào)碼的區(qū)域編碼是否符合規(guī)則*/
when b.county_sar_code is null then '區(qū)縣編碼校驗(yàn)錯(cuò)誤'
else '有效實(shí)名制客戶'
end
/*用戶信息表,包含所需要查詢的身份證號(hào)碼信息*/
from usr_info a
/*身份證的行政區(qū)域編碼表,從統(tǒng)計(jì)局官網(wǎng)和網(wǎng)上可以下載,設(shè)定county_sar_code為6位行政編碼*/
left join csounty_sar b
on substr(a.pspt_id,1,6)=b.county_sar_code
總結(jié)
以上是生活随笔為你收集整理的mysql验证身份证号正确_通过SQL校验身份证号码是否正确的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB学习——常用语句
- 下一篇: as真机调试_如何使用真机调试andro