Get First Day and Last Day of Current and Last Month MySQL

Get first day and last day of current and last month MySQL; In this tutorial, we will learn how to get the first day of the month, the last day of the month, the first day of the previous month, the last day of the previous month in Mysql.

If you need to get the first and last day of the current, next, and previous month, so you will learn here with demonstrating each example using MySQL queries.

Get First Day and Last Day of Current and Last Month MySQL

  • First day of Current Month
  • Last day of Current Month
  • Next Month Last Day
  • First day of Previous Month
  • Last day of Previous Month

First day of Current Month

Now let’s take an example to explain.

 SELECT LAST_DAY(curdate() - interval 1 month) + interval 1 day

 =========================OR======================================

 SELECT DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))-
 1 DAY) AS 'FIRST DAY OF CURRENT MONTH';

Output-1

+-------------+
| Result      |
+-------------+
| 2019-07-01  |
+-------------+

Last day of Current Month

Let’s take another example to get the last day of the current month in mysql. We will use MySQL now() or curdate() function with last_day().

select last_day(now())

 =========================OR======================================

select last_day(curdate())

Output

+-------------+
| Result      |
+-------------+
| 2019-07-31  |
+-------------+ 

Next Month Last Day

In this example, we will get the last day of next month using this function.

We will add one month in the current date-time value and pass the value in LAST_DAY() function. It will return on the last day of next month. See the query of the following :

SELECT LAST_DAY(CURDATE() + INTERVAL 1 MONTH);

Output

+----------------------------------------+
| LAST_DAY(CURDATE() + INTERVAL 1 MONTH) |
+----------------------------------------+
|  2019-08-31                            |
+----------------------------------------+
1 row in set (0.00 sec) 

First day of Previous Month

Let’s take a new example, we get the first day of the previous month.

select last_day(curdate() - interval 2 month) + interval 1 day

Output

+---------------------+
| result              |
+---------------------+
| 2019-06-01          |
+---------------------+

Last day of Previous Month

Let’s take the next example, we get last day of the previous month.

select last_day(curdate() - interval 1 month)

Output

+-------------+
| Result      |
+-------------+
| 2019-06-01  |
+-------------+ 

Conclusion

Get first day and last day of current and last month MySQL; In this tutorial, you have learned how to get the first day of the month, the last day of the month, the first day of the previous month, the last day of the previous month in Mysql.

Recommended MySQL Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Get First Day and Last Day of Current and Last Month MySQL

  1. Very helpfull, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *