Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding-up TSQL

I gotta be missing something obvious.

select CEILING(85/30) = 2

85/30 = 2.83333

I want the value to be 3.

Shouldn't the CEILING function round up for me?

like image 426
John Doe Avatar asked Feb 26 '13 19:02

John Doe


2 Answers

Try

SELECT CEILING(85.0/30)

And for comparison

SELECT 85.0 / 30, 85 / 30

The first example uses floats, the second uses ints, so the result is rounded before the ceiling function is hit. What you do is

SELECT CEILING(2) 

Rather than

SELECT CEILING(2.833333)
like image 95
faester Avatar answered Sep 21 '22 14:09

faester


Change it for :

select CEILING(85/30.0)

INT / INT yields an INT, so 85/30 rounds it down (FLOOR).

like image 25
Dominic Goulet Avatar answered Sep 21 '22 14:09

Dominic Goulet