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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 ROUNDING and taking the upper limit

Author  Topic 

jayram11
Yak Posting Veteran

97 Posts

Posted - 2011-01-03 : 14:58:42
Hi
i have a field which is float and some values have upto 4 decimal places. i want to round it and use the next upper limit

eg:) if it is 1.9015, i need to round it to 1.092
if it is 1.0905, i need to round it to 1.091

if it is upto 3 decimal places, leave it as its

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-03 : 15:42:52
DECLARE @SomeValue FLOAT
SET @SomeValue = 1.9015

SELECT CEILING(@SomeValue*1000)/1000

Not tested, but should give you the idea.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-01-03 : 16:19:03
SELECT ROUND(@SomeValue, 3) also works.
Go to Top of Page

jayram11
Yak Posting Veteran

97 Posts

Posted - 2011-01-03 : 16:58:55
Hello Robvolk

i initially tried SELECT ROUND(@SomeValue, 3)

but it makes 1.0915 into 1.091 but i needed 1.092 and GilaMonster's reply works.

Thanks

Jay
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-01-03 : 17:03:59
Ooops, forgot about the banker's rounding algorithm.
Go to Top of Page

BruceT
Yak Posting Veteran

78 Posts

Posted - 2011-01-04 : 09:23:01
This probably would work also without requiring the multiplication and division.

DECLARE @SomeValue FLOAT
SET @SomeValue = 1.0915
SELECT ROUND(convert(numeric(18,4),@somevalue), 3)

result = 1.0920
Go to Top of Page
   

- Advertisement -