Date & Time
BigQuery
DATE_TRUNC
Memotong DATE ke granularitas yang ditentukan (DAY, WEEK, MONTH, QUARTER, YEAR).
Tipe hasil:
DATEDiperbarui: 7 Jan 2026Syntax
SQL
DATE_TRUNC(date_expression, date_part)Parameter
date_expressionDATEwajib
DATE yang akan dipotong
date_partDATE_PARTwajib
Granularitas: DAY, WEEK, MONTH, QUARTER, YEAR, etc.
Contoh Penggunaan
Basic Truncation
SQL
1 SELECT 2 date, 3 DATE_TRUNC(date, WEEK) as week_start, 4 DATE_TRUNC(date, MONTH) as month_start, 5 DATE_TRUNC(date, YEAR) as year_start 6 FROM UNNEST([DATE '2024-01-15']) as date;
Potong tanggal ke berbagai granularitas.
Hasil
| date | week_start | month_start | year_start |
|---|---|---|---|
| 2024-01-15 | 2024-01-14 | 2024-01-01 | 2024-01-01 |
Monthly Aggregation
SQL
1 SELECT 2 DATE_TRUNC(order_date, MONTH) as month, 3 COUNT(*) as order_count, 4 SUM(amount) as total_amount 5 FROM `project.dataset.orders` 6 GROUP BY month 7 ORDER BY month;
Agregasi penjualan per bulan.
Week Starting Monday
SQL
1 SELECT 2 date, 3 DATE_TRUNC(date, WEEK(MONDAY)) as week_monday 4 FROM UNNEST([DATE '2024-01-15']) as date;
Minggu dimulai hari Senin.
Hasil
| date | week_monday |
|---|---|
| 2024-01-15 | 2024-01-15 |