Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
XINT64, 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;
Result
int64_valuefloat64_valuenumeric_value
423.750.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;
Result
tx_idabs_diffneeds_review
TX-10false
TX-20.05false
TX-310true

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;
Result
signed_daysdays_apart
-2222

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;
Result
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; use ABS(MOD(FARM_FINGERPRINT(id), n)).
  • Comparing FLOAT64 values with =. 0.1 + 0.2 = 0.3 is FALSE in BigQuery; compare with a tolerance such as ABS((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.

Errors you may hit

More mathematical functions

All BigQuery mathematical functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.