---
title: time_bucket function
description: The time\_bucket function in Databricks SQL and Databricks Runtime returns the start of a fixed-width time bucket for a timestamp, aligned to an origin.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/time_bucket
---

# `time_bucket` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 19 and above

Returns the start of the fixed-width time bucket that a timestamp falls into, aligned to an origin.

## Syntax

```
time_bucket(bucketSize, ts [, origin])
```

## Arguments

* `bucketSize`: An `INTERVAL` constant expression that specifies the size of each bucket. Supports day-time and year-month intervals. Must be positive and non-zero.
* `ts`: A `TIMESTAMP` or `TIMESTAMP_NTZ` expression to bucket.
* `origin`: An optional `TIMESTAMP` or `TIMESTAMP_NTZ` constant expression that defines the alignment anchor of the bucket grid. Defaults to `1970-01-01 00:00:00`. Must be the same type as `ts`.

## Returns

A value of the same type as `ts` (`TIMESTAMP` or `TIMESTAMP_NTZ`). The value is the start of the half-open bucket `[start, start + bucketSize)` that contains `ts`. The start is always less than or equal to `ts`, and the next bucket boundary (`start + bucketSize`) is strictly greater than `ts`.

## Notes

* `time_bucket` divides the time axis into consecutive, non-overlapping intervals of size `bucketSize`, anchored at `origin`. It returns the start of the interval that contains `ts`.
* `origin` does not need to precede `ts`. It only defines the alignment of the bucket grid. The grid extends infinitely in both directions from `origin`.
* For `TIMESTAMP_NTZ`, `time_bucket` buckets in UTC. For `TIMESTAMP`, year-month interval buckets and the calendar-day components of day-time interval buckets align to the session time zone.
* If any argument is `NULL`, the result is `NULL`.
* Use [`date_trunc`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/date_trunc.md) for calendar-aligned boundaries such as grouping by calendar month. Use `time_bucket` when the bucket width or alignment is user-defined.

## Common error conditions

* [DATATYPE\_MISMATCH.NON\_FOLDABLE\_INPUT](https://docs.databricks.com/gcp/en/error-messages/datatype-mismatch-error-class.md#non_foldable_input)
* [DATATYPE\_MISMATCH.UNEXPECTED\_INPUT\_TYPE](https://docs.databricks.com/gcp/en/error-messages/datatype-mismatch-error-class.md#unexpected_input_type)
* [DATATYPE\_MISMATCH.VALUE\_OUT\_OF\_RANGE](https://docs.databricks.com/gcp/en/error-messages/datatype-mismatch-error-class.md#value_out_of_range)

## Examples

```sql
-- 15-minute buckets (default origin at epoch)
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP '2024-01-01 11:27:00');
 2024-01-01 11:15:00

-- 1-hour buckets
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00');
 2024-01-01 11:00:00

-- Custom origin shifts bucket alignment to :05 past the hour
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00', TIMESTAMP '1970-01-01 00:05:00');
 2024-01-01 11:05:00

-- TIMESTAMP_NTZ variant
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP_NTZ '2024-01-01 11:27:00');
 2024-01-01 11:15:00

-- ts exactly on a bucket boundary returns itself
> SELECT time_bucket(INTERVAL '15' MINUTE, TIMESTAMP '2024-01-01 11:15:00');
 2024-01-01 11:15:00

-- Origin after ts: grid extends backward
> SELECT time_bucket(INTERVAL '1' HOUR, TIMESTAMP '2024-01-01 11:27:00', TIMESTAMP '2025-01-01 00:30:00');
 2024-01-01 10:30:00

-- Monthly buckets (default epoch origin: 1st of month)
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-15 11:27:00');
 2024-03-01 00:00:00

-- Quarterly buckets
> SELECT time_bucket(INTERVAL '3' MONTH, TIMESTAMP '2024-05-15 10:00:00');
 2024-04-01 00:00:00

-- Monthly buckets with origin on the 15th
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-20 09:00:00', TIMESTAMP '1970-01-15 00:00:00');
 2024-03-15 00:00:00

-- Origin on 31st: day clamping in short months
> SELECT time_bucket(INTERVAL '1' MONTH, TIMESTAMP '2024-03-01 12:00:00', TIMESTAMP '1970-01-31 00:00:00');
 2024-02-29 00:00:00

-- Zero bucket width is rejected
> SELECT time_bucket(INTERVAL '0' SECOND, TIMESTAMP '2024-01-01 11:00:00');
 [DATATYPE_MISMATCH.VALUE_OUT_OF_RANGE] The bucketSize must be between (0, inf) (current value = INTERVAL '00' SECOND).
```

## Related functions

* [`date_trunc` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/date_trunc.md)
* [`TIMESTAMP` type](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/timestamp-type.md)
* [`TIMESTAMP_NTZ` type](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/timestamp-ntz-type.md)
