PLSQL | TRUNC Function

The TRUNC function is an inbuilt function in PLSQL which is used to return a number truncated to a particular number of decimal places.

Syntax:

TRUNC( number, decimal_places )

Parameters Used:
This function accepts two parameters which are illustrated below:-

  • number – This is the input number which is going to be truncated to a certain number.
  • decimal_places – This is also a input number which specifies that up to what number after decimal point should be the output of this function.

Return Value:
This function returns a numeric value truncated to a particular number of decimal places.

Supported Versions of Oracle/PLSQL:



  1. Oracle 12c
  2. Oracle 11g
  3. Oracle 10g
  4. Oracle 9i
  5. Oracle 8i

Let’s see some examples which illustrate the TRUNC function:

Example-1:

DECLARE 
   Test_Number number := 5.5;
   
BEGIN 
   dbms_output.put_line(TRUNC(Test_Number number)); 
   
END;  

Output:

5

In the above example, the truncated value of 5.5 is 5

Example-2:

DECLARE 
   Test_Number number1 := 5;
   Test_Number number2 := 0;
   
BEGIN 
   dbms_output.put_line(TRUNC(Test_Number number1, 
                              Test_Number number2)); 
   
END;  

Output:

5

In the above example, the truncated value of (5, 0) is 5 because 5 is not having any decimal point and hence it returns 5 as the output.

Example-3:

DECLARE 
   Test_Number number1 := 15.3123;
   Test_Number number2 := 2;
   
BEGIN 
   dbms_output.put_line(TRUNC(Test_Number number1, 
                              Test_Number number2)); 
   
END;  

Output:

15.31

In the above example, the truncated value of 15.3123 is 15.31 because here 2 is at the place of decimal_place parameter and it shows that the output value must contain 2 decimal number after the decimal point.

Advantage:
This function is used to return a number truncated to a particular number of decimal places.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.