abs
SQLite 3.0.0 (original release)→ INTEGER or REALReturns the absolute value of the numeric argument X, returning NULL if X is NULL and 0.0 if X is a string or blob that cannot be converted to a numeric value.
Signature
abs(X)Parameters
| Parameter | Type | Description |
|---|---|---|
| X | any | The numeric value whose absolute value is to be returned. |
Examples
Basic absolute value of a negative integer
SELECT abs(-5);5Absolute value of a positive real
SELECT abs(3.14);3.14Absolute value of a negative real
SELECT abs(-3.14);3.14NULL propagates through abs()
SELECT abs(NULL);NULLNon-numeric strings return 0.0
SELECT abs('not a number');0.0Writing WHERE abs(price - target) < 10 forces SQLite to compute abs() for every row, preventing any index on the price column from being used. This causes a full table scan even when a perfectly good index exists.
✓ Instead: Use a range predicate: WHERE price BETWEEN target - 10 AND target + 10 so the query planner can use an index on price.
Passing the minimum 64-bit integer -9223372036854775808 to abs() throws an integer overflow error because there is no positive 64-bit equivalent. The random() function deliberately avoids generating this value so its output can always be safely passed to abs().
In PostgreSQL, abs() raises an error for numeric overflow too, but the overflow boundary differs by type. In SQLite, the only dangerous value is the single 64-bit minimum integer -9223372036854775808; passing a non-numeric string returns 0.0 instead of an error, unlike PostgreSQL which raises a cast error.