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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Age Calculation Help

Author  Topic 

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2014-07-14 : 16:52:22
I'm trying to get an accurate age calculation. The way I have it now seems to only calculate the year.

If I enter 7-13-2014 as the date of birth I get age 59 ...correct.

If I enter 7-15-2014 (which is tomorrow as I type this) the age remains 59 and it should be 58

Here is the calculation I am currently using

(datediff(day,[DOB],getdate())/(365))

What is the correct calculation??







Mike Brown

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-07-14 : 16:58:49
See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=181713 When you calculate age in order for it to work correctly in all cases, you have to account for things like leap years, February 29 etc. The formulas/code in that thread accounts for all of those.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-07-14 : 22:15:29
look for "Finding Age" in this thread
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762&SearchTerms=Finding%20Age


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

Go to Top of Page
   

- Advertisement -