Question

So I see this question has a good answer, but I want to round up no matter what, instead of rounding down no matter what. Adding 1 to it before casting int wouldn't work because it would "round" 5.0 into 6.0, for example.

So how should I implement ceil in SQLite?

Was it helpful?

Solution

How about this?

select (case when x = cast(x as int) then cast(x as int)
             else 1 + cast(x as int)
        end)

OTHER TIPS

This will give you the same answer more elegantly:

SELECT CAST(x+1-1e-n AS INT);

(assuming you won't have a precision greater than n decimal points)

using php you can easily:

$db->createFunction('ceil', 'ceil', 1);

$db->createFunction('floor', 'floor', 1);

select ceil(\`column`) from table;

Some useful answers, I found this solution really easy as well.

select round(column+0.5) from table;

It will mean you are always rounding up if your original number is < 0.5 or rounding back down if your original number is > 0.5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top