View a markdown version of this page

Interval functions - Amazon Athena

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 INTERVAL value.

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

1 month

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

36:00:00

1 day 12:00:00

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

2024-03-01 00:00:00

2024-02-29 00:00:00

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

-2 months 15 days

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 INTEGER or BIGINT value.

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

500 years

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

2100-02-28

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 INTEGER or BIGINT value from -2147483648 to 2147483647.

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

5 days

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

2024-03-01 00:00:00

2024-03-01

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

45 days

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 INTEGER or BIGINT value.

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

50 years

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

2034-02-28

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 BIGINT value.

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

05:00:00

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

36:00:00

true

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

2024-05-02 03:00:00

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 BIGINT value.

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

00:00:00.000005

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

2024-05-01 10:00:00.00025

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 INTEGER or BIGINT value.

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

1000 years

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

1024-05-01

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 DOUBLE value. 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

00:00:00.0055

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

00:00:01.5

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

2024-05-01 10:00:01.5

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 BIGINT value.

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

00:05:00

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

01:30:00

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

2024-05-01 08:30:00

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 INTEGER or BIGINT value.

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

5 months

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

1 year 2 months

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

2026-02-28 00:00:00

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(integer) returns the same interval as to_months(3 * integer).

Syntax

to_quarters(integer)

Arguments

The to_quarters function takes the following argument:

integer

The number of quarters, as an INTEGER or BIGINT value.

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

1 year 3 months

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

2026-04-15 00:00:00

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

true

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 DOUBLE value. 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

00:00:05.5

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

00:01:36

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

25:00:00

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

2026-01-01 00:01:30

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(n) AS VARCHAR), which returns text such as 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 INTEGER or BIGINT value.

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

35 days

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

2026-12-02 00:00:00

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 INTEGER or BIGINT value.

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

5 years

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

2025-02-28 00:00:00

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.