Search code examples
sqldatabasesql-server-2008floorceil

CEILING returns FLOOR result - SQL SERVER 2008 R2


DECLARE @a int;
DECLARE @b int;

SET @a = 9;
SET @b = 2;

SELECT CEILING (@a/@b);

It is returning as 4 instead of 5. Why?

Edit: I would like to get next smallest integer if the quotient is not whole number.


Solution

  • Try:

    SELECT CEILING (@a/CAST(@b AS float))
    

    And consider NULLIF(@b,0) too, to avoid a Division By Zero error.