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
 Working out Years diff

Author  Topic 

Robowski
Posting Yak Master

101 Posts

Posted - 2013-10-01 : 07:24:36
Just wondering, what you all consider to be the most efficient way for working out years? for example:

SELECT DATEDIFF(Year, CAST('01/02/1985' as datetime), CAST('01/01/1995' as datetime)) returns 10 this isn't actually correct in terms of how old that person is

SELECT DATEDIFF(Year, CAST('01/02/1985' as datetime), CAST('01/01/1995' as datetime)) / 12 returns 9 which is correct if you purely want to know the year but haven't looked at this over large amounts of data.

SELECT DATEDIFF(day, CAST('01/02/1985' as datetime), CAST('01/01/1995' as datetime)) / 365.4 gives 9.909688

without doing some testing about different points of the year etc, does has one looked into this?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-01 : 08:57:52
[code]SELECT ( CAST(CONVERT(CHAR(8), GETDATE(), 112) AS INT)
- CAST(CONVERT(CHAR(8), DateOfBirth, 112) AS INT) ) / 10000
[/code]
Go to Top of Page
   

- Advertisement -