sql 查询年龄

2018/12/25 10:00
阅读数 4
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        wgx
-- Create date: <Create Date, ,>
-- Description:    <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].Func_GetAge
(
    @birthday bigint
)
RETURNS int
AS
BEGIN
    DECLARE @age int ;

    if(@birthday is null or LEN(@birthday)<8)
    begin
        set @age=0;
    end
    else 
    begin
        set @age=FLOOR(datediff(DY, (LEFT(@birthday, 4) + '-' + SUBSTRING(CAST(@birthday AS varchar), 5, 2) + '-' + SUBSTRING(CAST(@birthday AS varchar), 7, 2) + ' 00:00:00'), getdate()) / 365.25);
    end
    
    RETURN @age;
END
GO

dbo.Func_GetAge(19820212)

展开阅读全文
go
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部