

# Truncation and bucketing functions

Use these functions to round a date or time value down to the start of a period, such as a month, an hour, or a 15-minute bucket. Use the result in `GROUP BY` to get a total for each period. Another function returns the last day of a date's month.

## `date_trunc` function
<a name="athena-duckdb-functions-date-time-date-trunc"></a>

Returns `value` with every part smaller than `part` set to its lowest value. For example, `'month'` returns midnight on the first day of the month, and `'hour'` sets the minutes, seconds, and fractions of a second to zero. With `'week'`, the result is the Monday of the week. If `value` is `NULL`, the result is `NULL`.

For a `DATE`, the result is a `TIMESTAMP`, not a `DATE`, so cast the result when you need a date. For an `INTERVAL`, the parts smaller than `part` are removed. `datetrunc` takes the same arguments and returns the same results.

### Syntax
<a name="athena-duckdb-functions-date-time-date-trunc-syntax"></a>

```sql
date_trunc({{part}}, {{value}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-date-trunc-arguments"></a>

The `date_trunc` function takes the following arguments:

**{{part}}**  
The name of the unit to truncate to, as a `VARCHAR` value, such as `'year'`, `'quarter'`, `'month'`, `'week'`, `'day'`, `'hour'`, `'minute'`, or `'second'`. Case is ignored. For the full list of part names, see [Date and time functions and operators in DuckDB](duckdb-functions-date-time.md).

**{{value}}**  
The value to truncate, as a `DATE`, `TIMESTAMP`, `TIMESTAMP WITH TIME ZONE`, or `INTERVAL` value. `TIME` values are not supported.

### Return type
<a name="athena-duckdb-functions-date-time-date-trunc-return-type"></a>

`TIMESTAMP` for a `DATE` or a `TIMESTAMP`, `TIMESTAMP WITH TIME ZONE` for a `TIMESTAMP WITH TIME ZONE`, and `INTERVAL` for an `INTERVAL`.

### Examples
<a name="athena-duckdb-functions-date-time-date-trunc-examples"></a>

The following SQL query truncates a `TIMESTAMP WITH TIME ZONE` value to the hour. The result is shown in UTC, with the offset `+00`.

```sql
SELECT
    date_trunc(
        'hour', TIMESTAMPTZ '1992-09-20 20:38:40'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1992-09-20 20:00:00+00` | 

The following SQL query truncates a date to the month. The result is a `TIMESTAMP`, and the second column casts it back to `DATE`.

```sql
SELECT
    date_trunc('month', DATE '2026-11-18') AS truncated,
    CAST(
        date_trunc('month', DATE '2026-11-18') AS DATE
    ) AS as_date
```

The query returns the following result.


| truncated | as\_date | 
| --- | --- | 
| `2026-11-01 00:00:00` | `2026-11-01` | 

The following SQL query truncates a Sunday, November 22, 2026, to the week. Weeks start on Monday, so the result is November 16.

```sql
SELECT date_trunc('week', DATE '2026-11-22') AS result
```

The query returns the following result.


| result | 
| --- | 
| `2026-11-16 00:00:00` | 

The following SQL query truncates an interval to the hour.

```sql
SELECT
    date_trunc(
        'hour', INTERVAL '1 day 04:05:06'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1 day 04:00:00` | 

### Considerations
<a name="athena-duckdb-functions-date-time-date-trunc-considerations"></a>

A `TIMESTAMP WITH TIME ZONE` value is truncated in UTC. For example, `date_trunc('day', TIMESTAMPTZ '2026-11-18 22:00:00-05')` returns `2026-11-19 00:00:00+00`, because 22:00 at UTC-5 is 03:00 on November 19 in UTC.

`'decade'`, `'century'`, and `'millennium'` truncate to a year that ends in 0, 00, or 000. For example, `date_trunc('century', DATE '2026-11-18')` returns `2000-01-01 00:00:00`, although the `century` function counts the 21st century from 2001.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-date-time-date-trunc-differences-from-engine-version-3"></a>

In Athena engine version 3, `date_trunc` of a `DATE` returns a `DATE`. In DuckDB, it returns a `TIMESTAMP`. To get a `DATE`, use `CAST(date_trunc({{unit}}, {{d}}) AS DATE)`.

## `last_day` function
<a name="athena-duckdb-functions-date-time-last-day"></a>

Returns the `DATE` of the final day in the month that contains `ts`. The result accounts for month lengths and leap years, so February 2024 ends on the 29th. The time of day in a timestamp is dropped.

A `TIMESTAMP WITH TIME ZONE` value is read in the session time zone, which is UTC. As a result, the month can differ from the month in the time zone of the original value. If `ts` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-date-time-last-day-syntax"></a>

```sql
last_day({{ts}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-last-day-arguments"></a>

The `last_day` function takes the following argument:

**{{ts}}**  
A value in the month to find the last day of, as a `DATE`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE` value.

### Return type
<a name="athena-duckdb-functions-date-time-last-day-return-type"></a>

`DATE`, for every input type.

### Examples
<a name="athena-duckdb-functions-date-time-last-day-examples"></a>

The following SQL query returns the last day of the month of a timestamp. The result is a `DATE`.

```sql
SELECT
    last_day(
        TIMESTAMP '1992-03-22 01:02:03.1234'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1992-03-31` | 

The following SQL query returns the last day of February in a leap year and in a common year.

```sql
SELECT
    last_day(DATE '2024-02-10') AS leap_year,
    last_day(DATE '2023-02-10') AS common_year
```

The query returns the following result.


| leap\_year | common\_year | 
| --- | --- | 
| `2024-02-29` | `2023-02-28` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-date-time-last-day-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `last_day_of_month`. DuckDB has no function with that name, so replace `last_day_of_month({{x}})` with `last_day({{x}})`.

## `time_bucket` function
<a name="athena-duckdb-functions-date-time-time-bucket"></a>

Divides time into consecutive buckets that are each `bucket_width` long, and returns the start of the bucket that contains `value`. Use it to group rows by intervals such as 15 minutes, 1 day, or 1 week. If `value` is `NULL`, the result is `NULL`.

By default, buckets of days, hours, minutes, or seconds are counted from Monday, January 3, 2000, at midnight, so weekly buckets start on Mondays. Buckets of months or years are counted from January 1, 2000, so 3-month buckets match calendar quarters. To move the boundaries, pass an `origin` that starts a bucket, or an `offset` that shifts every boundary. For a `TIMESTAMP WITH TIME ZONE` value, you can instead pass a `timezone`, and the buckets follow the local time in that zone.

### Syntax
<a name="athena-duckdb-functions-date-time-time-bucket-syntax"></a>

```sql
time_bucket({{bucket_width}}, {{value}} [, {{origin}} ])
time_bucket({{bucket_width}}, {{value}}, {{offset}})
time_bucket({{bucket_width}}, {{value}}, {{timezone}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-time-bucket-arguments"></a>

The `time_bucket` function takes the following arguments:

**{{bucket\_width}}**  
The length of each bucket, as an `INTERVAL`, such as `INTERVAL '15 minutes'`. It must be greater than zero. It can be a number of months and years, or a number of days and smaller units, but not both.

**{{value}}**  
The value to place in a bucket, as a `DATE`, `TIME`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE` value.

**{{origin}}**  
The start of a bucket, as a value of the same type as `value`. Every bucket boundary is a whole number of `bucket_width` intervals from it. This argument is optional.

**{{offset}}**  
The amount to shift every bucket boundary from the default origin, as an `INTERVAL`. For example, with daily buckets, `INTERVAL '6 hours'` starts each day at 06:00. This argument is optional.

**{{timezone}}**  
The time zone whose local time the buckets follow, as a `VARCHAR` time zone name, such as `'America/New_York'`. The default is `UTC`. It applies to `TIMESTAMP WITH TIME ZONE` values only. This argument is optional.

### Return type
<a name="athena-duckdb-functions-date-time-time-bucket-return-type"></a>

The same type as `value`: `DATE`, `TIME`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE`.

### Examples
<a name="athena-duckdb-functions-date-time-time-bucket-examples"></a>

The following SQL query returns the start of the 15-minute bucket that contains a timestamp.

```sql
SELECT
    time_bucket(
        INTERVAL '15 minutes',
        TIMESTAMP '2024-05-01 10:37:12'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2024-05-01 10:30:00` | 

The following SQL query counts events in 15-minute buckets. A timestamp exactly on a boundary, such as 10:15:00, starts a new bucket.

```sql
SELECT
    time_bucket(
        INTERVAL '15 minutes', ts
    ) AS bucket_start,
    count(*) AS events
FROM (VALUES
    (TIMESTAMP '2024-05-01 10:02:00'),
    (TIMESTAMP '2024-05-01 10:14:59'),
    (TIMESTAMP '2024-05-01 10:15:00'),
    (TIMESTAMP '2024-05-01 10:44:30')
) AS v(ts)
GROUP BY bucket_start
ORDER BY bucket_start
```

The query returns the following results.


| bucket\_start | events | 
| --- | --- | 
| `2024-05-01 10:00:00` | `2` | 
| `2024-05-01 10:15:00` | `1` | 
| `2024-05-01 10:30:00` | `1` | 

The following SQL query returns the start of the week that contains a date. With the default origin, weeks start on Monday, and a `DATE` input returns a `DATE`.

```sql
SELECT
    time_bucket(
        INTERVAL '1 week', DATE '2024-05-01'
    ) AS week_start,
    dayname(
        time_bucket(
            INTERVAL '1 week', DATE '2024-05-01'
        )
    ) AS week_start_day
```

The query returns the following result.


| week\_start | week\_start\_day | 
| --- | --- | 
| `2024-04-29` | `Monday` | 

The following SQL query uses daily buckets in the `America/New_York` time zone. The input time, 03:00 on May 1 in UTC, is 23:00 on April 30 in New York. The function returns the start of that New York day, April 30, which is 04:00 in UTC.

```sql
SELECT
    time_bucket(
        INTERVAL '1 day',
        TIMESTAMPTZ '2024-05-01 03:00:00+00',
        'America/New_York'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2024-04-30 04:00:00+00` | 

### Considerations
<a name="athena-duckdb-functions-date-time-time-bucket-considerations"></a>

A `bucket_width` of zero or less makes the query fail. So does an interval that mixes months with days or smaller units, such as `INTERVAL '1 month 1 day'`.

To start weekly buckets on Sunday, pass a Sunday as `origin`, for example `time_bucket(INTERVAL '1 week', {{value}}, DATE '2000-01-02')`.

A `DATE` is treated as midnight at the start of that day. When the bucket that contains that midnight starts on an earlier day, the function returns that earlier day. This can happen with an `offset`, such as `INTERVAL '6 hours'` with daily buckets. It can also happen with a `bucket_width` that is not a whole number of days, such as `INTERVAL '5 hours'`. If you pass a `TIMESTAMP` as `origin`, the result is a `TIMESTAMP` instead of a `DATE`.