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
Simply use the Datepart function:
declare @date date
set @date='16 Nov 2016'
select datediff(day, @date, dateadd(month, 1, @date)) - Datepart(DAY,@date)
DECLARE @date DATE SET @date='16 Nov 2016' SELECT DATEDIFF(DAY, @date,EOMONTH(@date))
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
Change your date to getdate()
declare @date date
set @date=GETDATE()
select datediff(day, @date, dateadd(month, 1, @date)) - DATENAME(DAY,GETDATE())
AS DaysLeft
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.
--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