sql 查询年龄

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)

猜你喜欢

转载自www.cnblogs.com/wgx0428/p/10172314.html
今日推荐