Get the first and last date of next month in MySQL

后端 未结 11 1111
感情败类
感情败类 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:28

    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)

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

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

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

    select affffdate(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

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

    0 讨论(0)
  • 2020-12-01 04:30
    select Convert(varchar(10),DateAdd(Day, 1, getdate() - Day(getdate()) + 1) -1,105)
    
    select Convert(varchar(10),getdate(),105)
    
    select year(getdate())
    
    0 讨论(0)
提交回复
热议问题