// math reference

SQL ROUND function

Rounds a number to a given number of decimal places, and to whole tens, hundreds and beyond with a negative digit count. The trap is that the rounding mode depends on the data type.

カテゴリ
Math
Returns
numeric
In this category
1 of 82

構文

ROUND(number, decimals)

エンジンサポート

エンジンSupportメモ
SQL Server はい Supported under this name.
PostgreSQL はい Supported under this name.
MySQL はい Supported under this name.
SQLite はい Supported under this name.
Oracle はい Supported under this name.

例

Any engine

SELECT ROUND(123.4567, 2) AS two_dp;  -- 123.46

PostgreSQL

-- also SQL Server, MySQL and Oracle; not SQLite
SELECT ROUND(1234, -2) AS hundreds;  -- 1200

PostgreSQL

SELECT ROUND(2.5::numeric) AS half_up, ROUND(2.5::float8) AS bankers;
-- 3, 2

知っておくと便利なこと

  • PostgreSQL rounds NUMERIC half away from zero but DOUBLE PRECISION half to even (banker's rounding): ROUND(2.5::numeric) is 3, ROUND(2.5::float8) is 2.
  • Negative decimals round to powers of ten: ROUND(1234, -2) is 1200 on SQL Server, PostgreSQL, MySQL and Oracle. SQLite treats a negative digit count as 0 and returns 1234.0.
  • MySQL rounds DOUBLE values half to even too: ROUND(2.5e0) is 2, while ROUND(2.5) on the exact DECIMAL literal is 3. Oracle does the same split: ROUND(2.5d) on a BINARY_DOUBLE is 2, ROUND(2.5) on a NUMBER is 3. SQL Server's ROUND on float rounds 2.5 to 3.
  • SQL Server's ROUND has a hidden third argument: ROUND(x, 2, 1) truncates instead of rounds.

Questions about ROUND

Does SQL round 2.5 up to 3?

For NUMERIC/DECIMAL, all five round half away from zero, so yes. Floating-point values are different: PostgreSQL, MySQL and Oracle (BINARY_DOUBLE) round a float 2.5 half to even, giving 2. Keep money in DECIMAL columns so ROUND behaves predictably.

How do I round to the nearest ten or hundred?

A negative second argument: ROUND(1234, -2) is 1200 on SQL Server, PostgreSQL, MySQL and Oracle. SQLite ignores negative digits, so divide, round and multiply: ROUND(1234 / 100.0) * 100.

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.