Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

ROUND() function doesn't round properly Expand / Collapse
Author
Message
Posted Friday, October 02, 2009 4:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 17, 2012 4:39 AM
Points: 4, Visits: 17
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 ROUND(60.444445, 1) it results in 60.4!

The problem is that SQL Server seems to ignore the digits after the rounding border. ROUND(60.449999, 1) 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!
Post #796803
Posted Friday, October 02, 2009 4:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 7:26 AM
Points: 1,949, Visits: 8,291
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...




Clear Sky SQL
My Blog
Kent user group
Post #796805
Posted Friday, October 02, 2009 4:33 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 7:26 AM
Points: 1,949, Visits: 8,291
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)




Clear Sky SQL
My Blog
Kent user group
Post #796810
Posted Friday, October 02, 2009 6:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, February 17, 2012 4:39 AM
Points: 4, Visits: 17
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.
Post #796835
Posted Friday, October 02, 2009 7:38 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #797330
Posted Monday, October 05, 2009 2:34 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 7:26 AM
Points: 1,949, Visits: 8,291
Cant take the credit for it ...


http://windowsitpro.com/article/articleid/14267/how-can-i-calculate-someones-age-in-sql-server.html




Clear Sky SQL
My Blog
Kent user group
Post #797711
Posted Monday, October 05, 2009 5:58 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
Shoot... the link is broken. Thanks anyway, Dave.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #797801
Posted Monday, October 05, 2009 7:05 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 7:26 AM
Points: 1,949, Visits: 8,291
Seems fine now, just a temporary thing i guess.

TBH , its hardly worth the click though simple Q and A




Clear Sky SQL
My Blog
Kent user group
Post #797849
Posted Monday, October 05, 2009 7:08 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:47 PM
Points: 35,959, Visits: 30,252
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
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #797855
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse