SQL CEIL function
Returns the smallest integer not less than the input. Its common job is page counts: CEIL(total_rows * 1.0 / page_size).
- Kategorie
- Math
- Returns
integer / numeric- In this category
- 3 of 82
Syntax
CEIL(number)
CEILING(number) -- SQL Server
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Different name or syntax | spelled CEILING only |
| PostgreSQL | Ja | Supported under this name. |
| MySQL | Ja | Supported under this name. |
| SQLite | Ja | CEIL and CEILING, math functions from 3.35+ |
| Oracle | Ja | CEIL only |
Beispiele
PostgreSQL
-- CEILING(2.1) on SQL Server SELECT CEIL(2.1) AS up; -- 3
SQL Server
SELECT CEILING(CAST(total_rows AS DECIMAL(18, 2)) / page_size) AS pages FROM stats;
Gut zu wissen
- Spelling: CEILING only on SQL Server, CEIL only on Oracle, both on PostgreSQL, MySQL and SQLite.
- CEIL rounds toward positive infinity: CEIL(-2.1) is -2.
- Integer division happens before CEIL: on SQL Server, PostgreSQL and SQLite, CEIL(7/2) is CEIL(3) = 3, not 4. MySQL's / returns 3.5, so it gives 4 there. Make one operand a decimal first.
Questions about CEIL / CEILING
Why does CEIL(7/2) return 3 instead of 4?
On SQL Server, PostgreSQL and SQLite, 7/2 is integer division and already truncated to 3 before CEIL sees it. Make one operand a decimal: CEIL(7.0 / 2) or CEIL(CAST(a AS DECIMAL) / b). MySQL returns 4, because its / is decimal division.
Is it CEIL or CEILING?
Both exist on PostgreSQL, MySQL and SQLite. SQL Server only has CEILING; Oracle only has CEIL.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.