Lesson

MySQL DAYOFMONTH() Function

10 min reading
Free Course

MySQL DAYOFMONTH(): Get the Numeric Day Within a Month

The DAYOFMONTH() function extracts the calendar day of the month from a DATE or DATETIME expression, returning an integer between 1 and 31. It behaves identically to DAY().

Payroll and reconciliation scripts use DAYOFMONTH() to split monthly cycles into first-half and second-half pay periods.

Syntax

Pass a date or timestamp expression into DAYOFMONTH():

DAYOFMONTH(date_value)
  • date_value: A DATE, DATETIME, TIMESTAMP, or valid date string.

The output is an integer in the range 1 to 31 (or 0 for zero-day dates like '2026-05-00' if non-strict mode permits them).

Practical Example

Suppose HR checks demo_employees to tag hires who started during the first 15 days of any month versus the second half.

SELECT
    employee_id,
    full_name,
    hire_date,
    DAYOFMONTH(hire_date) AS start_day,
    CASE
        WHEN DAYOFMONTH(hire_date) <= 15 THEN 'First Half'
        ELSE 'Second Half'
    END AS payroll_window
FROM demo_employees
WHERE employee_id IN (11, 14, 15);
employee_id full_name hire_date start_day payroll_window
11 Nadia Rahman 2025-03-03 3 First Half
14 Kevin O'Connor 2025-07-15 15 First Half
15 Sofia Alvarez 2026-01-22 22 Second Half

This classification stays clean because DAYOFMONTH() returns a raw integer ready for numeric comparisons.

Common Mistakes & NULL Behavior

Keep these edge cases in mind when using DAYOFMONTH():

  • Invalid calendar dates return NULL: Calling DAYOFMONTH('2026-02-29') in non-leap year 2026 returns NULL, and DAYOFMONTH(NULL) also returns NULL.
  • Do not confuse with DAYOFYEAR(): DAYOFMONTH('2026-02-05') returns 5, whereas DAYOFYEAR('2026-02-05') returns 36.
  • Zero-date behavior in older modes: If non-strict sql_mode allows '0000-00-00', DAYOFMONTH('0000-00-00') returns 0 rather than NULL.
  • Non-sargable filter warning: Avoid WHERE DAYOFMONTH(hire_date) = 1 on large tables if you only want a specific month; filter by a date range instead.

Frequently Asked Questions

Should I use DAYOFMONTH() or DAY()?

Both functions are exact synonyms in MySQL. DAYOFMONTH() makes SQL self-documenting when used alongside DAYOFWEEK() and DAYOFYEAR().

Does DAYOFMONTH() work on numeric YYYYMMDD inputs?

Yes. DAYOFMONTH(20260512) parses the integer as May 12, 2026 and returns 12.

What does DAYOFMONTH() return for a DATETIME with 23:59:59?

It ignores the time part completely and returns the calendar day of the date portion.

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