Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi my name is Jeff L and I havea 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;
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 thatKH[spoiler]Time is always against us[/spoiler]