Menu

SQL Server DATE_BUCKET() Function

DATE_BUCKET(datepart, number, date [, origin]) returns the start of the fixed-width bucket containing a date/time value. SQL Server introduced DATE_BUCKET() in SQL Server 2022 (16.x). See Microsoft’s DATE_BUCKET reference.

Syntax

DATE_BUCKET(datepart, number, date [, origin])
  • datepart and number define the bucket width, such as minute, 15 for 15-minute buckets. Use a supported date part as a literal, not a variable.
  • date must be a date/time expression such as a typed column or variable. Cast a string literal to a date/time type before passing it to DATE_BUCKET().
  • origin is optional and must resolve to the same data type as date. If omitted, SQL Server uses 1900-01-01 00:00:00.000 as the origin.
  • number must be a positive integer. A negative width is not valid.

DATE_BUCKET() returns the same data type as the date argument. It is available in SQL Server 2022 and later.

Group events into 15-minute buckets

The following query groups typed event timestamps into 15-minute windows:

SELECT DATE_BUCKET(minute, 15, EventTime) AS bucket_start,
       COUNT(*) AS event_count
FROM dbo.Events
GROUP BY DATE_BUCKET(minute, 15, EventTime)
ORDER BY bucket_start;

The default origin aligns the buckets to intervals beginning at 00, 15, 30, and 45 minutes past each hour.

Set a custom origin

Use an explicit origin when bucket boundaries must align to a particular timestamp. This example assumes EventTime is datetime2(0), matching the origin variable’s type:

DECLARE @origin datetime2(0) = '2026-01-01T00:05:00';

SELECT DATE_BUCKET(minute, 15, EventTime, @origin) AS bucket_start,
       COUNT(*) AS event_count
FROM dbo.Events
GROUP BY DATE_BUCKET(minute, 15, EventTime, @origin)
ORDER BY bucket_start;

For a bucket that starts at a period boundary, see DATETRUNC(). For counting date-part boundaries between two values, see DATEDIFF().