SQL FLOOR function
Returns the largest integer not greater than the input. FLOOR(-2.5) is -3, not -2: rounding down means toward negative infinity, which is where most off-by-one bugs come from.
- Категория
- Math
- Returns
integer / numeric- In this category
- 2 of 82
Синтаксис
FLOOR(number)
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | Supported under this name. |
| PostgreSQL | Да | Supported under this name. |
| MySQL | Да | Supported under this name. |
| SQLite | Да | math functions from SQLite 3.35+, enabled in standard builds |
| Oracle | Да | Supported under this name. |
Примеры
Any engine
SELECT FLOOR(2.9) AS down; -- 2
Any engine
SELECT FLOOR(-2.1) AS down; -- -3
Полезно знать
- FLOOR rounds toward negative infinity: FLOOR(-2.5) is -3. To drop the decimals toward zero, use TRUNC (or CAST to an integer type).
- Integer division happens first and truncates toward zero on SQL Server, PostgreSQL and SQLite: -7/2 is -3, so FLOOR(-7/2) is -3, not the -4 that FLOOR(-3.5) gives. Write FLOOR(-7 / 2.0). MySQL's / returns a decimal (7/2 is 3.5); DIV is its integer division.
- The result keeps the input's type on SQL Server and PostgreSQL: FLOOR(2.5) is a numeric 2, FLOOR of a float is a float. SQLite returns a real for real input; MySQL returns an integer for exact numeric input.
Questions about FLOOR
What is FLOOR of a negative number?
The next lower integer: FLOOR(-2.1) is -3. It rounds toward negative infinity, unlike TRUNC which rounds toward zero.
How do I get whole pages from a row count?
That is a ceiling, not a floor: CEIL(total * 1.0 / page_size). The * 1.0 avoids integer division on SQL Server, PostgreSQL and SQLite, which would otherwise round down before CEIL runs.
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.