SQL Server calculate the exact age on the day

Hi, I have a date field in MS SQL Server with the name "mDOB" with the datatype "datetime" applied to it.

this dateofbirth is  in that field "05.09.1973"

I need to calculate the exact age and get it into flash.

This is what I do, it works but it misses on up to 10 days, and that is not acceptable.

Colud you please help with a solution?

Response.write "mAge=" & (DateDiff("yyyy", objRS("mDOB"), Now()))& "&"
oksum73Asked:
Who is Participating?
 
nguyenvinhtuConnect With a Mentor Commented:
Your result fail to pleasure you maybe because your Date and the Date today are not in the same format.
To make sure the two date is same format:
Response.write "mAge="
& (DateDiff("yyyy", DateFormat(objRS("mDOB"),"mm/dd/yyyy"), DateFormat(Now(),"mm/dd/yyyy"))) & "&"
0
 
nguyenvinhtuCommented:
Why missed 10 days? We just simply subtract this year to birth year, then we have the result !!! Can you explain in detail?
0
 
Arthur_WoodCommented:
when you say 'te exact age', do you mean X years, Y months and Z days as the age , or do you simply want the age in Years?

DateDiff("yyyy".......) will simply return the difference between the two dates, in TOTAL YEARS, and at that it will be WRONG before that anniversay of your birth.

My birthday is 10/17/1944 making me 61 years 6 months and 11 days old at the moment, but

select DateDiff(year, '10/17/1944', getdate()) returns 62

however if you want the precise age, in Years months and Days, you need to do some calculations in the SQL, like this:

select cast(DateDiff(day, mDOB, getdate())/365.245 as int) as years ,cast((DateDiff(day, mDOB, getdate())/365.245 - cast(DateDiff(day, mDOB, getdate())/365.245 as int)) * 12 as int) as months,
cast((((DateDiff(day, mDOB, getdate())/365.245 - cast(DateDiff(day, mDOB, getdate())/365.245 as int)) * 12) - cast((DateDiff(day, mDOB, getdate())/365.245 - cast(DateDiff(day,mDOB, getdate())/365.245 as int)) * 12 as int))* 30 as int) as days  

this will return the correct number of Years, months and days as the 'exact' age.

AW
0
 
kenpemCommented:
Or you could just

SELECT CAST(GETDATE()-'28-Nov-2005' AS INT)

to get the age as a number of DAYS, and do what you like from there!
0
 
kenpemCommented:
or

SELECT CAST(GETDATE()-'28-Nov-2005' AS DECIMAL) /365.25

if you want it in years
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.