// math reference

SQL ROUND to nearest ten or hundred

Rounds to a position left of the decimal point using a negative second argument, the terse way to bucket numbers into tens, hundreds or thousands.

Categoria
Math
Returns
numeric
In this category
13 of 82

Sintaxe

ROUND(number, -1)
ROUND(number, -2)
ROUND(number, -3)

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

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim Supported under this name.
PostgreSQL Sim Works on numeric input; cast double precision to numeric first
MySQL Sim Supported under this name.
SQLite Different name or syntax round(X, Y) ignores negative Y; it rounds to 0 digits instead
Oracle Sim ROUND(n, -m) is standard Oracle practice

Exemplos

SQL Server

SELECT ROUND(1234.56, -2) AS bucket;  -- 1200.00; same on PostgreSQL, MySQL, Oracle; SQLite gives 1235.0

MySQL

SELECT ROUND(sales, -3) AS sales_k
FROM monthly_totals;

SQLite

SELECT ROUND(amount / 100.0) * 100.0 AS to_hundred
FROM invoices;

Bom saber

  • ROUND(1234, -2) is 1200 and ROUND(1250, -2) is 1300: the same half-up rule as positive digits, applied left of the point.
  • SQLite returns the value rounded to 0 digits for any negative Y, so round-to-hundreds needs ROUND(x / 100.0) * 100.
  • It only rounds to powers of ten. For buckets like 5 or 25, divide, round, and multiply back: ROUND(x / 5.0) * 5. Keep the .0, or integer x on SQL Server, PostgreSQL and SQLite is truncated by the division before ROUND runs.

Questions about ROUND(x, -n)

How do I round to the nearest ten or hundred?

Pass a negative digit count: ROUND(1234, -1) is 1230, ROUND(1234, -2) is 1200. SQLite lacks negative precision, so divide by 100, round, and multiply back.

Does negative precision work on floats?

Yes, but the float storage caveat still applies, and on PostgreSQL the two-argument form needs numeric input, so cast first: ROUND(x::numeric, -2).

How do I round to the nearest 5?

No engine rounds to 5 directly. Divide by 5.0, round to an integer, multiply by 5: ROUND(x / 5.0) * 5. Dividing by a plain 5 truncates integer columns first on SQL Server, PostgreSQL and SQLite, so 13 would land on 10 instead of 15.

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.