0

What is the best way to calculate age based on an event? I'm trying to look for individuals based on their age when an event took place? I'm relative new to SQL and the dates functions, I can't wrap my head around.

Example:

ID        Name         Age   DOB          AdmitDate
1234      John Smith   61    1960-07-15   2019-10-22

How can I can calculate whose age was 59 at the time of admit date?

Leonardo
  • 71
  • 1
  • 13
  • For accuracy, I recommend this https://stackoverflow.com/a/1572938/9947159 – Radagast Mar 09 '20 at 17:13
  • SELECT DATEDIFF(DAY, [DOB], AdmitDate) / 365 You can use this as a derived column, put into a temp table, and from that temp table, add your WHERE clause to find people who are 59. – jw11432 Mar 09 '20 at 18:09

2 Answers2

1

DATEDIFF provides the function to do this.

e.g.

SELECT DATEDIFF ( yy , '1960-07-15' , '2019-10-22' ) 
Eric Brandt
  • 7,886
  • 3
  • 18
  • 35
Peter Schofield
  • 939
  • 7
  • 13
0
Select * from  yourtable 
where  DATEDIFF( year , CAST(DOB AS datetime), CAST(AdmitDate AS datetime)) >= 59

Try this

Vijay
  • 137
  • 13