DATE_TRUNC (or TRUNC) rounds a date or timestamp down to the specified date part (year, month, week, day, etc.).
DATE_TRUNC returns the first instant of the requested date part, letting you group or compare data at consistent boundaries such as month-start or week-start.
Use either DATE_TRUNC('part', date) or TRUNC(date, 'part'). Both are interchangeable for dates.
Common parts: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND. Snowflake ignores case.
Aggregate metrics per period, join tables on aligned dates, or create calendar dimensions.Example: total monthly revenue.
SELECT c.id,
DATE_TRUNC('month', o.order_date) AS order_month,
SUM(o.total_amount) AS monthly_spend
FROM Orders o
JOIN Customers c ON c.id = o.customer_id
GROUP BY c.id, order_month
ORDER BY order_month;
Write DATE_TRUNC('month',23-08-15), not shorthand numbers, for readability.
Store dates in DATE or TIMESTAMP columns so DATE_TRUNC can use metadata and avoid casts.
Wrong: DATE_TRUNC(month, order_date).Right: DATE_TRUNC('month', order_date).
DATE_TRUNC drops timezone info. Cast to TIMESTAMP_LTZ first if you need local time.
DATE_PART extracts a specific part; TO_DATE converts strings to dates; DATEADD shifts dates.
.
Yes. If your fiscal year starts in July, add or subtract months before truncation: DATEADD(month, 6, DATE_TRUNC('year', DATEADD(month, -6, order_date))).
It works on all timestamp types. The return type is TIMESTAMP_NTZ unless you cast input to TIMESTAMP_LTZ.
Performance is identical; both resolve to the same internal function. Choose the one your team finds clearer.