SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Best Way to Calculate Age


Best Way to Calculate Age

Author
Message
Jimi Meyer
Jimi Meyer
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1534 Visits: 766
Only the second query is getting the right answer. #1 and #3 are getting +1 to the my current age.
Tommy Bollhofer
Tommy Bollhofer
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1768 Visits: 3359
The third solution is clearly incorrect - given a datetime value of 01/26/1978.

i.e.


declare @DateOfBirth datetime
set @DateOfBirth = '1978-01-26 00:00:00.000'

select @DateOfBirth

select DATEDIFF(yy, @DateOfBirth, GETDATE())

select FLOOR(CONVERT(decimal(9, 2), DATEDIFF(d, @DateOfBirth, GETDATE())) / 365.0)

select DATEDIFF(yy, @DateOfBirth, GETDATE()) - CASE WHEN DATEPART(m, @DateOfBirth) >= DATEPART(m, GETDATE()) AND DATEPART(d, @DateOfBirth) >= DATEPART(d, GETDATE()) THEN 0 ELSE 1 END



Returns:

30 - Correct

30 - Correct

29 - Incorrect

Tommy

Follow @sqlscribe
Peter Brinkhaus
Peter Brinkhaus
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2022 Visits: 7367
I intuitively choose 2 because 1 and 3 are definitely wrong. 1 doesn't take care of month and day of birth at all and 3 does it in the wrong way. But the are all wrong. It can be easily verified with the script below. Play around with some values at the beginning and end of the month in both dates. I added a fourth solution which I think is correct.

declare @DateOfBirth datetime
declare @Today datetime

select @DateOfBirth = '2004/03/01'
select @Today = '2008/02/29'

select
DATEDIFF(yy, @DateOfBirth, @today)
select
FLOOR(CONVERT(decimal(9, 2), DATEDIFF(d, @DateOfBirth, @Today)) / 365.0)
select
DATEDIFF(yy, @DateOfBirth, @today)
- CASE
WHEN DATEPART(m, @DateOfBirth) >= DATEPART(m, @Today) AND DATEPART(d, @DateOfBirth) >= DATEPART(d, @Today) THEN
0
ELSE
1
END
select
DATEDIFF(yy, @DateOfBirth, @today)
- CASE
WHEN DATEPART(m, @Today) > DATEPART(m, @DateOfBirth)
OR (DATEPART(m, @Today) = DATEPART(m, @DateOfBirth)
AND DATEPART(d, @Today) >= DATEPART(d, @DateOfBirth)) THEN
0
ELSE
1
END


StarNamer
StarNamer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1638 Visits: 1992
Of course, I suppose the real point of the question was to draw attention to the fact that (1) was wrong and (2) was inaccurate. But it would have been helpful to have a correct solution in the mix. Smile

Derek
Steven Cameron
Steven Cameron
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2087 Visits: 215
I originally thought the 1 and 0 were switched but actually, the two comparisons should be >=
Patrick Cahill
Patrick Cahill
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3806 Visits: 993
I also ran all three queries with my birthdate, and the first and third returned the wrong answer. However, the second one was correct.
andre.deslauriers
andre.deslauriers
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 44
That one works flawlessly.

DECLARE @DateOfBirth VARCHAR(10)
SET @DateOfBirth = '1960-03-07'

print YEAR(GETDATE()) - YEAR(@DateOfBirth) - CASE WHEN MONTH(GETDATE())*31 + DAY(GETDATE()) >= MONTH(@DateOfBirth)*31 + DAY(@DateOfBirth) THEN 0 ELSE 1 END
Q -631159
Q -631159
SSC Veteran
SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)

Group: General Forum Members
Points: 281 Visits: 165
The formula is incorrect. If your birthday has occurred in the calendar year, the formula below returns 1 year less than it should. I know many people that would be happy if the formula was correct, but I am afraid they really are the age that DATEDIFF(yy, DateOfBirth, GETDATE()) gives.

DATEDIFF(yy, DateOfBirth, GETDATE()) - CASE WHEN DATEPART(m, DateOfBirth) >= DATEPART(m, GETDATE()) AND DATEPART(d, DateOfBirth) >= DATEPART(d, GETDATE()) THEN 0 ELSE 1 END

Q

Please take a number. Now serving emergency 1,203,894
Simon Facer
Simon Facer
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1727 Visits: 724
Another point - the question specifies TINYINT, none of the queries return TINYINT. (1) is INT, (2) is DECIMAL, (3) is INT. Not only are the queries inaccurate, but none of them fit the criteria ... Crazy



jgaroutte
jgaroutte
SSC Veteran
SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)SSC Veteran (228 reputation)

Group: General Forum Members
Points: 228 Visits: 61
sorry the 3rd answer is not 100% correct as written

DECLARE @DateOfBirth DATETIME
SET @DateOfBirth='08/xx/1975' --commented out the day just because

SELECT DATEDIFF(yy, @DateOfBirth, GETDATE()) AS [first],
FLOOR(CONVERT(decimal(9, 2), DATEDIFF(d, @DateOfBirth, GETDATE())) / 365.0) AS [second],
DATEDIFF(yy, @DateOfBirth, GETDATE()) - CASE WHEN DATEPART(m, @DateOfBirth) >= DATEPART(m, GETDATE()) AND DATEPART(d, @DateOfBirth) >= DATEPART(d, GETDATE()) THEN 0 ELSE 1 END AS [third]

the result when it runs
first second third
33 32 33

Don't age me before my time, only the second one was correct.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search