// math reference

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).

類別
Math
Returns
integer / numeric
In this category
3 of 82

語法

CEIL(number)
CEILING(number)  -- SQL Server

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支援

引擎Support備註
SQL Server Different name or syntax spelled CEILING only
PostgreSQL 是 Supported under this name.
MySQL 是 Supported under this name.
SQLite 是 CEIL and CEILING, math functions from 3.35+
Oracle 是 CEIL only

範例

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;

小知識

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.