BigQuery · Math function
BigQuery TRUNC
TRUNC(x, n) cuts x to n decimal places by rounding toward zero: TRUNC(2.789, 2) is 2.78 and TRUNC(-2.789, 2) is -2.78. Without n it drops the fractional part. It never rounds up and never overflows. TRUNC is for numbers only; truncate dates with DATE_TRUNC.
TRUNC removes digits instead of rounding them. With one argument it returns the integer part (TRUNC(2.7) is 2.0, TRUNC(-2.7) is -2.0). With N it keeps N digits after the decimal point, and a negative N zeroes digits to the left of it: TRUNC(1789, -2) is 1700.0. Unlike ROUND it never rounds up, and unlike FLOOR it moves negative numbers toward zero.
INT64 and FLOAT64 inputs return FLOAT64; NUMERIC and BIGNUMERIC keep their type. FLOAT64 values often sit just below the decimal you see, so TRUNC(4.35, 2) returns 4.34 because 4.35 is stored as 4.3499999...; the NUMERIC version returns 4.35.
In BigQuery, TRUNC only accepts numbers. Oracle, Snowflake and Redshift also apply TRUNC to dates, but here TRUNC(order_date) fails with No matching signature for function TRUNC; use DATE_TRUNC or TIMESTAMP_TRUNC. MySQL's TRUNCATE(x, d) is called TRUNC here.
Syntax
TRUNC(X [, N])Arguments and return value
| Argument | Description |
|---|---|
X | The number to truncate: INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
N | Optional INT64. Digits to keep after the decimal point; negative values zero out digits to the left of it. Defaults to 0. |
Returns: FLOAT64 for INT64 or FLOAT64 input, NUMERIC for NUMERIC and BIGNUMERIC for BIGNUMERIC.
Examples
Truncate to decimals, whole numbers and hundreds
SELECT
TRUNC(2.789, 2) AS two_decimals,
TRUNC(-2.789, 2) AS negative,
TRUNC(2.789) AS whole_number,
TRUNC(1789, -2) AS hundreds;| two_decimals | negative | whole_number | hundreds |
|---|---|---|---|
| 2.78 | -2.78 | 2.0 | 1700.0 |
TRUNC, FLOOR, ROUND and CAST side by side
For 2.7, TRUNC and FLOOR return 2.0 while ROUND and CAST return 3. For -2.7, TRUNC goes toward zero, FLOOR goes down, and ROUND and CAST go to the nearest integer.
SELECT
x,
TRUNC(x) AS trunc_x,
FLOOR(x) AS floor_x,
ROUND(x) AS round_x,
CAST(x AS INT64) AS cast_x
FROM UNNEST([2.7, -2.7]) AS x
ORDER BY x DESC;| x | trunc_x | floor_x | round_x | cast_x |
|---|---|---|---|---|
| 2.7 | 2.0 | 2.0 | 3.0 | 3 |
| -2.7 | -2.0 | -3.0 | -3.0 | -3 |
FLOAT64 representation errors
4.35 can't be stored exactly as FLOAT64, so truncating to two decimals loses a cent. NUMERIC keeps the exact value.
SELECT
TRUNC(4.35, 2) AS float64_result,
TRUNC(NUMERIC '4.35', 2) AS numeric_result;| float64_result | numeric_result |
|---|---|
| 4.34 | 4.35 |
Group latencies into 100 ms bins
A negative N truncates to hundreds. The bins come back as FLOAT64; DIV(latency_ms, 100) * 100 gives INT64 bins instead.
WITH requests AS (
SELECT latency_ms FROM UNNEST([142, 187, 205, 960]) AS latency_ms
)
SELECT
TRUNC(latency_ms, -2) AS bin_start_ms,
COUNT(*) AS requests
FROM requests
GROUP BY bin_start_ms
ORDER BY bin_start_ms;| bin_start_ms | requests |
|---|---|
| 100.0 | 2 |
| 200.0 | 1 |
| 900.0 | 1 |
Common mistakes
- Calling TRUNC on a DATE or TIMESTAMP as in Oracle, Snowflake or Redshift. It fails with No matching signature; use DATE_TRUNC, DATETIME_TRUNC or TIMESTAMP_TRUNC.
- Treating TRUNC and FLOOR as the same. They differ for negative values:
TRUNC(-2.7)is -2.0 andFLOOR(-2.7)is -3.0. - Using CAST to drop decimals.
CAST(2.7 AS INT64)returns 3 because CAST rounds; useCAST(TRUNC(x) AS INT64)to cut toward zero. - Truncating FLOAT64 money values.
TRUNC(4.35, 2)returns 4.34; keep amounts in NUMERIC. - Writing MySQL's
TRUNCATE(x, d). BigQuery returns Function not found: TRUNCATE. The function is TRUNC, and TRUNCATE only exists as the TRUNCATE TABLE statement.
FAQ
What is the difference between TRUNC and ROUND in BigQuery?
ROUND goes to the nearest value, with halves away from zero; TRUNC always cuts toward zero. ROUND(2.789, 2) is 2.79 and TRUNC(2.789, 2) is 2.78.
How do I remove decimals without rounding?
TRUNC(x) returns the whole-number part as FLOAT64 (or NUMERIC for NUMERIC input). Wrap it as CAST(TRUNC(x) AS INT64) for an integer, because a plain CAST rounds.
How do I truncate a date or timestamp in BigQuery?
Use DATE_TRUNC(order_date, MONTH) for dates, TIMESTAMP_TRUNC(event_ts, DAY) for timestamps and DATETIME_TRUNC for datetimes. TRUNC only accepts numbers.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
