Get the first and last date of next month in MySQL

后端 未结 11 1110
感情败类
感情败类 2020-12-01 03:43

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

相关标签:
11条回答
  • 2020-12-01 04:06

    Shorter query:

    SELECT
    ADDDATE(LAST_DAY(NOW()), 1) AS firstOfNextMonth,
    LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)) AS lastOfNextMonth
    
    0 讨论(0)
  • 2020-12-01 04:07

    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));
    
    0 讨论(0)
  • 2020-12-01 04:13
    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 讨论(0)
  • 2020-12-01 04:14

    In postgresql you have perfect date truncation functions.

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

    0 讨论(0)
  • 2020-12-01 04:18

    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
    
    0 讨论(0)
  • 2020-12-01 04:25

    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)
    
    0 讨论(0)
提交回复
热议问题