SQL Date Functions

SQL DATE_TRUNC: Truncate Date to Month, Year, Week (All Databases)

Truncate dates to specific precision in SQL. Covers DATE_TRUNC (PostgreSQL), DATE() (MySQL), DATEFROMPARTS (SQL Server), TRUNC (Oracle).

Mar 12, 20264 min read

Overview

DATE_TRUNC rounds a date down to the start of a given time period (month, week, year). Essential for time-series grouping and reporting.

PostgreSQL

-- DATE_TRUNC(precision, date):
SELECT DATE_TRUNC('month', TIMESTAMP '2026-03-12 14:30:00');
-- '2026-03-01 00:00:00'

SELECT DATE_TRUNC('year', NOW());   -- '2026-01-01 00:00:00'
SELECT DATE_TRUNC('week', NOW());   -- start of this week
SELECT DATE_TRUNC('hour', NOW());   -- current hour start

-- Group by month:
SELECT DATE_TRUNC('month', created_at) AS month,
  SUM(amount) AS revenue
FROM orders
GROUP BY DATE_TRUNC('month', created_at)
ORDER BY month;

MySQL

-- No DATE_TRUNC — use DATE_FORMAT or DATE:
SELECT DATE(NOW());  -- '2026-03-12' (truncate to day)

-- Truncate to month:
SELECT DATE_FORMAT(created_at, '%Y-%m-01') AS month
FROM orders;

-- Or LAST_DAY trick:
SELECT DATE_FORMAT(created_at, '%Y-%m-01') AS month,
  SUM(amount) AS revenue
FROM orders
GROUP BY DATE_FORMAT(created_at, '%Y-%m-01');

-- Truncate to year:
SELECT MAKEDATE(YEAR(created_at), 1) AS year_start FROM orders;

SQL Server

-- Cast to date (truncate time):
SELECT CAST(GETDATE() AS DATE);  -- '2026-03-12'

-- Truncate to month:
SELECT DATEFROMPARTS(YEAR(created_at), MONTH(created_at), 1) AS month
FROM orders;

-- Or DATETRUNC (SQL Server 2022+):
SELECT DATETRUNC(MONTH, created_at) AS month FROM orders;

-- Group by month:
SELECT DATEFROMPARTS(YEAR(created_at), MONTH(created_at), 1) AS month,
  SUM(amount) AS revenue
FROM orders
GROUP BY DATEFROMPARTS(YEAR(created_at), MONTH(created_at), 1);

Oracle

-- TRUNC(date, format):
SELECT TRUNC(SYSDATE) FROM DUAL;          -- today at midnight
SELECT TRUNC(SYSDATE, 'MM') FROM DUAL;    -- first of this month
SELECT TRUNC(SYSDATE, 'YYYY') FROM DUAL;  -- first of this year
SELECT TRUNC(SYSDATE, 'IW') FROM DUAL;    -- start of ISO week

-- Group by month:
SELECT TRUNC(created_at, 'MM') AS month,
  SUM(amount) AS revenue
FROM orders
GROUP BY TRUNC(created_at, 'MM');

Skip the Syntax Lookup

Instead of memorizing DATE_TRUNC / TRUNC / DATEFROMPARTS syntax for each database, describe what you need in plain English and let AI2SQL generate the correct query.

Try AI2SQL Free

No credit card required

Frequently Asked Questions

How do I truncate a date to the start of the month?

PostgreSQL: DATE_TRUNC('month', date). MySQL: DATE_FORMAT(date, '%Y-%m-01'). SQL Server: DATEFROMPARTS(YEAR, MONTH, 1) or DATETRUNC (2022+). Oracle: TRUNC(date, 'MM').

Why use DATE_TRUNC for grouping?

DATE_TRUNC ensures all rows in the same month/week/year get the same value, making GROUP BY clean and efficient.

Can AI2SQL generate time-series queries?

Yes. Say 'show monthly revenue for the last 12 months' and AI2SQL generates the correct date truncation and grouping.

Generate SQL from Plain English

Stop looking up syntax. Describe what you need and AI2SQL writes the query.

Try AI2SQL Free

No credit card required