Pages

Monday, April 16, 2007

GetAge - Get Age Using SQL Query

How to calculate Age using SQL Query

DateDiff function can be used to get the Age from the birth date

Select BirthDate, DateDiff(year, BirthDate, getdate()) as Age from HumanResources.Employee

4 comments:

  1. That only works for users that have had their birthday already this year. If the users birthday is after the current date, the age is off by 1 year.

    ReplyDelete
  2. Datediff function worked perfectly for me.

    ReplyDelete
  3. Does not work.......

    ReplyDelete

  4. it will work...it will show in negative valueas a age in months...

    Select BirthDate, DateDiff(month, BirthDate, getdate()) as Ageinmonths from HumanResources.Employee

    http://www.w3schools.com/sql/func_datediff.asp

    ReplyDelete