Lesson

MySQL MONTH() Function

10 min reading
Free Course

MySQL MONTH() Function

The MySQL MONTH() function returns the calendar month number from a DATE or DATETIME expression. It outputs an integer from 1 (January) to 12 (December).

Analysts rely on MONTH() to build monthly sales summaries, cohort charts, and recurring billing checks. Because it returns a numeric value instead of a text label, you can sort results chronologically without January sorting after February.

When zero dates are allowed in non-strict SQL modes, a date with a zero month like '2026-00-15' returns 0.

Syntax

MONTH(date)
  • date: A DATE, DATETIME, TIMESTAMP, or a valid date string or numeric YYYYMMDD value.

Practical Example

Suppose you want to calculate monthly order counts and revenue from demo_orders for the year 2026. Combining MONTH() with a BETWEEN range filter keeps the query fast and accurate.

SELECT
    MONTH(order_date) AS order_month,
    COUNT(*) AS total_orders,
    SUM(total_amount) AS monthly_revenue
FROM demo_orders
WHERE order_date >= '2026-01-01'
  AND order_date < '2027-01-01'
GROUP BY MONTH(order_date)
ORDER BY order_month;
order_month total_orders monthly_revenue
1 124 18420.50
2 98 14910.00
3 142 21305.75

Notice how the WHERE clause avoids wrapping order_date in YEAR(order_date) so MySQL can still use an index on order_date.

Common Mistakes & NULL Behavior

A common trap is grouping by MONTH(order_date) alone across multiple years. That merges January 2025 with January 2026 into a single row. Always include YEAR(order_date) in both SELECT and GROUP BY when spanning multiple years.

Another mistake is filtering with WHERE MONTH(order_date) = 5 on multi-million-row tables. Calling MONTH() on the column forces a full table scan. Use explicit date ranges or generated indexed columns instead.

MONTH() returns NULL when date is NULL or represents an impossible calendar date like '2026-02-29' (2026 is not a leap year) or '2026-04-31'.

Frequently Asked Questions

How do I get a two-digit month like '05' instead of 5?

MONTH() always returns an unpadded integer. Use DATE_FORMAT(order_date, '%m') or LPAD(MONTH(order_date), 2, '0') when you need a leading zero.

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

If your server SQL mode allows zero dates, MONTH('0000-00-00') returns 0. In strict modes with NO_ZERO_DATE, inserting or parsing zero dates may fail or yield NULL.

Is MONTH() affected by lc_time_names?

No. MONTH() returns integers 1 through 12, which do not change with locale settings.

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