SQL CEILING function
Rounds a number up to the nearest integer, toward positive infinity: CEILING(2.1) is 3 and CEILING(-2.1) is -2.
- 类别
- Math
- Returns
same type as the input- In this category
- 11 of 82
语法
CEILING(number)
CEIL(number)
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | Only CEILING; CEIL is not a T-SQL function |
| PostgreSQL | 是 | CEIL and CEILING are both built in |
| MySQL | 是 | CEIL and CEILING are both built in |
| SQLite | 是 | Requires SQLite 3.35+ with the math functions built in; both spellings exist and return REAL for REAL input |
| Oracle | 是 | Only CEIL; there is no CEILING keyword |
示例
SQL Server
SELECT CEILING(2.1) AS up, CEILING(-2.1) AS neg_up; -- 3, -2
Oracle
SELECT CEIL(2.1) AS up, CEIL(-2.1) AS neg_up FROM dual; -- 3, -2
PostgreSQL
SELECT CEIL(total_pages / 500.0) AS page_batches FROM print_jobs;
SQL Server
SELECT CEILING(disk_gb / 100.0) AS billed_units FROM usage;
值得了解
- CEILING moves toward positive infinity, so CEILING(-2.1) is -2, not -3. FLOOR(-2.1) is -3.
- The result keeps the input's type family: CEILING of a decimal is a decimal with scale 0, CEILING of a float is a float. SQLite returns 3.0, not 3, for REAL input.
- Integer division eats the rounding: CEILING(7/2) on integer columns is CEILING(3) = 3, not 4. Compute CEILING(7/2.0).
Questions about CEILING
Is it CEIL or CEILING?
Both names work on PostgreSQL, MySQL and SQLite. SQL Server only accepts CEILING and Oracle only accepts CEIL, so no single spelling runs on all five.
Why does CEILING(7/2) return 3 instead of 4?
7/2 is integer division and evaluates to 3 before CEILING sees it. Force decimal division with 7/2.0, or CAST one operand, then CEILING gets 3.5 and returns 4.
What does CEILING return for a negative number?
The next integer toward positive infinity: CEILING(-2.1) is -2. If you want -3 you are thinking of FLOOR.
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.