How to use the MySQL WEEK() function

In this article, we will learn how to use the MySQL WEEK() function, which returns the week number for a given date or datetime value.

Posted on

In this article, we will learn how to use the MySQL WEEK() function, which returns the week number for a given date or datetime value. We will also see some examples of how to use this function in different situations, and explore some related functions that can be helpful for working with dates and weeks.

Syntax

The syntax of the WEEK() function is as follows:

WEEK(date, mode)

The date parameter can be any valid date or datetime expression, or a string that can be converted to a date or datetime value. If the date parameter is NULL, the function returns NULL. The mode parameter is an optional integer that specifies how the week number should be calculated. The default value of the mode parameter is 0, which means that the week starts on Sunday and the week 1 is the first week that contains at least 4 days in the year. The possible values of the mode parameter are shown in the following table:

mode First day of week Range Week 1 is the first week …
0 Sunday 0-53 with more than 3 days this year
1 Monday 0-53 with more than 3 days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with a Monday in this year
4 Sunday 0-53 with more than 3 days this year
5 Monday 0-53 with more than 3 days this year
6 Sunday 1-53 with a Sunday in this year
7 Monday 1-53 with a Monday in this year

The WEEK() function returns an integer value that represents the week number for the given date or datetime value, according to the specified mode. For example, WEEK('2023-01-15', 0) returns 2, which is the week number for the date ‘2023-01-15’ when the week starts on Sunday and the week 1 is the first week that contains at least 4 days in the year.

Examples

Let’s see some examples of how to use the WEEK() function in MySQL.

Example 1: Get the week number for a date value

We can use the WEEK() function to get the week number for a date value. For example:

SELECT WEEK('2023-01-15', 0) AS result;

This query will return the week number for the date value ‘2023-01-15’ when the week starts on Sunday and the week 1 is the first week that contains at least 4 days in the year. The query will return 2, which is the week number for the date value.

Example 2: Get the week number for a datetime value

We can use the WEEK() function to get the week number for a datetime value. For example:

SELECT WEEK('2023-01-15 10:02:34', 0) AS result;

This query will return the week number for the datetime value ‘2023-01-15 10:02:34’ when the week starts on Sunday and the week 1 is the first week that contains at least 4 days in the year. The query will return 2, which is the same as the week number for the date part of the datetime value.

Example 3: Get the week number for a string value

We can use the WEEK() function to get the week number for a string value that can be converted to a date or datetime value. For example:

SELECT WEEK('January 15, 2023', 0) AS result;

This query will return the week number for the string value ‘January 15, 2023’ that can be converted to a date value. The query will return 2, which is the same as the week number for the date value ‘2023-01-15’.

Example 4: Get the week number for a date value with a different mode

We can use the WEEK() function to get the week number for a date value with a different mode. For example, if we want to get the week number for the date value ‘2023-01-15’ when the week starts on Monday and the week 1 is the first week that contains a Monday in the year, we can use the following query:

SELECT WEEK('2023-01-15', 3) AS result;

This query will return the week number for the date value ‘2023-01-15’ when the week starts on Monday and the week 1 is the first week that contains a Monday in the year. The query will return 3, which is the week number for the date value.

Example 5: Get the difference between two date values in weeks

We can use the WEEK() function with the DATEDIFF() function, which returns the difference between two date values, to get the difference between two date values in weeks. For example, if we want to get the difference between the two date values ‘2023-01-15’ and ‘2023-01-01’ in weeks, we can use the following query:

SELECT DATEDIFF(WEEK('2023-01-15', 0), WEEK('2023-01-01', 0)) AS result;

This query will return the difference between the two date values ‘2023-01-15’ and ‘2023-01-01’ in weeks. The query will use the WEEK() function to convert the date values to the week numbers, and then use the DATEDIFF() function to get the difference between them. The query will return 1, which is the number of weeks between the two date values.

There are some other functions that are related to the WEEK() function, and can be useful for working with dates and weeks. Here are some of them:

  • DATE(): This function returns the date part of a date or datetime value, or a string that can be converted to a date value. For example, DATE('2023-01-15 10:02:34') returns ‘2023-01-15’.
  • DATETIME(): This function returns the datetime value of a date or datetime value, or a string that can be converted to a datetime value. For example, DATETIME('2023-01-15 10:02:34') returns ‘2023-01-15 10:02:34’.
  • WEEKDAY(): This function returns the weekday index for a given date or datetime value. The weekday index is an integer that ranges from 0 to 6, where 0 is Monday and 6 is Sunday. For example, WEEKDAY('2023-01-15') returns 6, which is the weekday index for Sunday.
  • WEEKOFYEAR(): This function returns the week number for a given date or datetime value, according to the ISO 8601 standard. The ISO 8601 standard defines that the week 1 is the first week that contains the first Thursday of the year, and the week starts on Monday. For example, WEEKOFYEAR('2023-01-15') returns 2, which is the week number for the date ‘2023-01-15’ according to the ISO 8601 standard.
  • DATEDIFF(): This function returns the difference between two date values. For example, DATEDIFF('2023-01-15', '2023-01-01') returns 14, which is the number of days between the two date values.

Conclusion

In this article, we learned how to use the MySQL WEEK() function, which returns the week number for a given date or datetime value. We also saw some examples of how to use this function in different situations, and explored some related functions that can be helpful for working with dates and weeks.