SQL ABS() function returns the absolute value of a number when passed as an argument, i.e., the negative number gets converted to an absolute positive number.
SQL ABS()
SQL ABS() is a built-in method is used to get the absolute value of the number passed as an argument. An expression is the numeric value or numeric data type.
The bit data type is not allowed. All of the above platforms support the SQL syntax of ABS().
Syntax
SELECT ABS (expression);
Parameters
Expression: The number whose absolute positive value is to be retrieved.
Examples
Query 1
SELECT ABS (-25);
Output
25
Query 2
SELECT ABS (-25.6);
Output
25.6
Query 3
SELECT ABS (24.65 * -1);
Output
24.65
Query 4
SELECT ABS (2 + 55 – 77);
Output
20
Here, the ABS() function also works on multiple values.
Let’s apply the ABS() function to a table.
Consider Table: (Agents)
ID | NAME | WORKING_AREA | COMMISSION | PHONE |
101 | Karan | Allahabad | 0.15 | 9989887854 |
102 | Shivam | Palampur | 0.14 | 8956858598 |
103 | Rohit | Patna | 0.13 | 8787458745 |
104 | Rounak | Dhulian | 0.15 | 8885457854 |
105 | Nishant | Kolkata | 0.16 | 7895856589 |
106 | Shouvik | Barharwa | 0.17 | 7455878964 |
Suppose, if we want to get a unique absolute value from the columns named COMMISSION after multiplying it by (-1), then the following query has to be executed.
Query
SELECT DISTINCT (ABS (commission* (-1))) "DISTINCT (ABS ())" FROM Agents;
Output
DISTINCT (ABS ()) |
.15 |
.14 |
.13 |
.16 |
.17 |
Here, we had used the DISTINCT clause, which is used for displaying unique values and ABS() function, which converted negative value to positive ones.