DATE_TRUNC cuts a DATE or TIMESTAMP down to a chosen precision (year, month, day, etc.) so you can group or compare dates easily.
DATE_TRUNC removes the smaller time portions from a DATE or TIMESTAMP, letting you compare or aggregate rows on the same calendar boundary without manual string work.
Use DATE_TRUNC('precision', column). Precisions include millennium, century, decade, year, quarter, month, week, day, hour, minute, second.
Sales dashboards often group orders by month.Truncating order_date to month gives repeatable bucket edges and speeds GROUP BY queries.
Yes—return both the truncated value for display and an aggregate, e.g., monthly revenue. The function is immutable and index-friendly on expression indexes.
SELECT DATE_TRUNC('month', order_date) AS month, SUM(total_amount) FROM Orders GROUP BY 1 ORDER BY 1;
SELECT DATE_TRUNC('day', created_at) AS day, COUNT(DISTINCT customer_id) FROM Customers GROUP BY 1;
Yes.For TIMESTAMPTZ, PostgreSQL converts to the session time zone before truncation. Set TIME ZONE 'x' to control results.
Create an expression index on DATE_TRUNC result when filtering large tables repeatedly. Keep precision strings lowercase for readability and consistency.
.
Yes. It always returns the same output for the same input and precision, making it safe for indexed expression columns.
Yes. Use DATE_TRUNC('quarter', order_date). PostgreSQL maps quarters to calendar quarters starting January.
Absolutely. Pass a text variable or column containing the precision string, but note that expression indexes require a literal string.