Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 age query newbie

Author  Topic 

jeff357
Starting Member

4 Posts

Posted - 2011-06-26 : 19:56:52
Hi my name is Jeff L and I have
a query that should work but dosent here goes...

SELECT CAST(DATEDIFF(hh, [Date of Birth], GETDATE()) / 8766 AS int) AS Age
SQL SERVER EXPRESS Help
TIA!
Jeff

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-26 : 21:38:36
Leap years etc. make that work not quite right, I guess. An easy way to calculate the age is to express the date of birth as an integer in the YYYYMMDD form and then take the difference and divide by 10000. Works in every case.
SELECT (CAST(CONVERT(CHAR(8), GETDATE(), 112) AS INT)-CAST(CONVERT(CHAR(8), [Date of Birth], 112) AS INT))/10000;
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-06-26 : 21:43:20
if you are only interested in the age in year, datediff(year, [Date of Birth], getdate()) will give you that


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -