Subtracts a specified time interval from a DATE. Returns a new DATE.
DATEDiperbarui: 13 Jun 2026DATE_SUB(date_expression, INTERVAL int64_expression date_part)The starting DATE
Number of intervals to subtract
Interval unit: DAY, WEEK, MONTH, QUARTER, YEAR
1 SELECT 2 CURRENT_DATE() as today, 3 DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) as week_ago, 4 DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) as month_ago;
Subtract various intervals from the current date.
| today | week_ago | month_ago |
|---|---|---|
| 2024-01-15 | 2024-01-08 | 2023-12-16 |
1 SELECT * 2 FROM `project.dataset.orders` 3 WHERE order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY);
Filter records from the last 30 days.
1 SELECT 2 DATE_TRUNC(order_date, MONTH) as month, 3 SUM(amount) as this_year, 4 SUM(CASE 5 WHEN order_date >= DATE_SUB(DATE_TRUNC(order_date, MONTH), INTERVAL 1 YEAR) 6 THEN amount 7 END) as last_year 8 FROM `project.dataset.orders` 9 GROUP BY month;
Compare current period sales with the same period from the prior year.
Sudah paham DATE_SUB? Latih langsung di browser
Latihan interaktif, langsung di browser.