--创建函数(函数来自csdn,作者不详)
create function [dbo].[Get_sex]
(
@idcardno nvarchar(50)
)
returns int
as
begin
declare @sex int
if (len(@idcardno)=18 and isnumeric(substring(@idcardno,17,1))=1 )
set @sex= (case when substring(@idcardno,17,1) in(1,3,5,7,9) then 1
when substring(@idcardno,17,1) in(2,4,6,7,0) then 2 else 0 end)
else if (len(@idcardno)=15 and isnumeric(substring(@idcardno,15,1))=1 )
set @sex= (case when substring(@idcardno,15,1) in(1,3,5,7,9) then 1
when substring(@idcardno,15,1) in(2,4,6,7,0) then 2 else 0 end)
else
set @sex=0
return(@sex)
end
--测试示例
select dbo.[Get_sex](‘222222198306043213‘)
--运行结果(1表示男0表示女)
/*
1
*/
时间: 2024-10-22 09:36:28