SQL: Calculate Age Based on DOB

I intend to live forever. So far so good.

SQL: Calculate Age Based on DOB

Postby ShiftPoint » Thu Jul 16, 2009 11:32 am

I had to come up with a sql query that would calculate a DOB based on a datetime field. This is what I came up with.

Code: Select all
select year(getdate())
     - year(DOB)
     - case when month(DOB) > month(getdate()) then 1
            when month(DOB) < month(getdate()) then 0
            when day(DOB) > day(DOB) then 1
            else 0
        end as age
  from HR
User avatar
ShiftPoint
 
Posts: 70
Joined: Sun Jul 05, 2009 4:52 pm
Location: Pickerington, OH

Return to Database ( MSSQL, MYSQL, Oracle)

Who is online

Users browsing this forum: No registered users and 0 guests

cron