Lesson

MySQL YEARWEEK() Function

10 min reading
Free Course

MySQL YEARWEEK() Function

The MySQL YEARWEEK() function returns both the week-year and week number for a DATE or DATETIME expression as a single six-digit integer (YYYYWW). For example, a date in the 19th week of 2026 returns 202619.

Unlike WEEK(), YEARWEEK() never returns week 0, and it adjusts the year portion whenever a date near January 1 or December 31 belongs to the adjacent year's week. That makes YEARWEEK() the safest function for grouping weekly time-series data across multi-year reports.

You can pass an optional mode (0 through 7) to control week start day and Week 1 rules. Mode 3 follows ISO 8601.

Syntax

YEARWEEK(date[, mode])
  • date: A DATE, DATETIME, or TIMESTAMP expression.
  • mode: Optional integer 0–7 (defaults to 0, or default_week_format). Use 3 for ISO 8601 weeks.

Practical Example

Suppose demo_orders contains orders spanning December 2024 and January 2025. Using YEARWEEK(order_date, 3) groups late-December ISO week-1 orders into 202501 instead of mislabeling them under 2024.

SELECT
    YEARWEEK(order_date, 3) AS iso_year_week,
    COUNT(*) AS weekly_orders,
    SUM(total_amount) AS weekly_revenue
FROM demo_orders
WHERE order_date >= '2024-12-23'
  AND order_date < '2025-01-13'
GROUP BY YEARWEEK(order_date, 3)
ORDER BY iso_year_week;
iso_year_week weekly_orders weekly_revenue
202452 41 6120.00
202501 53 7890.50
202502 48 7045.25

Orders on '2024-12-30' and '2025-01-02' land together in 202501 automatically, keeping weekly totals intact.

Common Mistakes & NULL Behavior

Notice a key difference from WEEK(): even in mode 0, YEARWEEK() returns weeks 1 through 53, never 0. Days before the first week of the new year belong to the final week (52 or 53) of the previous year. Do not expect YEARWEEK('2026-01-01', 0) to return 202600—it returns 202552.

Another common error is omitting mode. Always write YEARWEEK(col, 3) (or your chosen mode) explicitly so server default_week_format changes cannot alter your reports.

YEARWEEK() returns NULL if date is NULL, invalid (like '2026-02-30'), or '0000-00-00'.

Frequently Asked Questions

Why did YEARWEEK('2024-12-30', 3) return 202501 instead of 202453?

Under ISO 8601 (mode 3), the week starting Monday December 30, 2024 has four days in January 2025, making it Week 1 of 2025 (202501).

Does YEARWEEK() return an integer or a string?

It returns an integer (YYYYWW, such as 202619). You can format it as '2026-W19' using INSERT(YEARWEEK(d, 3), 5, 0, '-W') if needed.

What is the difference between YEARWEEK(d, 3) and DATE_FORMAT(d, '%x%v')?

Both return ISO 8601 YYYYWW, except YEARWEEK() returns an integer while DATE_FORMAT(d, '%x%v') returns a string.

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