DATE_TRUNC in BigQuery rounds a DATE to the first day of the specified calendar part (YEAR, QUARTER, MONTH, WEEK, or DAY).
A retailer might need monthly revenue totals. Truncating order dates to the first day of each month simplifies GROUP BY logic and improves query clarity.
DATE_TRUNC(date_expression, part) converts every supplied DATE to the first day of the requested part. The result always retains DATE type, making it safe for joins and aggregations.
YEAR, QUARTER, MONTH, WEEK, and DAY are valid.BigQuery rejects unsupported parts with an error, so always validate the part before running production SQL.
Use DATE_TRUNC(date_column, part). Place part
in uppercase and enclose in quotes if you prefer string literals. Aliasing the result keeps downstream SQL readable.
First truncate Orders.order_date
to MONTH, then GROUP BY the truncated value while summing total_amount
. The example below demonstrates.
BigQuery DATE_TRUNC is calendar-based.For non-January fiscal years, add interval offsets before truncation, then reverse the offset after aggregation.
If your table is partitioned by DATE, apply DATE_TRUNC only after the partition filter. Filtering on raw partitions first prevents full-table scans.
The query in the next section shows how to count orders per customer per month using DATE_TRUNC and standard ecommerce tables.
.
Yes. A given DATE and part always produces the same result.
Use DATE_TRUNC(order_date, WEEK). Weeks start on Monday in BigQuery’s ISO 8601 implementation.
First CAST the STRING to DATE: DATE_TRUNC(CAST(order_date AS DATE), MONTH).