Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 147

First and Last Day of a Month in MySQL

In this lesson: Get the first and last date of any month in MySQL with LAST_DAY, DATE_FORMAT and DATE_SUB — with output. In Hindi & English.

Month boundaries

Monthly reports need the first and last day of a month. Months have different lengths (28 to 31 days) and leap years complicate February, so calculating these by hand is error-prone. MySQL's LAST_DAY and DATE_FORMAT make it reliable.

💡 LAST_DAY gives the last day; DATE_FORMAT(d, '%Y-%m-01') gives the first day.

LAST_DAY for the last day

SELECT LAST_DAY('2026-02-10');
2026-02-28

LAST_DAY(date) returns the last day of that date's month. It correctly handles 28, 29, 30 and 31-day months, including leap years — no manual logic needed.

Getting the first day

SELECT DATE_FORMAT('2026-02-10', '%Y-%m-01');
2026-02-01

There is no FIRST_DAY function, but formatting the date with day 01 gives the first day of the month. This works for any date you pass in.

This month automatically

SELECT
    DATE_FORMAT(CURDATE(), '%Y-%m-01') AS first_day,
    LAST_DAY(CURDATE()) AS last_day;
+------------+------------+ | first_day | last_day | +------------+------------+ | 2026-09-01 | 2026-09-30 | +------------+------------+

Using CURDATE() as the input gives the current month's boundaries, updating automatically as months change.

A monthly date range

SELECT * FROM orders
WHERE order_date BETWEEN
    DATE_FORMAT(CURDATE(), '%Y-%m-01')
    AND LAST_DAY(CURDATE());

Combine both to filter rows within the current month. This is the standard pattern for "this month's" reports — sales, sign-ups, or any dated records.

Previous month

SELECT
    DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, '%Y-%m-01') AS prev_first,
    LAST_DAY(CURDATE() - INTERVAL 1 MONTH) AS prev_last;

Subtracting INTERVAL 1 MONTH before applying the functions gives last month's boundaries — useful for monthly comparisons and closing reports.

Common mistakes

  • Hard-coding 30 or 31. Month lengths vary; let LAST_DAY handle it.
  • Forgetting leap years. LAST_DAY handles February correctly.
  • Looking for a FIRST_DAY function. There is none; use DATE_FORMAT with 01.
  • Using > last day without <= . Use BETWEEN or inclusive bounds for the full month.

Summary

  • LAST_DAY(date) gives the last day of the month, leap-year safe.
  • DATE_FORMAT(date, '%Y-%m-01') gives the first day.
  • Use CURDATE() for the current month, updating automatically.
  • Combine both with BETWEEN for a monthly range.
  • Subtract INTERVAL 1 MONTH for the previous month.

Next, compute ages in age from date of birth, or filter recent rows in records from the last 7 days.

Frequently Asked Questions

How do I get the last day of a month in MySQL?
Use the LAST_DAY() function: SELECT LAST_DAY('2026-02-10'); returns 2026-02-28. It automatically handles different month lengths and leap years, always giving the correct final day.
How do I get the first day of a month?
MySQL has no FIRST_DAY function, but you can use DATE_FORMAT(d, '%Y-%m-01') to set the day to 01, or subtract days from LAST_DAY. Both give the first day of the month for any date.
How do I get the first and last day of the current month?
Use CURDATE() as the input: DATE_FORMAT(CURDATE(), '%Y-%m-01') for the first day and LAST_DAY(CURDATE()) for the last. These update automatically as the month changes.
How do I select records for the current month?
Filter with a range from the first to the last day: WHERE date BETWEEN DATE_FORMAT(CURDATE(),'%Y-%m-01') AND LAST_DAY(CURDATE()). This returns all rows dated within the current month.
Does LAST_DAY handle leap years?
Yes. LAST_DAY correctly returns February 29 in a leap year and February 28 otherwise, and it knows which months have 30 or 31 days. You do not need to handle month lengths yourself.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPivot Tables in MySQL Your progress: 147 / 151 (97%) · Lesson 148 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free