// math reference

SQL TRUNCATE numeric function

Cuts a number at a given decimal position and discards the rest, without rounding. It moves toward zero, so it differs from FLOOR on negative numbers.

Catégorie
Math
Returns
numeric
In this category
10 of 82

Syntaxe

TRUNCATE(number, decimals)  -- MySQL
TRUNC(number [, decimals])  -- PostgreSQL, Oracle
ROUND(number, decimals, 1)  -- SQL Server

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

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Different name or syntax no TRUNC; ROUND(x, d, 1) truncates via the third argument
PostgreSQL Oui Supported under this name.
MySQL Different name or syntax spelled TRUNCATE(x, d); the decimals argument is required
SQLite Different name or syntax trunc(x) to a whole number (3.35+ math functions); CAST(x * 100 AS INTEGER) / 100.0 for 2 dp
Oracle Oui TRUNC(n, d), and the same name truncates dates

Exemples

MySQL

SELECT TRUNCATE(123.4567, 2) AS cut;  -- 123.45

PostgreSQL

SELECT TRUNC(123.4567, 2) AS cut;  -- 123.45

SQL Server

SELECT ROUND(123.4567, 2, 1) AS cut;  -- 123.45

SQLite

SELECT CAST(123.4567 * 100 AS INTEGER) / 100.0 AS cut;  -- 123.45

Bon à savoir

  • TRUNC always discards: TRUNC(2.99, 1) is 2.9, not 3.0.
  • Negative decimals cut whole positions: TRUNC(1234, -2) is 1200 on PostgreSQL and Oracle, TRUNCATE(1234, -2) on MySQL, ROUND(1234, -2, 1) on SQL Server.
  • Oracle's TRUNC does double duty: TRUNC(date) cuts the time part, TRUNC(number) cuts decimals. Same name, different overloads.

Questions about TRUNCATE / TRUNC (numbers)

What is the difference between TRUNC and ROUND?

ROUND considers the dropped digit and may carry; TRUNC never does. TRUNC(-2.9, 0) is -2 (toward zero), while ROUND gives -3.

How do I truncate instead of round in SQL Server?

ROUND takes an optional third argument, the documented function parameter: any non-zero value truncates instead of rounding, so ROUND(123.4567, 2, 1) is 123.4500. The result keeps the input's scale.

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.