PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL TRUNC() Function



The PostgreSQL TRUNC() function returns a number truncated to the specified number of decimal places. If decimal_places is a negative number, this function will make digits to the left of the decimal place 0 values.

Syntax

TRUNC(number, decimal_places)

Parameters

number Required. Specify the number to truncate.
decimal_places Optional. Specify the number of decimal places truncate to. This value must be a positive or negative integer. If this parameter is omitted, the function rounds the number to 0 decimal places.

Return Value

Returns the truncated value of the number to specified decimal_places.

Example 1:

The example below shows the usage of TRUNC() function.

SELECT TRUNC(1234.5678, 0);
Result: 1234

SELECT TRUNC(1234.5678, 1);
Result: 1234.5

SELECT TRUNC(1234.5678, 2);
Result: 1234.56

SELECT TRUNC(1234.5678, 3);
Result: 1234.567

SELECT TRUNC(1234.5678, -1);
Result: 1230

SELECT TRUNC(1234.5678, -2);
Result: 1200

SELECT TRUNC(1234.5678);
Result: 1234

SELECT TRUNC(-1234.5678);
Result: -1234

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 1-3.75567
Data 2-5.3867
Data 313.9804
Data 493.1601
Data 548.1322

The statement given below can be used to truncate the records of column x to 2 decimal places.

SELECT *, TRUNC(x, 2) AS TRUNC_Value FROM Sample;

This will produce the result as shown below:

DataxTRUNC_Value
Data 1-3.75567-3.75
Data 2-5.3867-5.38
Data 313.980413.98
Data 493.160193.16
Data 548.132248.13

❮ PostgreSQL Functions