ABS
Returns the absolute value of a number.
MySQL 8.1
ABS(num)Parameters
- numDECIMAL
- Number
Return value
A non-negative number of the same type; NULL for NULL
Examples
Absolute value of a negative number
MySQL 8.1
SELECT ABS(-2);Prices closest to 250
MySQL 8.1
SELECT payment_id,
unit_price,
ABS(unit_price - 250) AS diff_from_250
FROM Payments
ORDER BY diff_from_250
LIMIT 3;Details
The absolute difference ABS(a - b) shows the distance between numbers regardless of sign, so it is handy for finding the closest values. For the smallest BIGINT (-9223372036854775808) the function gives an error: this type has no such positive number.