Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ROUNDDOWN in sqlserver

I want to use ROUNDDOWN function.

When i tried using the following query,it gives me an error saying "'rounddown' is not a recognized built-in function name."

select rounddown(25.227,2)

My requirement is to rounddown the value to two decimals

for ex: for value 25.22789 result should be 25.22

and round up also

for ex: for value 25.22789 result should be 25.23

Any help?

Thanks in advance

like image 602
Santosh Avatar asked Sep 02 '15 12:09

Santosh


People also ask

How do you ROUND down in SQL Server?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places.

How do I ROUND up in SQL?

In Oracle, MySQL, and PostgreSQL, you can use either the CEIL() or CEILING() function to round up.

How do you ROUND to the nearest .5 in SQL?

A general math solution: Divide by 5, round to the nearest integer, then multiply by 5.

How do you ROUND a number to the nearest 1000 in SQL?

Notice that we need a complex expression involving CEILING, in order to get the "rounded up to next 1000" number that you wanted. The "trick" (if you want to call it that) is to divide by 1000.0, which forces a decimal result, before applying the CEILING.


2 Answers

Use third parameter of ROUND() function to truncate and then CONVERT() it to DECIMAL(x, 2) to get rid of unwanted trailing zeros.

Fiddle demo

SELECT CONVERT(DECIMAL(10,2), ROUND(25.227, 2, 1)) RoundDown,
        CONVERT(DECIMAL(10,2), ROUND(25.227, 2, 0)) RoundUp

Results

| RoundDown | RoundUp |
|-----------|---------|
|     25.22 |   25.23 |
like image 54
Kaf Avatar answered Sep 19 '22 14:09

Kaf


I think you are looking for either CEILING() or floor() function like

select CEILING(25.227) //results in 26

(OR)

select FLOOR(25.227) //Results in 25

EDIT:

for ex: for value 25.22789 result should be 25.22

You can try like below

select round(25.22789, 2, 2)

Which will result in 25.22000

like image 40
Rahul Avatar answered Sep 19 '22 14:09

Rahul