首页 > 数据库技术 > 详细

sql 查询年龄

时间:2018-12-25 10:46:04      阅读:167      评论:0      收藏:0      [点我收藏+]
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)

技术分享图片

sql 查询年龄

原文:https://www.cnblogs.com/wgx0428/p/10172314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!