Lesson

MySQL LAST_DAY() Function

10 min reading
Free Course

MySQL LAST_DAY(): Find the Final Calendar Day of Any Month

The LAST_DAY() function accepts a DATE or DATETIME value and returns a DATE representing the final day of that month ('YYYY-MM-DD'). It handles 28-, 29-, 30-, and 31-day months automatically, including leap-year Februaries.

Subscription billing and accounting queries use LAST_DAY() to close monthly ledgers and calculate month-end prorated charges.

Syntax

Supply one date or datetime expression:

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

If the input is a DATETIME, LAST_DAY() drops the time portion and returns a pure DATE.

Practical Example

Suppose your finance team computes the month-end statement date and remaining days in the month for rows in demo_orders.

SELECT
    order_id,
    order_date,
    LAST_DAY(order_date) AS statement_close_date,
    DATEDIFF(LAST_DAY(order_date), order_date) AS days_until_month_end
FROM demo_orders
WHERE order_id IN (101, 102, 103);
order_id order_date statement_close_date days_until_month_end
101 2024-02-10 2024-02-29 19
102 2026-02-10 2026-02-28 18
103 2026-04-15 2026-04-30 15

Notice how row 101 (leap year 2024) returns 2024-02-29, while row 102 (2026) returns 2026-02-28.

Common Mistakes & NULL Behavior

Avoid these errors when using LAST_DAY():

  • NULL and invalid dates return NULL: Passing NULL, '2026-02-30', or '2026-00-15' makes LAST_DAY() return NULL.
  • No built-in FIRST_DAY() function: MySQL does not have a FIRST_DAY() function. To get the first day of the same month, write DATE_FORMAT(order_date, '%Y-%m-01') or DATE_ADD(LAST_DAY(DATE_SUB(order_date, INTERVAL 1 MONTH)), INTERVAL 1 DAY).
  • Time portion is stripped: Comparing created_at <= LAST_DAY(created_at) on a DATETIME column misses rows after 00:00:00 on the last day! Use created_at < LAST_DAY(created_at) + INTERVAL 1 DAY instead.
  • No TIME-only input: Passing '15:00:00' without a date returns NULL.

Frequently Asked Questions

How do I find the last day of the previous month?

Run LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) or LAST_DAY(CURDATE() - INTERVAL 1 MONTH).

How do I get the first day of the current month using LAST_DAY()?

Subtract one month, take LAST_DAY(), and add one day: LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY.

Does LAST_DAY() return a DATE or a DATETIME?

It always returns a DATE (YYYY-MM-DD), even if you pass a DATETIME or TIMESTAMP input.

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