Lesson

MySQL WEEK() Function

10 min reading
Free Course

MySQL WEEK() Function

The MySQL WEEK() function returns the week number for a DATE or DATETIME expression, ranging from 0 to 53 or 1 to 53 depending on the mode argument.

Unlike simpler date extractors, calendar weeks vary by region and business standard. Some teams start weeks on Sunday, others on Monday, and European accounting follows ISO 8601. WEEK() handles all eight variations through its optional mode parameter (0 through 7).

If you omit mode, MySQL uses the default_week_format server system variable, which defaults to 0.

Syntax

WEEK(date[, mode])
  • date: A DATE, DATETIME, or TIMESTAMP expression.
  • mode: Optional integer 0–7 selecting Sunday/Monday start, 0–53 vs 1–53 range, and Week 1 rules.

Practical Example

Using demo_orders, you can compare default mode 0 (Sunday start, 0–53) against ISO 8601 mode 3 (Monday start, 1–53, four-day rule) around New Year's Day.

SELECT
    order_id,
    order_date,
    WEEK(order_date, 0) AS week_mode_0,
    WEEK(order_date, 3) AS week_iso_8601
FROM demo_orders
WHERE order_date BETWEEN '2026-01-01' AND '2026-01-06'
ORDER BY order_date;
order_id order_date week_mode_0 week_iso_8601
1201 2026-01-01 0 1
1202 2026-01-04 1 1
1203 2026-01-05 1 2

Notice how Thursday '2026-01-01' returns week 0 in mode 0 (before the first Sunday), but week 1 in ISO mode 3.

Common Mistakes & NULL Behavior

The biggest mistake is calling WEEK(col) without specifying mode. If one environment sets default_week_format = 3 and production uses 0, your weekly dashboards will silently disagree! Always pass the mode explicitly.

Another trap is grouping by WEEK(order_date, 3) alone across year boundaries. In ISO 8601 (mode 3), '2023-01-01' belongs to week 52 of 2022, so pairing WEEK(col, 3) with YEAR(col) puts it in 2023-W52 wrongly! Use YEARWEEK(col, 3) instead when grouping across years.

WEEK() returns NULL if date is NULL or invalid (such as '2026-02-30').

Frequently Asked Questions

Which mode in WEEK() matches ISO 8601?

Mode 3 (WEEK(date, 3)) implements ISO 8601: weeks start on Monday, range from 1 to 53, and Week 1 is the first week with 4 or more days in the new year.

What is the difference between mode 0 and mode 1?

Mode 0 starts weeks on Sunday (first Sunday starts Week 1, earlier days are Week 0). Mode 1 starts weeks on Monday (4+ days in new year starts Week 1, earlier days are Week 0).

How do I avoid Week 0 in weekly reports?

Pass a 1–53 mode (1, 2, 3, 5, 6, 7) or use YEARWEEK(date, mode), which never returns week 0.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum