Number of days left in current month

后端 未结 6 882
梦谈多话
梦谈多话 2021-01-15 03:44

Number of days left in a given month How do I find the number of days left in the current month? Example if current month is November and todays date is 16/11/2016 The Numb

6条回答
  •  不知归路
    2021-01-15 04:35

    Since this is sql server 2008 you can't use EOMonth (that was introduced in 2012 version).
    You have to do some date adds and date diffs:

    SELECT DATEDIFF(DAY, 
                    GETDATE(),
                    DATEADD(MONTH, 
                            1, 
                            DATEADD(DAY, 1 - DAY(GETDATE()), GETDATE()) 
                           ) 
                   ) - 1 
    

    explanations:
    DATEADD(DAY, 1 - DAY(GETDATE()), GETDATE()) gets the first day of the current month, the wrapping DATEADD adds one month, and the wrapping DATEDIFF returns the number of days between the current date and the first date of the next month. This is why you need to subtruct 1 to get the correct number of days.

提交回复
热议问题