Round up value to nearest whole number in SQL UPDATE
Solution 1
You could use the ceiling
function; this portion of SQL code :
select ceiling(45.01), ceiling(45.49), ceiling(45.99);
will get you "46" each time.
For your update, so, I'd say :
Update product SET price = ceiling(45.01)
BTW : On MySQL, ceil
is an alias to ceiling
; not sure about other DB systems, so you might have to use one or the other, depending on the DB you are using...
Quoting the documentation :
CEILING(X)
Returns the smallest integer value not less than X.
And the given example :
mysql> SELECT CEILING(1.23);
-> 2
mysql> SELECT CEILING(-1.23);
-> -1
Solution 2
Try ceiling...
SELECT Ceiling(45.01), Ceiling(45.49), Ceiling(45.99)
http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
Solution 3
For MS SQL CEILING(your number) will round it up. FLOOR(your number) will round it down
Solution 4
Combine round and ceiling to get a proper round up.
select ceiling(round(984.375000), 0)) => 984
while
select round(984.375000, 0) => 984.000000
and
select ceil (984.375000) => 985
Solution 5
Ceiling is the command you want to use.
Unlike Round, Ceiling only takes one parameter (the value you wish to round up), therefore if you want to round to a decimal place, you will need to multiply the number by that many decimal places first and divide afterwards.
Example.
I want to round up 1.2345 to 2 decimal places.
CEILING(1.2345*100)/100 AS Cost
Related videos on Youtube
user2120901
Updated on July 09, 2022Comments
-
user2120901 almost 2 years
I'm running SQL that needs rounding up the value to the nearest whole number.
What I need is 45.01 rounds up to 46. Also 45.49 rounds to 46. And 45.99 rounds up to 46, too. I want everything up one whole digit.
How do I achieve this in an UPDATE statement like the following?
Update product SET price=Round
-
kliron over 14 yearswelcome to stackoverflow! Anything indented four spaces is formatted as code, you can use the button with binary digits on it to do this in the editor. Hope you don't mind me fixing your answer to do this!
-
datagod over 12 yearsWhat if you just want to round up to the nearest penny? If the tax due is $13.052, I need to round that up to $13.053. I could use ceiling(), but I would have to first multiple by 100, then divide by 100. Seems lame.