How to calculate age (in years) based on Date of Birth and getDate()

前端 未结 30 2437
死守一世寂寞
死守一世寂寞 2020-11-22 02:08

I have a table listing people along with their date of birth (currently a nvarchar(25))

How can I convert that to a date, and then calculate their age in years?

30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 02:36

    Here is how i calculate age given a birth date and 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
    

提交回复
热议问题