0

I have a table that stores the date of birth of patients in DateTime format(2022-06-22).

How can I get the age in years for people born in July?

I have tried the below:

select sFirstName,
sLastName,
dDateOfBirth,
DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
ifkMedicalAidID
from Patients
where DATEDIFF(month,dDateOfBirth,GETDATE()) between 5 and 5

Although I get only all the records for July(which is correct) all the ages are 0, I see the problem with the above query but I don't have the SQL knowledge to resolve it...

Kyle
  • 51
  • 9

2 Answers2

0
SELECT 
FLOOR((DATEDIFF(DAY,0,GETDATE()) - DATEDIFF(DAY,0,dDateOfBirth)) / 365.2425) AS AgeYears
planetmatt
  • 407
  • 3
  • 10
-2

this did the trick thanks John

select sFirstName,
    sLastName,
    dDateOfBirth,
    DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
    ifkMedicalAidID
    from Patients
    where   datepart(month,dDateOfBirth)=7
Kyle
  • 51
  • 9
  • 1
    `DATEDIFF(yy,dDateOfBirth,GETDATE()` doesn't give you someone's age. Someone born on `2021-12-31` is not `1` year old today. People age on their birthday, not when the year changes. – Thom A Nov 09 '22 at 15:29