使用t-sql从身份证号中提取生日

使用t-sql从身份证号中提取生日,一下是转换16位身份证号的例子,仅供参考。

create function getDateFromID(
@id char(15)
)
returns datetime
as
begin
declare @birthPart char(6);
set @birthPart = substring(@id,7,6);
declare @year int;
set @year = cast(left(@birthPart,2) as int);
if @year < 10 
SET @year = 2000 + @year;
else
SET @year = 1900 + @year;

declare @birthday datetime;
set @birthday = cast(cast(@year as char(4)) + '-' 
+ substring(@birthpart,3,2) + '-'
+ substring(@birthpart,6,2) as datetime)
return @birthday
end
GO
declare @id char(16)
set @id = '510106830328511';
print dbo.getDateFromID(@id)

 

posted on 2014-08-31 21:58  davidkam  阅读(438)  评论(0编辑  收藏  举报