Abs Function In Sql in SQL
? ABS() Function in SQL
The ABS() function in SQL returns the absolute (positive) value of a number — it removes any negative sign.
? Syntax
SELECT ABS(number);? Example Usage
1. Basic Example
SELECT ABS(-100) AS result;-- Output: 1002. With a Table
Assume a table transactions:
| id | amount |
|---|---|
| 1 | -500 |
| 2 | 300 |
| 3 | -150 |
Query:
SELECT id, ABS(amount) AS abs_amountFROM transactions;Result:
| id | abs_amount |
|---|---|
| 1 | 500 |
| 2 | 300 |
| 3 | 150 |
? Use Cases
Financial calculations (e.g., profit/loss)
Distance measurements (difference between values)
Normalization of values
? Supported In
? MySQL
? PostgreSQL
? SQL Server
? Oracle
? SQLite
? Tip
ABS() works on both integer and decimal types.
SELECT ABS(-5.75); -- Output: 5.75Let me know if you want practical problems or query examples using ABS() with joins or conditions!