> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for the Interval special data type

# Interval

The family of data types representing time and date intervals. The resulting types of the [INTERVAL](/reference/operators/index#interval) operator.

Structure:

* Time interval as an unsigned integer value.
* Type of an interval.

Supported interval types:

* `NANOSECOND`
* `MICROSECOND`
* `MILLISECOND`
* `SECOND`
* `MINUTE`
* `HOUR`
* `DAY`
* `WEEK`
* `MONTH`
* `QUARTER`
* `YEAR`

For each interval type, there is a separate data type. For example, the `DAY` interval corresponds to the `IntervalDay` data type:

```sql theme={null}
SELECT toTypeName(INTERVAL 4 DAY)
```

```text theme={null}
┌─toTypeName(toIntervalDay(4))─┐
│ IntervalDay                  │
└──────────────────────────────┘
```

<h2 id="usage-remarks">
  Usage Remarks
</h2>

You can use `Interval`-type values in arithmetical operations with [Date](/reference/data-types/date) and [DateTime](/reference/data-types/datetime)-type values. For example, you can add 4 days to the current time:

```sql theme={null}
SELECT now() AS current_date_time, current_date_time + INTERVAL 4 DAY
```

```text theme={null}
┌───current_date_time─┬─plus(now(), toIntervalDay(4))─┐
│ 2019-10-23 10:58:45 │           2019-10-27 10:58:45 │
└─────────────────────┴───────────────────────────────┘
```

Also it is possible to use multiple intervals simultaneously:

```sql theme={null}
SELECT now() AS current_date_time, current_date_time + (INTERVAL 4 DAY + INTERVAL 3 HOUR)
```

```text theme={null}
┌───current_date_time─┬─plus(current_date_time, plus(toIntervalDay(4), toIntervalHour(3)))─┐
│ 2024-08-08 18:31:39 │                                                2024-08-12 21:31:39 │
└─────────────────────┴────────────────────────────────────────────────────────────────────┘
```

And to compare values with different intervals:

```sql theme={null}
SELECT toIntervalMicrosecond(179999999) < toIntervalMinute(3);
```

```text theme={null}
┌─less(toIntervalMicrosecond(179999999), toIntervalMinute(3))─┐
│                                                           1 │
└─────────────────────────────────────────────────────────────┘
```

```sql theme={null}
SELECT toIntervalMicrosecond(3600000000) = toIntervalHour(1);
```

```text theme={null}
┌─equals(toIntervalMicrosecond(3600000000), toIntervalHour(1))─┐
│                                                            1 │
└──────────────────────────────────────────────────────────────┘
```

<h2 id="aggregation">
  Aggregation
</h2>

`sum`, `avg`, `min`, `max` and `any` over an `Interval` column return an interval of the same type.
`sum` adds up the number of units, and `avg` averages them and rounds the result to a whole number
of them:

```sql theme={null}
SELECT
    sum(duration) AS total_duration,
    avg(duration) AS avg_duration,
    toTypeName(total_duration)
FROM VALUES('duration IntervalMillisecond',
    toIntervalMillisecond(100), toIntervalMillisecond(200), toIntervalMillisecond(300))
```

```text theme={null}
┌─total_duration─┬─avg_duration─┬─toTypeName(total_duration)─┐
│            600 │          200 │ IntervalMillisecond        │
└────────────────┴──────────────┴────────────────────────────┘
```

Values of different units are brought to their common unit before they are aggregated, so
summing a minute and a second gives 61 seconds. `Month`, `Quarter` and `Year` have no common
unit with the smaller ones - a month is not a fixed number of days - and cannot be mixed with
them at all.

<h2 id="mixed-type-intervals">
  Mixed-type Intervals
</h2>

Intervals of mixed type, e.g. multiple hours and multiple minutes, can be created using `INTERVAL 'value' <from_kind> TO <to_kind>` syntax.
The result is a tuple of two or more intervals.

Supported combinations:

| Syntax | String format | Example |
| - | - | - |
| `YEAR TO MONTH` | `Y-M` | `INTERVAL '2-6' YEAR TO MONTH` |
| `DAY TO HOUR` | `D H` | `INTERVAL '5 12' DAY TO HOUR` |
| `DAY TO MINUTE` | `D H:M` | `INTERVAL '5 12:30' DAY TO MINUTE` |
| `DAY TO SECOND` | `D H:M:S` | `INTERVAL '5 12:30:45' DAY TO SECOND` |
| `HOUR TO MINUTE` | `H:M` | `INTERVAL '1:30' HOUR TO MINUTE` |
| `HOUR TO SECOND` | `H:M:S` | `INTERVAL '1:30:45' HOUR TO SECOND` |
| `MINUTE TO SECOND` | `M:S` | `INTERVAL '5:30' MINUTE TO SECOND` |

Non-leading fields are validated per the SQL standard: `MONTH` 0-11, `HOUR` 0-23, `MINUTE` 0-59, `SECOND` 0-59.

```sql theme={null}
SELECT INTERVAL '1:30' HOUR TO MINUTE;
```

```text theme={null}
┌─(toIntervalHour(1), toIntervalMinute(30))─┐
│ (1,30)                                     │
└────────────────────────────────────────────┘
```

An optional leading `+` or `-` sign applies to all components:

```sql theme={null}
SELECT INTERVAL '+1:30' HOUR TO MINUTE;
-- this is equivalent to:
-- SELECT INTERVAL '1:30' HOUR TO MINUTE;
```

```text theme={null}
┌─(toIntervalHour(1), toIntervalMinute(30))─┐
│ (1,30)                                     │
└────────────────────────────────────────────┘
```

<h2 id="see-also">
  See Also
</h2>

* [INTERVAL](/reference/operators/index#interval) operator
* [toInterval](/reference/functions/regular-functions/type-conversion-functions#toIntervalYear) type conversion functions
* [avg](/reference/functions/aggregate-functions/avg) aggregate function
* [sum](/reference/functions/aggregate-functions/sum) aggregate function
