sql 身份证号码提取出生日期
来源:百度知道 编辑:UC知道 时间:2024/05/19 13:26:42
如何在SQL数据库中直接搞定》?我导入数据后,系统把标记为NULL的出生年月字段都改为了1900-1-1,如何在数据库中直接提取出生日期,写入BIRTH字段中,把标记为1900-1-1的字段也根据身份证号码提取更改
if exists (select * from sysobjects where name='f_getbirth' and xtype='FN')
drop function f_getbirth
go
create function f_getbirth (@card varchar(20))
returns datetime
as
begin
declare @year varchar(4),@month varchar(2),@day varchar(2),@len int,@birth datetime
select @len=len(@card)
if @len=18
begin
select @year=substring(@card,7,4)
select @month=substring(@card,11,2)
select @day=substring(@card,13,2)
end
if @len=15
begin
select @year=substring(@card,7,2)
if cast(@year as int)>9
select @year='19'+@year
else
select @year='20'+@year
select @month=substring(@card,9,2)
select @day=substring(@card,11,2)
end
select @birth=cast(@year+'-'+@month+'-'+@day as datetime)
return @birth
end
up