Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round up value to nearest whole number in SQL UPDATE

Tags:

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 
like image 385
Skuta Avatar asked Sep 07 '09 11:09

Skuta


People also ask

How do you round to the nearest whole number in SQL?

The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round off, it rounds off the number to the nearest integer.

Does round in SQL round up or down?

Bookmark this question. Show activity on this post. Knowing that round(10.823, 2) only rounds down.

How do you change a decimal to a whole number in SQL?

Using ROUND() function: This function in SQL Server is used to round off a specified number to a specified decimal places. Using FLOOR() function: It returns the largest integer value that is less than or equal to a number.


1 Answers

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 
like image 129
Pascal MARTIN Avatar answered Oct 16 '22 04:10

Pascal MARTIN