abs

Returns the absolute value of a number.

Syntax

abs(<expr>)

Arguments

ArgumentTypeDescription
<expr>int, uint, or floatInput value.

Returns

Returns the same type as the input. Returns NULL in either case:

  • The input is NULL.
  • The input is the minimum int, -9223372036854775808. Its absolute value is outside the int range.

Examples

Compare the magnitude of positive and negative inventory corrections without losing the signed adjustment.

VALUES ('evt-17', -12), ('evt-42', 5), ('evt-63', -2)
SELECT $0 AS event_id, $1 AS stock_delta
SELECT event_id, stock_delta, abs(stock_delta) AS changed_units
ORDER BY changed_units DESC
+----------+-------------+---------------+
| event_id | stock_delta | changed_units |
+----------+-------------+---------------+
| evt-17   | -12         | 12            |
| evt-42   | 5           | 5             |
| evt-63   | -2          | 2             |
+----------+-------------+---------------+