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

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

    select floor((datediff(day,0,@today) - datediff(day,0,@birthdate)) / 365.2425) as age
    

    There are a lot of 365.25 answers here. Remember how leap years are defined:

    • Every four years
      • except every 100 years
        • except every 400 years

提交回复
热议问题