How to calculate age in T-SQL with years, months, and days

后端 未结 24 1959
无人共我
无人共我 2020-11-22 05:42

What would be the best way to calculate someone\'s age in years, months, and days in T-SQL (SQL Server 2000)?

The datediff function doesn\'t handle year

24条回答
  •  执笔经年
    2020-11-22 06:06

    Here is how I calculate the age given a birth date and the current date.

    select case 
                when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
                    then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
                else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
            end as MemberAge
    go
    

提交回复
热议问题