Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the ceil value of a number in SQLite

Tags:

sql

sqlite

ceil

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?

like image 524
wrongusername Avatar asked Feb 19 '13 22:02

wrongusername


People also ask

How do I get Ceil in SQL?

The CEIL() function returns the nearest smallest integer that is greater than or equal to a number. Some database servers use the CEILING() function, which is equivalent to the CEIL() function. Figure 1 shows the mathematical representation of the CEIL() function.

What does the Ceil () function do?

The ceil() function computes the smallest integer that is greater than or equal to x.

How do you calculate mode in sqlite?

To compute the mode, group by the browser column, get the COUNT(*) for each group, sort by that value, and take the record with the largest value. CL. Save this answer.

What is the difference between Ceil and round in SQL?

CEILING is operation, which return the smallest integer greater than passed number, so it rounds up to next integer. CONLUSION: So basic difference: CEILING rounds up, while ROUND rounds number in standard way. Another key difference is that ROUND let's you specify number of decimal places you want round to.


2 Answers

How about this?

select (case when x = cast(x as int) then cast(x as int)
             else 1 + cast(x as int)
        end)
like image 195
Gordon Linoff Avatar answered Sep 27 '22 20:09

Gordon Linoff


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)

like image 28
DomingoR Avatar answered Sep 27 '22 20:09

DomingoR