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


Rounding up to fifth decimal


Rounding up to fifth decimal

Author
Message
tarr94
tarr94
SSC-Addicted
SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)

Group: General Forum Members
Points: 424 Visits: 222
Hi,

I have a field that stores 8 decimals, but I want to select this field in a query with the decimals rounded up to the fifth. The rounding up should always occur, even if the number is below five. For example:

83.00381433 rounds up to 83.00382000

I've played around with the CEILING function, but I'm not sure how to make it apply to the fifth decimal place.

Any help would be greatly appreciated!

Thank you.
Doug J.
Doug J.
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 942
Interesting question. Try putting together a CASE statement using the ROUND() function. Something like this:


declare @num numeric(10, 8) = 83.00381433 ;
SELECT CASE
WHEN round(@num, 5) - @Num < 0 THEN round(@num, 5) + .00001
ELSE round(@num, 5) END;



I'm not sure if this is the best way to do it, but it should work

Edit: corrected boneheaded arithmetic error.

-Doug



http://xkcd.com/327/
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95091 Visits: 21186
Maybe you didn't play enough with the ceiling function. ;-)

DECLARE @num numeric(10, 8) = 83.00381433 ;
SELECT CEILING( @num * 100000) / 100000;




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
tarr94
tarr94
SSC-Addicted
SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)

Group: General Forum Members
Points: 424 Visits: 222
Thank you both for responding. I tried the solution from Luis first (it was the simpler of the two) and found that it works like a charm. Guess I gave up on the Ceiling function too quickly.
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