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

前端 未结 30 1951
死守一世寂寞
死守一世寂寞 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:32

    What about:

    DECLARE @DOB datetime
    SET @DOB='19851125'   
    SELECT Datepart(yy,convert(date,GETDATE())-@DOB)-1900
    

    Wouldn't that avoid all those rounding, truncating and ofsetting issues?

提交回复
热议问题