--調用方法 select dbo.wsh_sfzjy ('身份證號')
ALTER FUNCTION [dbo].[wsh_sfzjy]
(
@sfz varchar(20)
)
RETURNS varchar(100)
AS
BEGIN
DECLARE @PY varchar(100)
DECLARE @tou varchar(100)
DECLARE @wei varchar(100)
DECLARE @jiaoyan varchar(100)
set @tou=''
set @wei=''
if( LEN(@sfz)=18)
BEGIN
set @tou=substring(@sfz,1,17)
set @wei=substring(@sfz,18,1)
END
else
if(LEN(@sfz)=17)
BEGIN
set @tou=@sfz
end
if(@tou<>'')
BEGIN
with sfz as (SELECT @tou as pspt_id)
select
/*pspt_id為用戶身份證號碼前17位*/
@jiaoyan=( case
when substring(a.pspt_id,11,2) not between '01' and '12' then 'cuowu'
when substring(a.pspt_id,13,2) not between '01' and '31' then 'cuowu'
when ( 12-
((
substring(a.pspt_id,1,1)*7+
substring(a.pspt_id,2,1)*9+
substring(a.pspt_id,3,1)*10+
substring(a.pspt_id,4,1)*5+
substring(a.pspt_id,5,1)*8+
substring(a.pspt_id,6,1)*4+
substring(a.pspt_id,7,1)*2+
substring(a.pspt_id,8,1)*1+
substring(a.pspt_id,9,1)*6+
substring(a.pspt_id,10,1)*3+
substring(a.pspt_id,11,1)*7+
substring(a.pspt_id,12,1)*9+
substring(a.pspt_id,13,1)*10+
substring(a.pspt_id,14,1)*5+
substring(a.pspt_id,15,1)*8+
substring(a.pspt_id,16,1)*4+
substring(a.pspt_id,17,1)*2
)%11) )%11=10 then 'x'
else CONVERT(VARCHAR(5),( 12-
((
substring(a.pspt_id,1,1)*7+
substring(a.pspt_id,2,1)*9+
substring(a.pspt_id,3,1)*10+
substring(a.pspt_id,4,1)*5+
substring(a.pspt_id,5,1)*8+
substring(a.pspt_id,6,1)*4+
substring(a.pspt_id,7,1)*2+
substring(a.pspt_id,8,1)*1+
substring(a.pspt_id,9,1)*6+
substring(a.pspt_id,10,1)*3+
substring(a.pspt_id,11,1)*7+
substring(a.pspt_id,12,1)*9+
substring(a.pspt_id,13,1)*10+
substring(a.pspt_id,14,1)*5+
substring(a.pspt_id,15,1)*8+
substring(a.pspt_id,16,1)*4+
substring(a.pspt_id,17,1)*2
)%11) )%11)
end )
from sfz a
END
if(@tou='') set @PY='輸入錯誤'
ELSE if(@jiaoyan='cuowu') set @PY='輸入錯誤'
else if (@wei=@jiaoyan) set @PY=@sfz
else if ( @wei='') set @PY=CONVERT(VARCHAR(50),@sfz)+@jiaoyan
else set @PY='輸入錯誤'
--set @PY=@sfz17+' '+@PY
RETURN @PY
END
第二代身份證號組成規則:
a) 身份證號碼(18位)= 地址碼(6)+ 出生日期碼(8)+ 順序碼(3)+校驗碼(1);
b) 地址碼:保證位數合法即可,無需校驗合法性;
c) 出生日期碼:格式為YYYYMMDD,需校驗日期有效性;
d) 順序碼:男性為奇數,女性為偶數;
e) 校驗碼:
S = ∑(i = 1, 17) { A[i] * W[i] }
Y = S % 11
校驗碼 N = (12 - Y) % 11
所以N取值范圍是0-10,10在身份證號碼中用大寫字母'X'表示
i:表示號碼字符從左至右不包括校驗碼字符在內的位置序號
A[i]:表示第i位置上的身份證號碼字符值
W[i]:表示第i位置上的加權系數,其數列為7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
