October 2, 2009 at 4:00 am
Hello. I use SQL Server 2005 and I'm calculating the age of patients in the following manner:
DATEDIFF(DAY, birthdate, operationdate)/365.25
The output always seems to be a decimal with 6 decimal digits. The rounding problem arises with border cases like for instance an age of 60.444445. If you round this properly it should result in 60.45 and that should result in 60.5. However, when I use [font="Courier New"]ROUND(60.444445, 1)[/font] it results in 60.4!
The problem is that SQL Server seems to ignore the digits after the rounding border. [font="Courier New"]ROUND(60.449999, 1)[/font] for instance also results in 60.4. It seems to truncate it 1 position behind the rounding index and starts rounding after that.
I found out that using the ROUND() function repeatedly in the following manner solves the issue but it is a bit ridiculous:
PRINT ROUND(ROUND(ROUND(ROUND(ROUND(60.444445, 5), 4), 3), 2), 1)
Is there any other way to solve this and am I missing something?
Thanks in advance!
October 2, 2009 at 4:16 am
Heres how to calculate an age
select CASE
WHEN dateadd(year, datediff (year, d1, d2), d1) > d2
THEN datediff (year, d1, d2) - 1
ELSE datediff (year, d1, d2)
END as Age
I'll take a look at the rounding issue though...
October 2, 2009 at 4:33 am
I think you are wrong in your assessment of round though,
round(60.444445,1) = 60.4
This
PRINT ROUND(ROUND(ROUND(ROUND(ROUND(60.444445, 5), 4), 3), 2), 1)
works because you are rounding a rounded , rounded, rounded , rounded number (ie the 5 has for want of a better word been cascaded up)
October 2, 2009 at 6:40 am
For some reason that way of rounding was in my head. But I spoke with others about and I think SQL Server does round it correctly yes.
October 2, 2009 at 7:38 pm
Dave Ballantyne (10/2/2009)
Heres how to calculate an age
select CASE
WHEN dateadd(year, datediff (year, d1, d2), d1) > d2
THEN datediff (year, d1, d2) - 1
ELSE datediff (year, d1, d2)
END as Age
I'll take a look at the rounding issue though...
That's one of the simplest I've seen. Correctly handles the Feb 29 problem that most other's don't handle, too!
--Jeff Moden
Change is inevitable... Change for the better is not.
October 5, 2009 at 2:34 am
Cant take the credit for it ...
http://windowsitpro.com/article/articleid/14267/how-can-i-calculate-someones-age-in-sql-server.html
October 5, 2009 at 5:58 am
Shoot... the link is broken. Thanks anyway, Dave.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 5, 2009 at 7:05 am
Seems fine now, just a temporary thing i guess.
TBH , its hardly worth the click though 🙂 simple Q and A
October 5, 2009 at 7:08 am
Hmmmm.... something must be wrong on my end. It keeps giving me a 404 error.
Q & A's are sometimes pretty rich in information like the little date jewel you found.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2016 at 2:24 pm
DECLARE @test-2 TABLE (
EmpName VARCHAR(40) ,
BirthDate DATE
);
INSERT INTO @test-2 ( EmpName , BirthDate
)
VALUES ( '30 Yrs old yesterday' , DATEADD(day , -1 , DATEADD(year , -30 , GETDATE()))
) , ( '30 Yrs old today' , DATEADD(year , -30 , GETDATE())
) , ( '30 Yrs old tomorrow' , DATEADD(day , 1 , DATEADD(year , -30 , GETDATE()))
);
SELECT EmpName , BirthDate ,
CASE
WHEN DATEADD(YY , DATEDIFF(yy , BirthDate , GETDATE()) , BirthDate) < GETDATE()
THEN DATEDIFF(yy , BirthDate , GETDATE())
ELSE DATEDIFF(yy , BirthDate , GETDATE()) - 1
END AS Age
FROM @test-2;
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply