Link to home
Start Free TrialLog in
Avatar of frimy
frimyFlag for United States of America

asked on

SQL, round to the nearest whole number

Hello all,

How to round to the nearest whole number in SQL
e.i.
.03   = 1
1.03 = 2
if there is a remaining i want to add 1.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please try this

/*------------------------
SELECT CEILING(.03) 
UNION ALL
SELECT CEILING(1.03) 
------------------------*/

---------------------------------------
1
2

(2 row(s) affected)

Open in new window

Another solution for you.

/*------------------------

SELECT CASE WHEN CAST(a AS FLOAT) LIKE '%.%' THEN CAST(1 + a AS INT) ELSE a END 
FROM
(
	SELECT (.03) a
	UNION ALL
	SELECT (1.03) 
	UNION ALL
	SELECT 2 
	UNION ALL
	SELECT 5 
)r
------------------------*/

---------------------------------------
1.00
2.00
2.00
5.00

(4 row(s) affected)

Open in new window

Actually strictly speaking 1.03 rounded to the nearest whole number is 1 not 2. For this one usually uses bankers rounding
int (value + .45)
@Author - did u tried my suggestions, both are giving exactly same output you need. Thanks
Avatar of frimy

ASKER

Thanks
I wasn't sure what ceiling does.
it works fine
have a nice day!