Number of days left in current month

后端 未结 6 878
梦谈多话
梦谈多话 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:21

    Simply use the Datepart function:

    declare @date date
    set @date='16 Nov 2016'
    select datediff(day, @date, dateadd(month, 1, @date)) - Datepart(DAY,@date) 
    
    0 讨论(0)
  • 2021-01-15 04:28
    DECLARE @date DATE SET @date='16 Nov 2016' SELECT DATEDIFF(DAY, @date,EOMONTH(@date))
    
    0 讨论(0)
  • 2021-01-15 04:29

    Use EOMONTH and DATEDIFF functions

        declare @date date 
        set @date='16 Nov 2016'
        select datediff(day,@date,eomonth(@date)) as days_left
    

    Use below solution For Below versions of sql server 2012

        DECLARE @DATE DATE 
        SET @DATE='16 NOV 2016'
        SELECT   DATEDIFF(DAY,@DATE,DATEADD( MM,DATEDIFF(MM,0,@DATE)+1,0))-1 AS DAYS_LEFT
    
    0 讨论(0)
  • 2021-01-15 04:32

    Change your date to getdate()

         declare @date date 
         set @date=GETDATE()
         select datediff(day, @date, dateadd(month, 1, @date)) - DATENAME(DAY,GETDATE())
     AS DaysLeft
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-15 04:39
    --For SQL 2012 And Above Version Use Below Query to Get Count Of Days Left In A Month
    
    DECLARE @date DATE
    SET @date=GETDATE()
    SELECT DATEDIFF(DAY, @date,EOMONTH(@date)) 
    
    -- And for Sql Server 2008 Use Below Query to Get Count of Days Left for the Month
        DECLARE @date Date 
             SET @date=GETDATE()
             SELECT DATEDIFF(DAY, @date, DATEADD(MONTH, 1, @date)) - DATENAME(DAY,GETDATE())
         AS DaysLeft
    
    0 讨论(0)
提交回复
热议问题