PL/SQL Trunc

The Oracle PL/SQL TRUNC function is used to truncate a numeric value to a specified number of decimal places. It takes two arguments: the first argument is the numeric value to be truncated, and the second argument is the number of decimal places to truncate the value to.

Syntax

The syntax of the TRUNC function is as follows:

TRUNC(numeric_value, decimal_places)

where numeric_value is the value to be truncated, and decimal_places is the number of decimal places to truncate to. If decimal_places is not specified, it defaults to 0, which means that the function will truncate the value to the nearest integer.

The TRUNC function can be used with both positive and negative numbers. If the numeric_value is positive, the function truncates the number towards zero. If the numeric_value is negative, the function truncates the number away from zero.

Examples

Here are some examples of using the Oracle PL/SQL TRUNC function:

SELECT TRUNC(3.14159) FROM DUAL;
-- Result: 3

SELECT TRUNC(3.14159, 2) FROM DUAL;
-- Result: 3.14

SELECT TRUNC(3.14159, -1) FROM DUAL;
-- Result: 0

SELECT TRUNC(-3.14159) FROM DUAL;
-- Result: -3

SELECT TRUNC(-3.14159, 2) FROM DUAL;
-- Result: -3.14

SELECT TRUNC(-3.14159, -1) FROM DUAL;
-- Result: -0

In the first example, the TRUNC function truncates the value of 3.14159 to the nearest integer, which is 3.

In the second example, the TRUNC function truncates the value of 3.14159 to two decimal places, which results in the value of 3.14.

In the third example, the TRUNC function truncates the value of 3.14159 to the nearest tens place, which is 0.

In the fourth example, the TRUNC function truncates the value of -3.14159 to the nearest integer, which is -3.

In the fifth example, the TRUNC function truncates the value of -3.14159 to two decimal places, which results in the value of -3.14.

In the sixth example, the TRUNC function truncates the value of -3.14159 to the nearest tens place, which is 0.

In summary, the TRUNC function in Oracle PL/SQL is a useful tool for truncating numeric values to a specified number of decimal places. It can be used to format numbers for display or to perform calculations that require a certain level of precision.