Interval functions
Use these functions to build an INTERVAL from a number of units, such as days, hours, or months, and to rewrite an interval in larger units. Add an interval to a date or timestamp with the + (addition) operator, or subtract it with the - (subtraction and negation) operator.
normalized_interval function
Returns an INTERVAL that is equal to interval, with its parts rewritten. Each whole 24 hours in the time part moves to the days part, and then each whole 30 days moves to the months part. For example, INTERVAL '36 hours' becomes 1 day 12:00:00, and INTERVAL '45 days' becomes 1 month 15 days.
If interval is NULL, the result is NULL.
Syntax
normalized_interval(interval)
Arguments
The normalized_interval function takes the following argument:
interval-
The interval to rewrite, as an
INTERVALvalue.
Return type
INTERVAL
Examples
The following SQL query rewrites an interval of 30 days as 1 month.
SELECT normalized_interval(INTERVAL '30 days') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows an interval of 36 hours before and after normalization.
SELECT INTERVAL '36 hours' AS original, normalized_interval( INTERVAL '36 hours' ) AS normalized
The query returns the following result.
| original | normalized |
|---|---|
|
|
The following SQL query adds 30 days to January 31, 2024, first as days and then as the normalized interval of 1 month. The two results differ by one day.
SELECT DATE '2024-01-31' + INTERVAL '30 days' AS days_added, DATE '2024-01-31' + normalized_interval( INTERVAL '30 days' ) AS normalized_added
The query returns the following result.
| days_added | normalized_added |
|---|---|
|
|
The following SQL query normalizes a negative interval. The months part is negative and the days part is positive.
SELECT normalized_interval(INTERVAL '-45 days') AS result
The query returns the following result.
| result |
|---|
|
Considerations
Comparisons already treat 30 days as equal to 1 month and 24 hours as equal to 1 day, so the result compares equal to the input. Date arithmetic does not treat them as equal. Adding 1 month moves to the same day of the next month, or to the last day of the next month when that month has no such day. Adding 30 days moves exactly 30 days. Adding a normalized interval to a date can give a different result from adding the original interval.
For a negative interval, the parts of the result can have different signs. For example, INTERVAL '-45 days' becomes -2 months 15 days, which is still equal to 45 days back.
to_centuries function
Returns an INTERVAL of integer centuries, where each century is 100 years. Add the result to a date or timestamp to move it forward by that many centuries, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown in years, so to_centuries(5) is shown as 500 years. To build intervals in other units, use to_millennia, to_decades, or to_years.
Syntax
to_centuries(integer)
Arguments
The to_centuries function takes the following argument:
integer-
The number of centuries, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five centuries.
SELECT to_centuries(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds one century to February 29, 2000. The year 2100 is not a leap year, so the result is February 28.
SELECT CAST( DATE '2000-02-29' + to_centuries(1) AS DATE ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_centuries(2.5), fails. Cast the value to INTEGER first. The cast rounds to the nearest whole number. A DECIMAL value exactly halfway between two whole numbers, such as 2.5, rounds away from zero, to 3. A DOUBLE value exactly halfway rounds to the even number, so CAST(2.5e0 AS INTEGER) returns 2.
The interval is stored as a number of months. When you add it to February 29 and the target year is not a leap year, the result is February 28.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result.
to_days function
Returns an INTERVAL of integer days. Add the result to a date or timestamp to move it forward by that many days, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The days are not converted to months, so to_days(45) is shown as 45 days. To build intervals in other units, use to_weeks, to_hours, or to_months.
Syntax
to_days(integer)
Arguments
The to_days function takes the following argument:
integer-
The number of days, as an
INTEGERorBIGINTvalue from-2147483648to2147483647.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five days.
SELECT to_days(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds 30 days to a date. The sum is a TIMESTAMP, so the second column casts it to DATE.
SELECT DATE '2024-01-31' + to_days(30) AS plus_days, CAST( DATE '2024-01-31' + to_days(30) AS DATE ) AS plus_days_date
The query returns the following result.
| plus_days | plus_days_date |
|---|---|
|
|
The following SQL query shows that 45 days stays 45 days. It is not converted to months.
SELECT to_days(45) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_days(2.5), fails. Cast the value to INTEGER first. The cast rounds to the nearest whole number. A DECIMAL value exactly halfway between two whole numbers, such as 2.5, rounds away from zero, to 3. A DOUBLE value exactly halfway rounds to the even number, so CAST(2.5e0 AS INTEGER) returns 2.
A value outside the range of a 32-bit integer makes the query fail.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result, as in CAST(DATE '2024-01-31' + to_days(30) AS DATE).
to_decades function
Returns an INTERVAL of integer decades, where each decade is 10 years. Add the result to a date or timestamp to move it forward by that many decades, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown in years, so to_decades(5) is shown as 50 years. To build intervals in other units, use the to_centuries function or the to_years function.
Syntax
to_decades(integer)
Arguments
The to_decades function takes the following argument:
integer-
The number of decades, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five decades.
SELECT to_decades(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds one decade to February 29, 2024. The year 2034 is not a leap year, so the result is February 28.
SELECT CAST( DATE '2024-02-29' + to_decades(1) AS DATE ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_decades(2.5), fails. Cast the value to INTEGER first. The cast rounds to the nearest whole number. A DECIMAL value exactly halfway between two whole numbers, such as 2.5, rounds away from zero, to 3. A DOUBLE value exactly halfway rounds to the even number, so CAST(2.5e0 AS INTEGER) returns 2.
The interval is stored as a number of months. When you add it to February 29 and the target year is not a leap year, the result is February 28.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result.
to_hours function
Returns an INTERVAL of integer hours. Add the result to a time or timestamp to move it forward by that many hours, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The hours are not converted to days, so to_hours(36) is shown as 36:00:00. The interval still compares as equal to INTERVAL '1 day 12 hours'. To build intervals in other units, use to_days, to_minutes, or to_seconds.
Syntax
to_hours(integer)
Arguments
The to_hours function takes the following argument:
integer-
The number of hours, as a
BIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five hours.
SELECT to_hours(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that to_hours(36) displays as 36 hours, not as 1 day and 12 hours. The value still compares as equal to INTERVAL '1 day 12 hours'.
SELECT to_hours(36) AS result, to_hours(36) = INTERVAL '1 day 12 hours' AS is_equal
The query returns the following result.
| result | is_equal |
|---|---|
|
|
The following SQL query adds five hours to a timestamp late in the day, which moves it to the next day.
SELECT TIMESTAMP '2024-05-01 22:00:00' + to_hours(5) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_hours(1.5), fails. For a fraction of an hour, use a smaller unit, such as to_minutes(90).
to_microseconds function
Returns an INTERVAL of integer microseconds. A microsecond is the smallest unit that an interval can hold. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown as hours, minutes, and seconds with a fraction, so to_microseconds(1500000) is shown as 00:00:01.5. To build intervals in other units, use the to_milliseconds function or the to_seconds function.
Syntax
to_microseconds(integer)
Arguments
The to_microseconds function takes the following argument:
integer-
The number of microseconds, as a
BIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five microseconds.
SELECT to_microseconds(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds 250 microseconds to a timestamp.
SELECT TIMESTAMP '2024-05-01 10:00:00' + to_microseconds(250) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_microseconds(1.5), fails.
to_millennia function
Returns an INTERVAL of integer millennia, where each millennium is 1,000 years. Add the result to a date or timestamp to move it forward by that many millennia, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown in years, so to_millennia(1) is shown as 1000 years. To build intervals in other units, use the to_centuries function or the to_years function.
Syntax
to_millennia(integer)
Arguments
The to_millennia function takes the following argument:
integer-
The number of millennia, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of one millennium.
SELECT to_millennia(1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query subtracts one millennium from a date and casts the result to DATE.
SELECT CAST( DATE '2024-05-01' - to_millennia(1) AS DATE ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_millennia(1.5), fails. Cast the value to INTEGER first. The cast rounds to the nearest whole number. A DECIMAL value exactly halfway between two whole numbers, such as 2.5, rounds away from zero, to 3. A DOUBLE value exactly halfway rounds to the even number, so CAST(2.5e0 AS INTEGER) returns 2.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result.
to_milliseconds function
Returns an INTERVAL of double milliseconds. Unlike to_microseconds, to_minutes, and to_hours, this function accepts a fraction, so to_milliseconds(5.5) is 5,500 microseconds. A negative value gives a negative interval. If double is NULL, the result is NULL.
The interval is shown as hours, minutes, and seconds with a fraction, so to_milliseconds(1500) is shown as 00:00:01.5. To build intervals in other units, use the to_microseconds function or the to_seconds function.
Syntax
to_milliseconds(double)
Arguments
The to_milliseconds function takes the following argument:
double-
The number of milliseconds, as a
DOUBLEvalue. It can be negative and can have a fractional part.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5.5 milliseconds.
SELECT to_milliseconds(5.5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns an interval of 1,500 milliseconds, which is shown as 1.5 seconds.
SELECT to_milliseconds(1500) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds 1,500 milliseconds to a timestamp.
SELECT TIMESTAMP '2024-05-01 10:00:00' + to_milliseconds(1500) AS result
The query returns the following result.
| result |
|---|
|
Considerations
An interval holds whole microseconds, so the result is rounded to the nearest microsecond. For example, to_milliseconds(0.0004) is an interval of zero.
to_minutes function
Returns an INTERVAL of integer minutes. Add the result to a time or timestamp to move it forward by that many minutes, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown as hours, minutes, and seconds, so to_minutes(90) is shown as 01:30:00. To build intervals in other units, use the to_hours function or the to_seconds function.
Syntax
to_minutes(integer)
Arguments
The to_minutes function takes the following argument:
integer-
The number of minutes, as a
BIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of five minutes.
SELECT to_minutes(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns an interval of 90 minutes, which is shown as 1 hour and 30 minutes.
SELECT to_minutes(90) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query subtracts 90 minutes from a timestamp.
SELECT TIMESTAMP '2024-05-01 10:00:00' - to_minutes(90) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_minutes(1.5), fails. For a fraction of a minute, use the to_seconds function, which accepts a fraction.
to_months function
Returns an INTERVAL of integer months. Add the result to a date or timestamp to move it forward by that many months, or subtract it to move back. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
The interval is shown in years and months, so 14 months is shown as 1 year 2 months. To build intervals in other units, use functions such as to_years, to_quarters, to_weeks, to_days, to_hours, to_minutes, or to_seconds.
Syntax
to_months(integer)
Arguments
The to_months function takes the following argument:
integer-
The number of months, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5 months.
SELECT to_months(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns an interval of 14 months, which is shown in years and months.
SELECT to_months(14) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds one month to January 31. February has no 31st day, so the result is the last day of February. The result is a TIMESTAMP, not a DATE.
SELECT DATE '2026-01-31' + to_months(1) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_months(2.5), fails. Cast the value to INTEGER first. The cast rounds to the nearest whole number.
When you add months to a date and the day does not exist in the target month, the result is the last day of that month. For example, January 31 plus one month is February 28 in a year that is not a leap year.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result, as in CAST(DATE '2026-01-31' + to_months(1) AS DATE).
to_quarters function
Returns an INTERVAL of integer quarters. Each quarter is three months. The interval is stored as months and shown in years and months, so to_quarters(5) is shown as 1 year 3 months. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
to_quarters( returns the same interval as integer)to_months(3 * .integer)
Syntax
to_quarters(integer)
Arguments
The to_quarters function takes the following argument:
integer-
The number of quarters, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5 quarters, which is 15 months and is shown in years and months.
SELECT to_quarters(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds one quarter to a date.
SELECT DATE '2026-01-15' + to_quarters(1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that one quarter equals three months.
SELECT to_quarters(1) = to_months(3) AS is_equal
The query returns the following result.
| is_equal |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_quarters(2.5), fails.
When you add quarters to a date and the day does not exist in the target month, the result is the last day of that month, as with to_months. Adding an interval to a DATE returns a TIMESTAMP.
to_seconds function
Returns an INTERVAL of double seconds. The value can have a fractional part, so you can also use to_seconds for intervals of milliseconds or microseconds. A negative value gives a negative interval. If double is NULL, the result is NULL.
The interval is shown as hours, minutes, and seconds, in the form HH:MM:SS, followed by any fraction of a second.
Syntax
to_seconds(double)
Arguments
The to_seconds function takes the following argument:
double-
The number of seconds, as a
DOUBLEvalue. It can be negative and can have a fractional part.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5.5 seconds.
SELECT to_seconds(5.5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query converts 96 seconds to an interval and casts it to text.
SELECT CAST(to_seconds(96) AS VARCHAR) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that 90,000 seconds stay in hours and are not converted to days.
SELECT to_seconds(90000) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds 90 seconds to a timestamp.
SELECT TIMESTAMP '2026-01-01 00:00:00' + to_seconds(90) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The interval keeps the seconds as a span of time and does not convert them to days, so to_seconds(90000) is shown as 25:00:00. When you compare intervals, 24 hours equals one day, so to_seconds(86400) = INTERVAL 1 DAY is true.
The interval has microsecond precision. A fraction smaller than one microsecond is rounded to the nearest microsecond.
Differences from Athena engine version 3
Athena engine version 3 has human_readable_seconds, which formats a number of seconds as text such as 1 minute, 36 seconds. DuckDB does not have this function. The closest replacement is CAST(to_seconds(, which returns text such as n) AS VARCHAR)00:01:36.
to_weeks function
Returns an INTERVAL of integer weeks. Each week is stored as 7 days, so the interval is shown in days: to_weeks(5) is shown as 35 days. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
Syntax
to_weeks(integer)
Arguments
The to_weeks function takes the following argument:
integer-
The number of weeks, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5 weeks, which is shown as 35 days.
SELECT to_weeks(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds two weeks to a date. The result is a TIMESTAMP.
SELECT DATE '2026-11-18' + to_weeks(2) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_weeks(2.5), fails.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result to DATE.
to_years function
Returns an INTERVAL of integer years. The interval is stored as 12 months for each year. A negative integer gives a negative interval. If integer is NULL, the result is NULL.
Syntax
to_years(integer)
Arguments
The to_years function takes the following argument:
integer-
The number of years, as an
INTEGERorBIGINTvalue.
Return type
INTERVAL
Examples
The following SQL query returns an interval of 5 years.
SELECT to_years(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query adds one year to February 29, 2024. The year 2025 is not a leap year, so the result is February 28, 2025.
SELECT DATE '2024-02-29' + to_years(1) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The argument must be a whole number. A call with a decimal value, such as to_years(2.5), fails.
When you add years to February 29 and the target year is not a leap year, the result is February 28.
Adding an interval to a DATE returns a TIMESTAMP. To keep a DATE, cast the result to DATE.