WEEKOFYEAR

WEEKOFYEAR is an SQL function that returns the week number for a given date. The weeks start from Sunday and the range of return value is from 0 to 53.

WEEKOFYEAR(date)

  • date: This parameter refers to the date value from which the week of the year is to be extracted. The date must be provided in a valid date format recognized by MySQL. WEEKOFYEAR() returns an integer value representing the week number of the provided date value within its respective year; this ranges from 0 to 53.

Example

SELECT WEEKOFYEAR('2021-12-26') AS Week_Number;

Output

+-------------+
| Week_Number |
+-------------+
| 52 |
+-------------+

Explanation

The WEEKOFYEAR() function in MySQL returns the week number for a given date. The week number ranges from 0 to 53. In the example, the week number of ‘2021-12-26’ is 52.

WEEKOFYEAR(date)

  • date: This parameter is a date expression from which the SQL Server will extract the week of the year. The value must be a date or a timestamp data type, and it determines the specific week within a year that the function will return.

Example

SELECT DATEPART(wk, '2022-09-18') AS WeekOfYear;

Output

WeekOfYear
----------
38

Explanation

The DATEPART function with the wk argument is used to get the week of the year for a specific date. In the given example, the function returns the week number 38 for the date 2022-09-18.

WEEK_OF_YEAR(date)

  • date: This parameter represents the date for which the week of the year is to be found. It takes a DATE or TIMESTAMP value and calculates the week number according to the Oracle’s default date format. The date should be a valid Oracle’s date or timestamp. The function returns an integer that indicates the week of the year (1-53) where the date argument falls.

Oracle doesn’t have a WEEKOFYEAR function like SQL Server or MySQL. However, you can extract the week of the year from a date by using the TO_CHAR function with the IW format model. Here’s an example:

Example

SELECT TO_CHAR(TO_DATE('15-02-2021', 'DD-MM-YYYY'), 'IW') FROM dual;

Output

07

Explanation

In the example, we convert a string to a date using the TO_DATE function, then extract the week of the year with the TO_CHAR function and the IW format model. The result, 07, indicates that the date ‘15-02-2021’ falls in the 7th week of the year.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.