Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$

select FineAmount from tickets

Thanks

like image 245
acadia Avatar asked Nov 09 '09 16:11

acadia


People also ask

How do you round to the nearest 5 in SQL?

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

How do you round to the nearest value in SQL?

Decimal data type value with positive LengthSELECT ROUND(@value, 1); SELECT ROUND(@value, 2); SELECT ROUND(@value, 3); In this example, we can see that with decimal values round up to the nearest value as per the length.

How do I limit decimal places in SQL Server?

The ROUND() function rounds a number to a specified number of decimal places. Tip: Also look at the FLOOR() and CEILING() functions.


2 Answers

select round(FineAmount*2,-1)/2 from tickets

or to put nicholaides suggestion in sql

select round(FineAmount/5,0)*5 from tickets

The example assumes that FineAmount is of type money. The second approach is probably better as the first one works with the limit of maximum_value_of_money_type/2

More on ROUND

like image 117
kristof Avatar answered Sep 19 '22 15:09

kristof


A general math solution:

Divide by 5, round to the nearest integer, then multiply by 5.

like image 38
nicholaides Avatar answered Sep 17 '22 15:09

nicholaides