PLSQL | ABS Function

The PLSQL ABS function is used for returning the absolute value of a number. Absolute value is used for depicting the distance of a number on the number line from 0. The direction of the number from zero is not considered since the absolute value of a number is never negative.

The ABS in PLSQL function takes as an argument any numeric data type or any non-numeric data type that can be implicitly converted to a numeric data type. The value returned by the PLSQL ABS function is of the same data type as the numeric data type of the argument.

Syntax:



ABS( number )

Parameters Used:

number – It is used to specify the number whose absolute value you want to know.

Return Value:
The ABS function in PLSQL returns a numeric value.

Supported Versions of Oracle/PLSQL:

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

Example-1: Using a positive numeric value as an argument in the ABS function.

DECLARE 
   Test_Number int := 20;
   
BEGIN 
   dbms_output.put_line(ABS(Test_Number)); 
   
END; 

Output:

20 

Example-2: Using a negative numeric value as an argument in the ABS function.

DECLARE 
   Test_Number int := -12;
   
BEGIN 
   dbms_output.put_line(ABS(Test_Number)); 
   
END; 

Output:

12 

Example-3: Using a negative numeric value with decimal as an argument in the ABS function.

DECLARE 
   Test_Number decimal(7, 2) := -12.23;
   
BEGIN 
   dbms_output.put_line(ABS(Test_Number)); 
   
END;  

Output:


12.23 

Example-4: Using an expression as an argument in the ABS function.

DECLARE 
   Test_Number decimal(11, 2) := (-20.45 * 2);
   
BEGIN 
   dbms_output.put_line(ABS(Test_Number)); 
   
END;   

Output:

40.9 

Example-5: Using ABS function with select query.

SELECT ABS(-20.45 * 1) FROM dual; 

Output:

20.45 

Advantages:
The ABS function accepts any numeric datatype as well as any non-numeric datatype as an argument that can be implicitly converted to a numeric datatype.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

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.