View a markdown version of this page

Week and weekday functions - Amazon Athena

Week and weekday functions

Use these functions to get the day of the week as a number or a name, and to number the weeks of the year. Week numbers and week-numbering years follow ISO 8601, where weeks start on Monday and week 1 contains the first Thursday of the year.

dayname function

Returns the name of the day of the week of value, in English with an initial capital letter, such as Sunday. If value is NULL, the result is NULL.

To get the day of the week as a number, use the dayofweek function or the isodow function. The dayofweek function numbers Sunday as 0, and the isodow function numbers Monday as 1 and Sunday as 7.

Syntax

dayname(value)

Arguments

The dayname function takes the following argument:

value

The value to read the day of the week from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

VARCHAR

Examples

The following SQL query returns the name of the day of the week of March 22, 1992.

SELECT dayname(TIMESTAMP '1992-03-22') AS result

The query returns the following result.

result

Sunday

The following SQL query shows that a lowercase name does not match unless you apply lower first.

SELECT dayname( DATE '2026-11-18' ) = 'wednesday' AS lowercase_match, lower( dayname(DATE '2026-11-18') ) = 'wednesday' AS lower_match

The query returns the following result.

lowercase_match lower_match

false

true

The following SQL query reads the day of a TIMESTAMP WITH TIME ZONE value. 22:00 on Wednesday, November 18, at UTC-5 is 03:00 on Thursday in UTC.

SELECT dayname( TIMESTAMPTZ '2026-11-18 22:00:00-05' ) AS result

The query returns the following result.

result

Thursday

Considerations

Comparisons of the name with a string are case-sensitive, so compare with 'Monday', not 'monday', or apply lower to the name first.

A TIMESTAMP WITH TIME ZONE value is read in UTC, so the day can differ from the day in the time zone that the value was written with.

dayofweek function

Returns the day of the week of ts, where 0 is Sunday, 1 is Monday, and 6 is Saturday. If ts is NULL, the result is NULL.

The weekday function returns the same result. For the ISO 8601 numbering, where Monday is 1 and Sunday is 7, use the isodow function. To get the name of the day, use the dayname function.

Syntax

dayofweek(ts)

Arguments

The dayofweek function takes the following argument:

ts

The value to read the day of the week from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

BIGINT

Examples

The following SQL query returns the day of the week of a timestamp. August 3, 2021, is a Tuesday, so the function returns 2.

