您当前的位置: 首页 >  sql

[MSSQL]根据身份证计算性别函数

发布时间:2012-02-14 08:22:49 ,浏览量:0

Go
--创建函数(函数来自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
*/
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    106354博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0465s