MSSQL实现身份证号15位转18位及有效性校验

效果预览:
MSSQL实现身份证号15位转18位及有效性校验
测试用例:
–1 15位转18位
select dbo.fun_utils_idnumberoprater(‘342224951001103’)
–2 检验位的检验
select dbo.fun_utils_idnumberoprater(‘342224199510011038’)
–3 出生日期的校验
select dbo.fun_utils_idnumberoprater(‘342224199513011037’)
–4 身份证位数的校验
select dbo.fun_utils_idnumberoprater(‘34222419951001103’)
函数实现:

if exists(select * from sysobjects where name='fun_utils_idnumberoprater' and type='FN')
	drop function fun_utils_idnumberoprater
go
create function fun_utils_idnumberoprater
(
@idnumber varchar(18)=''
) 
returns varchar(500)
as
/*
公民身份号码是由17位数字码和1位校验码组成。排列顺序从左至右分别为:6位地址码,8位出生日期码,3位顺序码和1位校验码。
地址码(身份证地址码对照表见下面附录)和出生日期码很好理解,顺序码表示在同一地址码所标识的区域范围内,对同年同月同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。
身份证最后一位校验码算法如下:
1. 将身份证号码前17位数分别乘以不同的系数,从第1位到第17位的系数分别为: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这些个数字,其对应的身份证最后一位校验码为1 0 X 9 8 7 6 5 4 3 2。
author:mxd
date:2019.04.12
function:
	1.校验身份证有效性
	2.15位身份证转18位
*/
begin

	declare @ReturnText varchar(100)--返回值
			,@Separator varchar(1)=','--分隔符
			
			,@idnumberElement varchar(1)--身份证每位元素
			,@CurrentIndex int=1--身份证号当前索引位
			,@NextIndex int--身份证号下一个索引位
			
			,@xsnumber varchar(50)='7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'--系数
			,@xsElement varchar(2)--系数每位元素
			,@xsCurrentIndex int=1--系数当前索引位
			,@xsNextIndex int--系数下一个索引位
			
			,@jym varchar(11)='10X98765432'--校验码
			,@sum int=0--身份证元素*系数求和
			,@div int--合计对11求余数
			
			,@CurrentJym varchar(1)=''--当前校验码
			,@CurrentWs int=0--当前身份证位数
			,@NewJym varchar(1)=''--新校验码
			,@NewWs int=0--新身份证位数

	--处理身份证号并校验位数有效性
	set @CurrentWs=len(@idnumber);
	if @CurrentWs=15
	begin
		set @idnumber=stuff(@idnumber,7,0,case when substring(@idnumber,13,3) in ('999','998','997','996') then '18' else '19' end);
		set @CurrentJym='';		
	end
	else if @CurrentWs=18
	begin
		set @CurrentJym=substring(@idnumber,18,1);
		set @idnumber=substring(@idnumber,1,17);
	end 
	else
	begin
		set @ReturnText='F校验身份证出错,传入的身份证位数无效!当前位数['+cast(@CurrentWs as varchar)+']'
		goto TheEnd;
	end
	if isdate(substring(@idnumber,7,8))=0 
	begin
		set @ReturnText='F校验身份证出错,日期部分出错!传入出生日期['+substring(@idnumber,7,8)+']'
		goto TheEnd;
	end
	--计算身份证校验位
	while(@CurrentIndex<=len(@idnumber))
	begin
		set @[email protected]+1;
		set @xsNextIndex=charindex(@Separator,@xsnumber,@xsCurrentIndex);
		
		if(@xsNextIndex=0 OR @xsNextIndex IS NULL)
			set @xsNextIndex=len(@xsnumber)+1;
		
		set @idnumberElement=substring(@idnumber,@CurrentIndex,@[email protected]);
		set @xsElement=substring(@xsnumber,@xsCurrentIndex,@[email protected]);
		
		set @[email protected]+cast(@idnumberElement as int)*cast(@xsElement as int)
		
		set @[email protected]+1;
		set @[email protected]+1;
	end	
	set @[email protected]%11;
	set @NewJym=substring(@jym,@div+1,1);
	--校验身份证校验位是否出错
	if (@CurrentJym<>'') and (@NewJym<>@CurrentJym)
	begin
		set @ReturnText='F校验身份证出错,第18位错!参考尾数['[email protected]+']'
		goto TheEnd
	end
	--输出新的身份证号
	set @[email protected][email protected]
	goto TheEnd	

TheEnd:
	return @ReturnText
end
go

下载链接:https://download.csdn.net/download/weixin_41660162/11109941

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
相关资源
相似解决方案