SELECT dayofweek( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result

The query returns the following result.

result

2

The following SQL query returns the numbers for a Sunday and a Saturday. Sunday is 0, not 7.

SELECT dayofweek(DATE '2021-03-07') AS sunday, dayofweek(DATE '2021-03-13') AS saturday

The query returns the following result.

sunday saturday

0

6

Considerations

For a TIMESTAMP WITH TIME ZONE value, the day is taken in the session time zone, which is UTC.

dayofweek does not work on INTERVAL values. A call such as dayofweek(INTERVAL '3 days') fails.

Differences from Athena engine version 3

In Athena engine version 3, day_of_week returns 1 for Monday through 7 for Sunday. DuckDB does not have day_of_week, and dayofweek returns 0 for Sunday. For the Athena engine version 3 numbering, use the isodow function.

isodow function

Returns the ISO 8601 day of the week of ts, where 1 is Monday and 7 is Sunday. If ts is NULL, the result is NULL.

isodow and dayofweek differ only for Sunday: isodow returns 7, and dayofweek returns 0.

Syntax

isodow(ts)

Arguments

The isodow function takes the following argument:

ts

The value to read the ISO day of the week from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

BIGINT

Examples

The following SQL query returns the ISO 8601 day of the week of a timestamp. August 3, 2021, is a Tuesday, so the function returns 2.

SELECT isodow( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result

The query returns the following result.

result

2

The following SQL query compares isodow and dayofweek for a Sunday.

SELECT isodow(DATE '2021-03-07') AS isodow_sunday, dayofweek(DATE '2021-03-07') AS dayofweek_sunday

The query returns the following result.

isodow_sunday dayofweek_sunday

7

0

Considerations

For a TIMESTAMP WITH TIME ZONE value, the day is taken in the session time zone, which is UTC.

isodow does not work on INTERVAL values. A call such as isodow(INTERVAL '3 days') fails.

Differences from Athena engine version 3

Athena engine version 3 uses day_of_week for the ISO 8601 day of the week. DuckDB does not have day_of_week, so use isodow, which returns the same numbers.

isoyear function

Returns the ISO 8601 year that ts belongs to. ISO weeks start on Monday, and week 1 of a year is the week that contains the first Thursday of that year. Because of this, the first days of January can belong to the last week of the previous ISO year. The last days of December can belong to week 1 of the next ISO year. For other dates, the result equals year(ts).

If ts is NULL, the result is NULL. To get the ISO week number that goes with this year, use the week function.

Syntax

isoyear(ts)

Arguments

The isoyear function takes the following argument:

ts

The value to read the ISO year from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

BIGINT

Examples

The following SQL query returns the ISO 8601 year of a timestamp in August.

SELECT isoyear( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result

The query returns the following result.

result

2021

The following SQL query shows that January 1, 2021, a Friday, belongs to week 53 of ISO year 2020.

SELECT isoyear(DATE '2021-01-01') AS iso_year, year(DATE '2021-01-01') AS calendar_year, week(DATE '2021-01-01') AS iso_week

The query returns the following result.

iso_year calendar_year iso_week

2020

2021

53

The following SQL query shows that December 30, 2024, a Monday, belongs to week 1 of ISO year 2025.

SELECT isoyear(DATE '2024-12-30') AS iso_year, year(DATE '2024-12-30') AS calendar_year, week(DATE '2024-12-30') AS iso_week

The query returns the following result.

iso_year calendar_year iso_week

2025

2024

1

Considerations

For a TIMESTAMP WITH TIME ZONE value, the year is taken in the session time zone, which is UTC.

isoyear does not work on INTERVAL values. A call such as isoyear(INTERVAL '3 years') fails.

Differences from Athena engine version 3

Athena engine version 3 names this function year_of_week. DuckDB does not have year_of_week, so use isoyear.

week function

Returns the week of the year of ts, from 1 to 53, as ISO 8601 defines it. ISO weeks start on Monday, and week 1 is the week that contains the first Thursday of the year. If ts is NULL, the result is NULL.

Because of this rule, the first days of January can be in week 52 or 53 of the year before. The last days of December can be in week 1 of the year after. To get the year that an ISO week belongs to, use the isoyear function. To get the year and the week as one number, use the yearweek function. The weekofyear function returns the same result as week.

Syntax

week(ts)

Arguments

The week function takes the following argument:

ts

The value to read the week number from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. Nanosecond values, TIMESTAMP_NS and TIMESTAMPTZ_NS, are accepted too.

Return type

BIGINT

Examples

The following SQL query returns the ISO week number of a timestamp.

SELECT week( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result

The query returns the following result.

result

31

The following SQL query returns the week of January 1, 2021. That day is a Friday, so it is in the last ISO week of 2020.

SELECT week(DATE '2021-01-01') AS result

The query returns the following result.

result

53

The following SQL query returns the week of December 30, 2024. That day is a Monday in the first ISO week of 2025.

SELECT week(DATE '2024-12-30') AS result

The query returns the following result.

result

1

Considerations

Pass a date or timestamp, not a string. A call such as week('2021-08-03') fails. Write week(DATE '2021-08-03'), or cast the string to DATE.

You cannot pass an INTERVAL value. The call fails.

For a TIMESTAMP WITH TIME ZONE value, the week is computed in the session time zone, which is UTC.

yearweek function

Returns isoyear(ts) * 100 + week(ts), so August 3, 2021 gives 202131. Both parts follow ISO 8601: weeks start on Monday, and the year is the year that the week belongs to. That year can differ from the calendar year in the first and last days of a year. If ts is NULL, the result is NULL.

Because the week always fills the last two digits, the results sort in time order. Use yearweek to group and sort rows by ISO week.

Syntax

yearweek(ts)

Arguments

The yearweek function takes the following argument:

ts

The value to read the year and week from, as a DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value. Nanosecond values, TIMESTAMP_NS and TIMESTAMPTZ_NS, are accepted too.

Return type

BIGINT

Examples

The following SQL query returns the ISO year and week of a timestamp.

SELECT yearweek( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result

The query returns the following result.

result

202131

The following SQL query returns the year and week of January 1, 2021. The ISO year is 2020, not 2021.

SELECT yearweek(DATE '2021-01-01') AS result

The query returns the following result.

result

202053

The following SQL query returns the year and week of December 30, 2024, which is in the first ISO week of 2025.

SELECT yearweek(DATE '2024-12-30') AS result

The query returns the following result.

result

202501

Considerations

To split the result, use yearweek(ts) // 100 for the year and yearweek(ts) % 100 for the week.

Pass a date or timestamp, not a string. A call such as yearweek('2021-08-03') fails. You also cannot pass an INTERVAL value.

For a TIMESTAMP WITH TIME ZONE value, the year and week are computed in the session time zone, which is UTC.