Get the first and last date of next month in MySQL?

MySQLMySQLi Database

You can get the first and last date of next month using date_add() function from MySQL.

The syntax is as follows -

select
date_sub(
   last_day(
      date_add(now(), interval anyIntervalTime)
   ),
   interval day(
      last_day(
         date_add(now(), interval anyIntervalTime)
      )
   )-1 DAY
) as anyVariableName,
last_day (
   date_add(now(), anyIntervalTime)
)
as anyVariableName;

Implement the above syntax to get the first and last date of next month using interval 1 month in date_add() function. The query is as follows.

mysql> select
-> date_sub(
->    last_day(
->       date_add(now(), interval 1 month)
->    ),
->    interval day(
->       last_day(
->          date_add(now(), interval 1 month)
->       )
->    )-1 DAY
-> ) as firstDayOfNextMonth,
->
-> last_day
-> (
->    date_add(now(), interval 1 month)
-> )
-> as lastDayOfNextMonth
-> ;

The following is the output displaying the first and last day of the next month.

+---------------------+--------------------+
| firstDayOfNextMonth | lastDayOfNextMonth |
+---------------------+--------------------+
| 2019-02-01 | 2019-02-28 |
+---------------------+--------------------+
1 row in set (0.00 sec)
raja
Published on 11-Jan-2019 14:55:40
Advertisements