31

How can I get the first and last day of next month to be used in the where clause?

shA.t
  • 16,580
  • 5
  • 54
  • 111
Lennie De Villiers
  • 751
  • 2
  • 9
  • 12

11 Answers11

39

Use:

SELECT

DATE_SUB(
    LAST_DAY(
        DATE_ADD(NOW(), INTERVAL 1 MONTH)
    ), 
    INTERVAL DAY(
        LAST_DAY(
            DATE_ADD(NOW(), INTERVAL 1 MONTH)
        )
    )-1 DAY
) AS firstOfNextMonth,

LAST_DAY(
    DATE_ADD(NOW(), INTERVAL 1 MONTH)
)AS lastOfNextMonth
Pacerier
  • 86,231
  • 106
  • 366
  • 634
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • For more MySQL time-related functions like this one, see [this blog post](http://sys-exit.blogspot.com/2013/06/mysql-today-tomorrow-yesterday-this.html) – ducin Mar 14 '14 at 21:27
  • @OMGPonies, Use `curdate` instead of `now()` because we don't need the timings. Also, for first day, it's more straightforward to [simply minus the number of days of the current month](http://stackoverflow.com/a/28844641/632951). It's also faster because we are doing only a single `date_sub()` call and a single `day()` call. – Pacerier Mar 04 '15 at 00:32
30

For the last day of next month, you can use the LAST_DAY() function:

SELECT LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 1 MONTH));
+-------------------------------------------------+
| LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) |
+-------------------------------------------------+
| 2010-07-31                                      |
+-------------------------------------------------+
1 row in set (0.00 sec)

Some tested edge cases:

SELECT LAST_DAY(DATE_ADD('2010-01-31', INTERVAL 1 MONTH));
+----------------------------------------------------+
| LAST_DAY(DATE_ADD('2010-01-31', INTERVAL 1 MONTH)) |
+----------------------------------------------------+
| 2010-02-28                                         |
+----------------------------------------------------+
1 row in set (0.00 sec)

SELECT LAST_DAY(DATE_ADD('2010-02-28', INTERVAL 1 MONTH));
+----------------------------------------------------+
| LAST_DAY(DATE_ADD('2010-02-28', INTERVAL 1 MONTH)) |
+----------------------------------------------------+
| 2010-03-31                                         |
+----------------------------------------------------+
1 row in set (0.00 sec)

SELECT LAST_DAY(DATE_ADD('2010-08-31', INTERVAL 1 MONTH));
+----------------------------------------------------+
| LAST_DAY(DATE_ADD('2010-08-31', INTERVAL 1 MONTH)) |
+----------------------------------------------------+
| 2010-09-30                                         |
+----------------------------------------------------+
1 row in set (0.00 sec)

There is also a tricky use of the DATE_FORMAT() function to get the first day of a month. You can use it as follows:

SELECT DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01');
+---------------------------------------------------------------+
| DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH),'%Y-%m-01') |
+---------------------------------------------------------------+
| 2010-07-01                                                    |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

Therefore:

SELECT   DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01') AS
            FirstDayOfNextMonth,
         LAST_DAY(DATE_ADD(CURDATE(), INTERVAL 1 MONTH)) AS
            LastDayOfNextMonth;

+---------------------+--------------------+
| FirstDayOfNextMonth | LastDayOfNextMonth |
+---------------------+--------------------+
| 2010-07-01          | 2010-07-31         |
+---------------------+--------------------+
1 row in set (0.00 sec)
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
19

First day of next month is simply last day of this month + 1:

select adddate(last_day(curdate()), 1)

Last day of next month is simply last day of (today + 1 month):

select last_day(curdate() + interval 1 month))

These are the most straightforward solutions. You'll not be able to find a shorter one.


If you need the first day of the current month, see https://stackoverflow.com/a/28966866/632951

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634
3
# FIRST date of next month
select date_sub(date_add(curdate(), interval 1 month), interval day(curdate())-1 day);

# LAST date of next month
select date_sub(date_add(curdate(), interval 2 month), interval day(curdate()) day);

not sure that's the shortest queries, but they do work

zed_0xff
  • 32,417
  • 7
  • 53
  • 72
2

As @DanielVassallo explained it, retrieving the last day of next month is easy:

SELECT LAST_DAY(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH));

To retrieve the first day, you could first define a custom FIRST_DAY function (unfortunately MySQL does not provide any):

DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
  RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;

And then you could do:

SELECT FIRST_DAY(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH));
Stéphane
  • 3,884
  • 1
  • 30
  • 27
1

To get first date of next month :-select LAST_DAY(NOW()) + INTERVAL 1 DAY

To get last date of next month :-select LAST_DAY(NOW()+ INTERVAL 1 Month)

JibinNajeeb
  • 784
  • 1
  • 10
  • 31
0

Here is all Possible solutions. Hope this will help ... Just Run this to get All details

SELECT 

  DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 1 DAY) AS GetLastDay, 
  DATE_FORMAT(NOW(),'%Y-%m-%d') AS GetTodaysDate,
  DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 7 DAY) AS Add7DaysFromTodaysDate,
  DATE_SUB(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 7 DAY) AS Substract7DaysFromTodaysDate,
  DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-%d'),INTERVAL 1 MONTH) AS Add1MonthFromTodaysDate,
  DATE_FORMAT(NOW(),'%Y-%m-01') AS FirstDayCurrentMonth , 
  LAST_DAY(DATE_FORMAT(NOW(),'%Y-%m-%d')) AS lastDayCurrentMonth,
  DATE_SUB(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), 
            INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AS FirstOfNextMont007,
  LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AS lastOfpREMonth,
  DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)), 
            INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AS FirstOfNextMonth,
   LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)) AS lastOfNextMonth
DBTutor
  • 315
  • 1
  • 6
  • 17
0

Shorter query:

SELECT
ADDDATE(LAST_DAY(NOW()), 1) AS firstOfNextMonth,
LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)) AS lastOfNextMonth
LateJ
  • 1
  • 1
  • [`curdate` is better](http://stackoverflow.com/a/28844641/632951) than `now` in terms of both speed and intention conveyance. Also, `+` is better than `DATE_ADD` in terms of both readability and command length. – Pacerier Mar 10 '15 at 15:19
0

In postgresql you have perfect date truncation functions.

For MySQL, I found a discussion here which might give some ideas.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Peter Tillemans
  • 34,983
  • 11
  • 83
  • 114
  • Good link actually, but you want to include the contents of it within this page. What do you mean by date truncation functions anyway? – Pacerier Mar 10 '15 at 15:10
0
SELECT DATE_ADD(LAST_DAY('2020-12-14'),INTERVAL 1 DAY) AS 'FIRST DAY OF NEXT MONTH';
        +----------------------------+
        | FIRST DAY OF NEXT MONTH |
        +----------------------------+
        | 2021-01-01                 |
        +----------------------------+
        1 row in set (1.06 sec)
        
SELECT DATE_ADD(LAST_DAY(NOW()),INTERVAL 1 MONTH) AS 'LAST DAY OF NEXT MONTH';
        +------------------------+
        | LAST DAY OF NEXT MONTH |
        +------------------------+
        | 2020-09-30             |
        +------------------------+
        1 row in set (0.51 sec)
0
select Convert(varchar(10),DateAdd(Day, 1, getdate() - Day(getdate()) + 1) -1,105)

select Convert(varchar(10),getdate(),105)

select year(getdate())
shA.t
  • 16,580
  • 5
  • 54
  • 111