SQL ABS() Function
The SQL ABS() is a function, and return absolute value of a given number from query result.
The SQL ABS() function remove the positive(+) or negative(-) sign symbol from the given input number.
Ex:
- Abs(5) = result is "5"
- Abs(5.77) = result is "5.77"
- Abs(5.12) = result is "5.12"
- Abs(+5) = result is "5"
- Abs(-5) = result is "5"
The SQL ABS() function is supports only numeric column or an numeric field based expression.
It can be used in SELECT statement as well in where clause.
Related Links
SQL ABS() Syntax
The below syntax is used to select a absolute value from a given number or expression without a table.
SELECT ABS(number);
The below syntax is used to select a absolute value from a given table numeric column or field from a specific table.
SELECT ABS(Column_name) FROM table_name;
SQL ABS() Example - Using Expression Or Formula
The following SQL SELECT statement returns the absolute value of a given input number or expression
SELECT Abs(-13) AS 'Absolute Value';
The result of above query is:
Absolute Value |
---|
13 |
SQL ABS() Function More Example
Input Value | Result |
---|---|
Abs(5) | 5 |
Abs(+6) | 6 |
Abs(-7) | 7 |
Abs(2.34) | 2.34 |
Abs(-1.6) | 1.6 |
Sample Database Table - Customer
CID | CName | Balance | City |
---|---|---|---|
111 | Suresh Babu | 128.66 | Nasik |
222 | Vinoth Kumar | -233.85 | Chennai |
333 | Azagu Varshith | 560 | Madurai |
444 | Haris Karthik | 673.88 | Bangalore |
SQL ABS() Example - With Table Column
The following SQL SELECT statement find the absolute value of a given table column "Balance" from the "Customer" table:
SELECT
ABS(Balance) As 'Absolute Balance Value'
FROM Customer;
The result of above query is:
Absolute Balance Value |
---|
128.66 |
233.85 |
560 |
673.88 |
SQL ABS() Example - Using WHERE Clause
The following SQL SELECT statement find the absolute value of a given input number for creating condition (greater than the given value) on table column "Balance" from the "Customer" table:
SELECT * FROM Customer
WHERE Balance > ABS(-200);
The result of above query is:
CID | CName | Balance | City |
---|---|---|---|
333 | Azagu Varshith | 560 | Madurai |
444 | Haris Karthik | 673.88 | Bangalore |
Note: The Abs(-200) function will return a value of "200". So now the result set contains and display records or rows which has balance more than "200" on column "Balance" from the "Customer" table.
Related Links