Floor Function In Sql in SQL
? FLOOR() Function in SQL
The FLOOR() function returns the largest integer less than or equal to a given numeric value.
? Syntax:
FLOOR(number)number: A numeric value (can be decimal or negative).
? Examples:
SELECT FLOOR(15.9); -- Returns 15SELECT FLOOR(15.1); -- Returns 15SELECT FLOOR(-3.7); -- Returns -4SELECT FLOOR(100); -- Returns 100? Use Cases:
| Use Case | Example |
|---|---|
| Round down prices | FLOOR(price) |
| Pagination calculations | FLOOR(total_rows / rows_per_page) |
| Time rounding (to hours) | FLOOR(minutes / 60) |
? Compare with Other Rounding Functions:
| Function | Description | Example (-2.3) |
|---|---|---|
FLOOR() | Round down to nearest integer | -3 |
CEIL() | Round up to nearest integer | -2 |
ROUND() | Round to nearest integer or decimal places | -2 |
TRUNCATE() | Trims to specified decimal places | -2.3 or -2 |
Let me know if you’d like examples with tables or used in real-world scenarios like tax or billing calculations.