SQL ROUND precision argument
Rounds a number to d decimal places (ROUND(123.4567, 2) is 123.46) with engine quirks on float input and negative digit counts.
- Categoría
- Math
- Returns
numeric- In this category
- 12 of 82
Sintaxis
ROUND(number, decimals)
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Different name or syntax | The two-argument form only accepts numeric; round(double precision, int) does not exist; cast to numeric first |
| MySQL | Sí | Supported under this name. |
| SQLite | Sí | round(X) and round(X, Y) are core functions on every version, no math extension needed |
| Oracle | Sí | Supported under this name. |
Ejemplos
Any engine
SELECT ROUND(123.4567, 2) AS r; -- 123.46
PostgreSQL
SELECT ROUND(AVG(price)::numeric, 2) AS avg_price FROM products;
SQLite
SELECT ROUND(2.675, 2) AS r; -- 2.67 or 2.68 depending on the stored float
Conviene saberlo
- PostgreSQL: round(0.285, 2) works on numeric, but round(0.285::float8, 2) is a type error. Cast: round(x::numeric, 2).
- d can be negative on SQL Server, PostgreSQL, MySQL and Oracle: ROUND(1234.56, -2) is 1200. SQLite treats a negative Y as 0.
- On float input the stored value rules: ROUND(2.675, 2) can be 2.67 because the float nearest 2.675 is a hair below it.
Questions about ROUND(x, d)
Why does PostgreSQL reject ROUND(x, 2) for my column?
The column is double precision, and PostgreSQL only defines round(numeric, int). Cast once: ROUND(x::numeric, 2). The one-argument round(double) exists but has no digit count.
Does the decimals argument accept negative values?
Yes on SQL Server, PostgreSQL (numeric only), MySQL and Oracle: ROUND(1234.56, -2) rounds to the nearest hundred and gives 1200. SQLite does not: a negative Y behaves like 0.
Why is ROUND(2.675, 2) not 2.68?
2.675 is not exactly representable as a binary float; the stored value sits just below it, so rounding gives 2.67. Use a decimal type when the digit boundary matters.
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.