SQL SERVER 2000中查询新身份证(18位)的校验位的函数(2分)

  • 主题发起人 主题发起人 softyes
  • 开始时间 开始时间
S

softyes

Unregistered / Unconfirmed
GUEST, unregistred user!
根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。<br><br>地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。生日期码表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。顺序码表示同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号。顺序码的奇数分给男性,偶数分给女性。校验码是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。下面举例说明该计算方法。<br><br>15位的身份证编码首先把出生年扩展为4位,简单的就是增加一个19,但是这对于1900年出生的人不使用(这样的寿星不多了)<br><br>某男性公民身份号码本体码为34052419800101001,首先按照公式⑴计算:<br><br>∑(ai×Wi)(mod 11)……………………………………(1)<br><br>公式(1)中:<br>i----表示号码字符从由至左包括校验码在内的位置序号;<br>ai----表示第i位置上的号码字符值;<br>Wi----示第i位置上的加权因子,其数值依据公式Wi=2(n-1)(mod 11)计算得出。<br><br>i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1<br><br>ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 a1<br><br>Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1<br><br>ai×Wi 21 36 0 25 16 16 2 9 48 0 0 9 0 5 0 0 2 a1<br><br>根据公式(1)进行计算:<br><br>∑(ai×Wi) =(21+36+0+25+16+16+2+9+48++0+0+9+0+5+0+0+2) = 189<br><br>189 ÷ 11 = 17 + 2/11<br><br>∑(ai×Wi)(mod 11) = 2<br><br>然后根据计算的结果,从下面的表中查出相应的校验码,其中X表示计算结果为10:<br><br>∑(ai×WI)(mod 11) 0 1 2 3 4 5 6 7 8 9 10<br>校验码字符值ai 1 0 X 9 8 7 6 5 4 3 2<br>根据上表,查出计算结果为2的校验码为所以该人员的公民身份号码应该为 34052419800101001X。<br><br>a[0]*7+a[1]*9+a[2]*10+a[3]*5+a[4]*8+a[5]*4+a[6]*2+a[7]*1+a[8]*6+a[9]*3<br>+a[10]*7+a[11]*9+a[12]*10+a[13]*5+a[14]*8+a[15]*4+a[16]*2<br>%11<br><br><br>when 0 then '1' when 1 then '0' when 2 then 'X' when 3 then '9'<br>when 4 then '8' when 5 then '7' when 6 then '6' when 7 then '5'<br>when 8 then '4' when 9 then '3' when 10 then '2'<br><br>*/<br><br><br>以上为算法详细说明,下面是SQL的自定义函数,返回值为18位的校验码<br><br>Create function getCheckCode(@SFZH char(18))<br>Returns char(1)<br>As<br>Begin<br>&nbsp; declare @r char(1)<br>&nbsp; declare @i int<br>&nbsp; if len(@SFZH) &lt;&gt; 18<br>&nbsp; &nbsp; set @r = '?'<br>&nbsp; else<br>&nbsp; &nbsp; set @i = cast(substring(@SFZH,1,1) as int) * 7<br>&nbsp; &nbsp; +cast(substring(@SFZH,2,1) as int) * 9<br>&nbsp; &nbsp; +cast(substring(@SFZH,3,1) as int) * 10<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,4,1) as int) * 5<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,5,1) as int) * 8<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,6,1) as int) * 4<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,7,1) as int) * 2<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,8,1) as int) * 1<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,9,1) as int) * 6<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,10,1) as int) * 3<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,11,1) as int) * 7<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,12,1) as int) * 9<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,13,1) as int) * 10<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,14,1) as int) * 5<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,15,1) as int) * 8<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,16,1) as int) * 4<br>&nbsp; &nbsp; &nbsp; +cast(substring(@SFZH,17,1) as int) * 2<br>&nbsp; set @i = @i - @i/11 * 11<br>&nbsp; set @r = (case @i<br>&nbsp; when 0 then '1' when 1 then '0' when 2 then 'X' when 3 then '9'<br>&nbsp; when 4 then '8' when 5 then '7' when 6 then '6' when 7 then '5'<br>&nbsp; when 8 then '4' when 9 then '3' when 10 then '2' else '/' end)<br>&nbsp; Return(@r)<br>End<br><br>/**//* Usage:<br>select dbo.getcheckcode('身份证号')
 
很不错啊
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部