MySQL WEEKOFYEAR() Function
MySQL WEEKOFYEAR() Function
The MySQL WEEKOFYEAR() function returns the ISO 8601 calendar week number for a DATE or DATETIME expression. It always outputs an integer between 1 and 53.
WEEKOFYEAR(date) is an exact shortcut for WEEK(date, 3):
- Weeks always start on Monday.
- Week
1is the first week of the year that contains at least 4 days in the new year (equivalently, the week containing January 4).
Because it enforces ISO 8601 regardless of server configuration, WEEKOFYEAR() never changes behavior if default_week_format differs across servers.
Syntax
WEEKOFYEAR(date)
date: ADATE,DATETIME,TIMESTAMP, or valid date string.
Practical Example
Suppose you inspect early-January and late-December orders in demo_orders to verify how ISO 8601 assigns week numbers across calendar year boundaries.
SELECT
order_id,
order_date,
DAYNAME(order_date) AS day_name,
WEEKOFYEAR(order_date) AS iso_week
FROM demo_orders
WHERE order_date IN ('2023-01-01', '2024-12-30', '2026-01-01')
ORDER BY order_date;
| order_id | order_date | day_name | iso_week |
|---|---|---|---|
| 1401 | 2023-01-01 | Sunday | 52 |
| 1402 | 2024-12-30 | Monday | 1 |
| 1403 | 2026-01-01 | Thursday | 1 |
Look closely at row 1401: '2023-01-01' is a Sunday, so ISO 8601 assigns it to week 52 of 2022. Row 1402 ('2024-12-30') is a Monday with 4 days in 2025, so it gets week 1 of 2025!
Common Mistakes & NULL Behavior
Because of ISO cross-year weeks, pairing YEAR(order_date) with WEEKOFYEAR(order_date) creates a classic reporting bug! For '2024-12-30', YEAR() gives 2024 while WEEKOFYEAR() gives 1, placing December 30 into '2024-W01' (almost a full year earlier). Whenever you group weekly data across years, use YEARWEEK(order_date, 3) instead, which outputs 202501 correctly.
WEEKOFYEAR() also returns NULL when date is NULL, invalid (like '2026-02-30'), or '0000-00-00'.
Frequently Asked Questions
Is WEEKOFYEAR(date) identical to WEEK(date, 3)?
Yes. MySQL defines WEEKOFYEAR(date) as completely equivalent to WEEK(date, 3).
Why did January 1 return week 52 or 53 in WEEKOFYEAR()?
If January 1 falls on a Friday, Saturday, or Sunday, that week has fewer than 4 days in the new year, so ISO 8601 counts it as the final week (52 or 53) of the previous year.
Can WEEKOFYEAR() return 0?
No. ISO 8601 week numbers range strictly from 1 to 53.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.