Lesson

MySQL DAY() Function

10 min reading
Free Course

MySQL DAY(): Extract the Day Number of the Month

The DAY() function returns the day of the month as an integer from 1 to 31 for a given DATE or DATETIME value. It is a concise synonym for DAYOFMONTH().

Billing scripts and payroll jobs use DAY() to group customers by billing cycle day or check whether an event occurred on the first of the month.

Syntax

Pass any date or timestamp expression to DAY():

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

The function returns an integer from 1 to 31 for normal dates, 0 when the day part is zero (if non-strict mode permits '2026-05-00'), or NULL for invalid inputs.

Practical Example

Suppose your billing service groups accounts in demo_customers by their monthly invoice day based on signup_date.

SELECT
    customer_id,
    full_name,
    signup_date,
    DAY(signup_date) AS billing_day
FROM demo_customers
WHERE customer_id IN (401, 405, 408);
customer_id full_name signup_date billing_day
401 Amina Chowdhury 2026-05-10 10
405 Maya Lin 2026-01-31 31
408 David Kim 2026-02-01 1

Using DAY() gives you a clean integer you can compare with > or < without converting formatted strings.

Common Mistakes & NULL Behavior

Avoid these common misunderstandings with DAY():

  • NULL and impossible dates return NULL: DAY(NULL) and DAY('2026-04-31') both evaluate to NULL.
  • Returns day of month, not weekday: DAY('2026-05-12') returns 12, not 2 or 'Tuesday'. Use DAYOFWEEK(), WEEKDAY(), or DAYNAME() for weekday values.
  • Watch out for day 31 billing logic: Customers with billing_day = 31 will not match DAY(CURDATE()) = 31 in 30-day months or February. Use LAST_DAY() checks for end-of-month billing.
  • No TIME-only strings: Passing '14:20:00' without a date causes DAY() to return NULL.

Frequently Asked Questions

Is there any difference between DAY() and DAYOFMONTH()?

No. DAY() is a direct synonym for DAYOFMONTH(). Both return the exact same integer value (1..31).

Does DAY() return zero-padded values like '05'?

No. DAY() returns an integer (5), whereas DATE_FORMAT(date_col, '%d') returns a zero-padded two-character string ('05').

Can DAY() extract the day from a DATETIME string?

Yes. DAY('2026-05-04 19:30:00') ignores the time portion and returns 4.

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