BigQuery · Math function
BigQuery ABS
ABS(x) returns the absolute value of x with the same type as the input: ABS(-42) is 42. Use it for distances and tolerances, such as ABS(ledger_amount - bank_amount) > 1, and for day gaps in either order. ABS of the smallest INT64 value fails with an integer overflow.
ABS drops the sign of a number and keeps its type: INT64 stays INT64, NUMERIC stays NUMERIC and FLOAT64 stays FLOAT64. It is the building block for tolerance checks (ABS(a - b) < 0.01), deviations from a target and the number of days between two dates in either order: ABS(DATE_DIFF(d1, d2, DAY)).
There is one failure case. The smallest INT64, -9223372036854775808, has no positive counterpart in INT64, so ABS(-9223372036854775808) raises an integer overflow error; SAFE.ABS returns NULL instead. This matters for hashes: FARM_FINGERPRINT can in principle return that value, so take MOD first when bucketing: ABS(MOD(FARM_FINGERPRINT(id), 10)).
ABS accepts numbers only. The difference of two timestamps (ts1 - ts2) is an INTERVAL, and ABS(ts1 - ts2) fails with No matching signature for function ABS; use ABS(TIMESTAMP_DIFF(ts1, ts2, SECOND)) instead.
Syntax
ABS(X)Arguments and return value
| Argument | Description |
|---|---|
X | INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
Returns: The same type as X. Raises an error for the smallest INT64 value.
Examples
Each numeric type keeps its type
SELECT
ABS(-42) AS int64_value,
ABS(-3.75) AS float64_value,
ABS(NUMERIC '-0.05') AS numeric_value;| int64_value | float64_value | numeric_value |
|---|---|---|
| 42 | 3.75 | 0.05 |
Reconcile two amounts with a tolerance
Differences up to 1 are accepted; TX-3 is off by 10 and needs review.
WITH reconciliation AS (
SELECT 'TX-1' AS tx_id, NUMERIC '120.00' AS ledger_amount, NUMERIC '120.00' AS bank_amount UNION ALL
SELECT 'TX-2', NUMERIC '75.50', NUMERIC '75.45' UNION ALL
SELECT 'TX-3', NUMERIC '300.00', NUMERIC '310.00'
)
SELECT
tx_id,
ABS(ledger_amount - bank_amount) AS abs_diff,
ABS(ledger_amount - bank_amount) > 1 AS needs_review
FROM reconciliation
ORDER BY tx_id;| tx_id | abs_diff | needs_review |
|---|---|---|
| TX-1 | 0 | false |
| TX-2 | 0.05 | false |
| TX-3 | 10 | true |
Days apart, in either order
SELECT
DATE_DIFF(DATE '2026-11-01', DATE '2026-11-23', DAY) AS signed_days,
ABS(DATE_DIFF(DATE '2026-11-01', DATE '2026-11-23', DAY)) AS days_apart;| signed_days | days_apart |
|---|---|
| -22 | 22 |
The smallest INT64 value
ABS(-9223372036854775808) fails with an integer overflow error. SAFE.ABS returns NULL instead.
SELECT SAFE.ABS(-9223372036854775808) AS abs_of_int64_min;| abs_of_int64_min |
|---|
| NULL |
Common mistakes
- Taking ABS of a hash before MOD.
MOD(ABS(FARM_FINGERPRINT(id)), n)can overflow on the INT64 minimum; useABS(MOD(FARM_FINGERPRINT(id), n)). - Comparing FLOAT64 values with
=.0.1 + 0.2 = 0.3is FALSE in BigQuery; compare with a tolerance such asABS((0.1 + 0.2) - 0.3) < 1e-9. - Passing an INTERVAL.
ABS(end_ts - start_ts)fails; turn the gap into a number first with TIMESTAMP_DIFF, DATETIME_DIFF or DATE_DIFF. - Applying ABS before summing signed amounts.
SUM(ABS(amount))adds refunds to revenue; keep the sign unless you want total volume.
FAQ
How do I get the absolute difference between two dates in BigQuery?
Use ABS(DATE_DIFF(date_a, date_b, DAY)). DATE_DIFF returns a signed INT64, and ABS removes the sign.
Why does ABS fail with an integer overflow?
The input is -9223372036854775808, the smallest INT64, whose absolute value is one more than the largest INT64. Use SAFE.ABS to get NULL, or cast to NUMERIC first: ABS(CAST(x AS NUMERIC)).
Does ABS change the data type?
No. ABS returns INT64 for INT64, NUMERIC for NUMERIC, BIGNUMERIC for BIGNUMERIC and FLOAT64 for FLOAT64.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
