Absolute Value In Oracle

Absolute Value In Oracle | In this post, we will discuss how to get the absolute value In the Oracle SQL database? What is Oracle abs() function? What is absolute value in SQL?

Absolute value describes the distance of a number on the number line from 0 without considering the direction. The absolute value of a number is always positive (distance can never be negative).

The absolute value of -8 is 8. Similarly, the absolute value of 5 is 5, and the absolute value of 0 is zero. The absolute value of -18.5 is 18.5

In the Oracle database, the abs() pre-defined function is available to find the absolute value of a given number. The number can be positive or negative.

Syntax:-
ABS(expression)

Example of ABS() function in Oracle,

SQL> SELECT ABS(-9) FROM dual;
   ABS(-9)
----------
         9
SQL> SELECT ABS(18) FROM dual;
   ABS(18)
----------
        18
SQL> SELECT ABS(20.99) FROM dual;
ABS(20.99)
----------
     20.99
SQL> SELECT ABS(-30.12345) FROM dual;
ABS(-30.12345)
--------------
      30.12345

Let us see some examples using the EMP table. The EMP table has COMM and SAL columns, where the SAL column contains the salary value, and the COMM column contains the commission value and for some employees COMM is NULL. If we subtract COMM-SAL then we can get negative values, but using the abs() function we can get the positive value.

SELECT ENAME, COMM, SAL FROM emp;

Output:-

ENAME            COMM        SAL
---------- ---------- ----------
SMITH                        800
ALLEN             300       1600
WARD              500       1250
JONES                       2975
MARTIN           1400       1250
BLAKE                       2850
CLARK                       2450
SCOTT                       3000
KING                        5000
TURNER              0       1500
ADAMS                       1100
JAMES                        950
FORD                        3000
MILLER                      1300

14 rows selected.
SELECT ENAME, COMM, SAL, ABS(COMM - SAL) 
FROM emp WHERE comm IS NOT NULL;

Output:-

ENAME            COMM        SAL ABS(COMM-SAL)
---------- ---------- ---------- -------------
ALLEN             300       1600          1300
WARD              500       1250           750
MARTIN           1400       1250           150
TURNER              0       1500          1500

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Also Learn,

Leave a Comment

Your email address will not be published. Required fields are marked *