Beginner

268. DATE_TRUNC Function

Learn how to truncate a date or timestamp to a specific time period.

📘 Lesson 268 of 728 ⏱ 10 min 🎯 Intermediate

DATE_TRUNC reduces a date or timestamp to the beginning of a specified period such as year, month, week, day, or hour.

Truncating dates makes it easier to group transactions into consistent reporting periods.

Group orders by month using the first day of each month as the period value.

SQL
SELECT
    DATE_TRUNC('month', OrderDate) AS OrderMonth,
    SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY DATE_TRUNC('month', OrderDate);

DATE_TRUNC is widely used in financial reporting, monthly analysis, dashboards, trend analysis, and time-series queries.

  • DATE_TRUNC reduces dates to a period boundary.
  • It can work with year, month, week, day, and other units.
  • It is useful for time-based grouping.
  • It simplifies period-level analysis.
  • Database support and syntax vary.
What does DATE_TRUNC do?

DATE_TRUNC reduces a date or timestamp to the beginning of a specified time period.

Why is DATE_TRUNC useful in reporting?

It creates consistent period values that can be used to group transactions by month, week, year, or another time period.

Have you finished this lesson?

Introduction to SQL – All Lessons