Lesson

MySQL YEAR() Function

10 min reading
Free Course

MySQL YEAR() Function

The MySQL YEAR() function extracts the four-digit calendar year from a DATE, DATETIME, or TIMESTAMP expression. It returns an integer in the range 1000 to 9999, or 0 for zero dates like '0000-00-00' when allowed.

Developers use YEAR() to group annual revenue, track employee tenure cohorts, build yearly archive partitions, and verify date boundaries in audit queries. When you need numeric sorting or math like subtracting a birth year or hire year, YEAR() gives you an integer directly.

Unlike DATE_FORMAT(d, '%Y'), which returns a four-character string, YEAR(d) returns an integer ready for numeric comparisons and arithmetic.

Syntax

YEAR(date)
  • date: A DATE, DATETIME, TIMESTAMP, or valid date string or YYYYMMDD integer.

Practical Example

In demo_employees, you can count how many active team members were hired each year and calculate approximate years since hire relative to 2026.

SELECT
    YEAR(hire_date) AS hire_year,
    COUNT(*) AS employees_hired,
    2026 - YEAR(hire_date) AS approximate_tenure_years
FROM demo_employees
WHERE hire_date IS NOT NULL
GROUP BY YEAR(hire_date)
ORDER BY hire_year;
hire_year employees_hired approximate_tenure_years
2023 12 3
2024 18 2
2025 24 1

Grouping by YEAR(hire_date) summarizes annual hiring velocity cleanly in one pass without needing extra string parsing. Because YEAR() returns an integer, subtracting it from 2026 works directly in SQL math without explicit type casting.

Common Mistakes & NULL Behavior

Avoid filtering large tables with WHERE YEAR(order_date) = 2026. Calling YEAR() on an indexed column prevents MySQL from using a standard B-tree index range scan. Write WHERE order_date >= '2026-01-01' AND order_date < '2027-01-01' instead so the optimizer uses the index directly. Generated columns or functional indexes on YEAR(order_date) in MySQL 8.0 can also solve this when querying by year repeatedly.

Another mistake is pairing YEAR(d) with WEEKOFYEAR(d) for weekly reports. At year boundaries, the ISO week-year can differ from the calendar year returned by YEAR(). For example, '2024-12-30' belongs to ISO week 1 of 2025, so YEAR() and WEEKOFYEAR() combine into a mismatched 2024-W01. Use YEARWEEK(d, 3) for accurate weekly grouping.

Also watch numeric inputs passed to YEAR(). An eight-digit integer like 20260510 parses as '2026-05-10' and returns 2026, whereas a four-digit year number like 2026 fails date parsing and returns NULL.

YEAR() returns NULL when date is NULL or invalid (such as '2026-02-29', '2026-13-01', or 'not-a-date'). Passing a TIME-only value also fails date parsing and yields NULL.

Frequently Asked Questions

What does YEAR('0000-00-00') return?

When zero dates are permitted by server SQL mode, YEAR('0000-00-00') returns 0. In strict NO_ZERO_DATE contexts, zero dates may trigger warnings or NULL depending on the statement.

Does YEAR() handle two-digit years like '26-05-10'?

Yes. MySQL interprets 00–69 as 2000–2069 and 70–99 as 1970–1999, so '26-05-10' returns 2026. Always store and query four-digit years to avoid century ambiguity across historical data.

Should I use YEAR(col) in a WHERE clause?

Avoid it on large unindexed expressions. Use half-open date ranges (col >= '2026-01-01' AND col < '2027-01-01') so MySQL can use B-tree indexes on col.

Can I extract the year from a TIMESTAMP column with YEAR()?

Yes. YEAR() accepts TIMESTAMP columns and converts them from UTC into the current session time zone before returning the four-digit year.

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