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

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



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)
Updated on: 2019-07-30T22:30:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements