Avatar of frimy
frimy
Flag 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
Microsoft OfficeMicrosoft ExcelSQL

Avatar of undefined
Last Comment
frimy

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

Please try this

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

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

(2 row(s) affected)

Open in new window

Pawan Kumar

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

David Johnson, CD

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)
Your help has saved me hundreds of hours of internet surfing.
fblack61
Pawan Kumar

@Author - did u tried my suggestions, both are giving exactly same output you need. Thanks
frimy

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