How to determine the number of days in a month in SQL Server?

I need to determine the number of days in a month for a given date in SQL Server.

Is there a built-in function? If not, what should I use as a custom function?

+85
date sql datetime sql-server user-defined-functions
Mar 27 '09 at 18:46
source share
27 answers

You can use the following with the first day of the specified month:

datediff(day, @date, dateadd(month, 1, @date)) 

To make it work for each date:

 datediff(day, dateadd(day, 1-day(@date), @date), dateadd(month, 1, dateadd(day, 1-day(@date), @date))) 
+111
Mar 27 '09 at 19:00
source share
โ€” -

In SQL Server 2012, you can use EOMONTH (Transact-SQL) to get the last day of the month, and then you can use DAY (Transact-SQL) to get the number of days in a month.

 DECLARE @ADate DATETIME SET @ADate = GETDATE() SELECT DAY(EOMONTH(@ADate)) AS DaysInMonth 
+135
Feb 02 '13 at 9:40
source share

The most elegant solution: works for any @DATE

 DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,@DATE),0))) 

Throw it into a function or just use it in a string. This answers the original question without unnecessary junk in the other answers.

examples of dates from other answers:

SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'1/31/2009'),0))) Returns 31

SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'2404-feb-15'),0))) Returns 29

SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'2011-12-22'),0))) Returns 31

+22
Oct 25 '13 at 22:42
source share

Much easier ... try day(eomonth(@Date))

+18
Jun 05 '13 at 14:36
source share
 --Last Day of Previous Month SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))) --Last Day of Current Month SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))) --Last Day of Next Month SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))) 

Personally, however, I would do UDF for it if there is no built-in function ...

+11
Mar 27 '09 at 19:03
source share

I would suggest:

 SELECT DAY(EOMONTH(GETDATE())) 
+6
Apr 05 '17 at 7:07 on
source share

This code gets the number of days in the current month:

 SELECT datediff(dd,getdate(),dateadd(mm,1,getdate())) as datas 

Change getdate() to the date on which you want to count the days.

+3
Apr 28 '16 at 10:55
source share
  --- sql server below 2012--- select day( dateadd(day,-1,dateadd(month, 1, convert(date,'2019-03-01')))) -- this for sql server 2012-- select day(EOMONTH(getdate())) 
+2
Mar 22 '19 at 12:54
source share

You need to add a function, but it is simple. I use this:

 CREATE FUNCTION [dbo].[ufn_GetDaysInMonth] ( @pDate DATETIME ) RETURNS INT AS BEGIN SET @pDate = CONVERT(VARCHAR(10), @pDate, 101) SET @pDate = @pDate - DAY(@pDate) + 1 RETURN DATEDIFF(DD, @pDate, DATEADD(MM, 1, @pDate)) END GO 
+1
Mar 27 '09 at 19:01
source share
 SELECT Datediff(day, (Convert(DateTime,Convert(varchar(2),Month(getdate()))+'/01/'+Convert(varchar(4),Year(getdate())))), (Convert(DateTime,Convert(varchar(2),Month(getdate())+1)+'/01/'+Convert(varchar(4),Year(getdate()))))) as [No.of Days in a Month] 
+1
Mar 18 2018-12-18T00:
source share

Solution 1. Find the number of days in any month in which we are currently

 DECLARE @dt datetime SET @dt = getdate() SELECT @dt AS [DateTime], DAY(DATEADD(mm, DATEDIFF(mm, -1, @dt), -1)) AS [Days in Month] 

Solution 2. Find the number of days in this combined month of the month.

 DECLARE @y int, @m int SET @y = 2012 SET @m = 2 SELECT @y AS [Year], @m AS [Month], DATEDIFF(DAY, DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m - 1, 0)), DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m, 0)) ) AS [Days in Month] 
+1
Aug 21 '12 at 19:34
source share
 select datediff(day, dateadd(day, 0, dateadd(month, ((2013 - 1900) * 12) + 3 - 1, 0)), dateadd(day, 0, dateadd(month, ((2013 - 1900) * 12) + 3, 0)) ) 

Nice Simple and does not require the creation of any Work Fine features

+1
Apr 10 '13 at 8:39
source share

You need to create a function, but it is for your convenience. It works perfect, and I never came across any erroneous calculations using this function.

 CREATE FUNCTION [dbo].[get_days](@date datetime) RETURNS int AS BEGIN SET @date = DATEADD(MONTH, 1, @date) DECLARE @result int = (select DAY(DATEADD(DAY, -DAY(@date), @date))) RETURN @result END 

How it works: subtracting the number of days of a date from the date itself gives you the last day of the previous month. Thus, you need to add one month to the specified date, subtract the day number and get the day component of the result.

+1
Feb 18 '15 at 12:31 on
source share
 select add_months(trunc(sysdate,'MM'),1) - trunc(sysdate,'MM') from dual; 
+1
Apr 12 '19 at 6:42
source share

I supported Mehrdad, but it works too. :)

 CREATE function dbo.IsLeapYear ( @TestYear int ) RETURNS bit AS BEGIN declare @Result bit set @Result = cast( case when ((@TestYear % 4 = 0) and (@testYear % 100 != 0)) or (@TestYear % 400 = 0) then 1 else 0 end as bit ) return @Result END GO CREATE FUNCTION dbo.GetDaysInMonth ( @TestDT datetime ) RETURNS INT AS BEGIN DECLARE @Result int DECLARE @MonthNo int Set @MonthNo = datepart(m,@TestDT) Set @Result = case @MonthNo when 1 then 31 when 2 then case when dbo.IsLeapYear(datepart(yyyy,@TestDT)) = 0 then 28 else 29 end when 3 then 31 when 4 then 30 when 5 then 31 when 6 then 30 when 7 then 31 when 8 then 31 when 9 then 30 when 10 then 31 when 11 then 30 when 12 then 31 end RETURN @Result END GO 

Verify

 declare @testDT datetime; set @testDT = '2404-feb-15'; select dbo.GetDaysInMonth(@testDT) 
0
Mar 27 '09 at 19:33
source share

here is another one ...

 Select Day(DateAdd(day, -Day(DateAdd(month, 1, getdate())), DateAdd(month, 1, getdate()))) 
0
Mar 27 '09 at 19:53
source share

I know this question is old, but I thought I would share what I use.

 DECLARE @date date = '2011-12-22' /* FindFirstDayOfMonth - Find the first date of any month */ -- Replace the day part with -01 DECLARE @firstDayOfMonth date = CAST( CAST(YEAR(@date) AS varchar(4)) + '-' + CAST(MONTH(@date) AS varchar(2)) + '-01' AS date) SELECT @firstDayOfMonth 

and

 DECLARE @date date = '2011-12-22' /* FindLastDayOfMonth - Find what is the last day of a month - Leap year is handled by DATEADD */ -- Get the first day of next month and remove a day from it using DATEADD DECLARE @lastDayOfMonth date = CAST( DATEADD(dd, -1, DATEADD(mm, 1, FindFirstDayOfMonth(@date))) AS date) SELECT @lastDayOfMonth 

They can be combined to create one function to get the number of days in a month, if necessary.

0
Dec 23 '11 at 4:12
source share
 SELECT DAY(SUBDATE(ADDDATE(CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-1'), INTERVAL 1 MONTH), INTERVAL 1 DAY)) 

Nice 'n' Simple and does not require the creation of any functions

0
Jul 13 '12 at 13:22
source share

Mehrdad Afshari's answer is the most accurate, besides the usual answer, this question is based on a formal mathematical approach given by Curtis McEnroe on his blog https://cmcenroe.me/2014/12/05/days-in-month-formula.html

 DECLARE @date DATE= '2015-02-01' DECLARE @monthNumber TINYINT DECLARE @dayCount TINYINT SET @monthNumber = DATEPART(MONTH,@date ) SET @dayCount = 28 + (@monthNumber + floor(@monthNumber/8)) % 2 + 2 % @monthNumber + 2 * floor(1/@monthNumber) SELECT @dayCount + CASE WHEN @dayCount = 28 AND DATEPART(YEAR,@date)%4 =0 THEN 1 ELSE 0 END -- leap year adjustment 
0
Jul 14 '16 at 11:53 on
source share

To get a number. days a month, we can directly use Day (), available in SQL.

Follow the link posted at the end of my answer for SQL Server 2005/2008.

The following example and result from SQL 2012

 alter function dbo.[daysinm] ( @dates nvarchar(12) ) returns int as begin Declare @dates2 nvarchar(12) Declare @days int begin select @dates2 = (select DAY(EOMONTH(convert(datetime,@dates,103)))) set @days = convert(int,@dates2) end return @days end --select dbo.daysinm('08/12/2016') 

Result in SSMS SQL Server

  (no column name) 1 31 

process:

When EOMONTH is used, depending on the date format we use, it is converted to the SQL Server DateTime format. Then the EOMONTH () date output will be 2016-12-31, having 2016 as Year, 12 as Month and 31 as Days. This output, when passed to Day (), gives you the total number of days in a month.

If we want to get an instant result for verification, we can directly run the code below,

 select DAY(EOMONTH(convert(datetime,'08/12/2016',103))) 

or

 select DAY(EOMONTH(convert(datetime,getdate(),103))) 

for links to work in SQL Server 2005/2008/2012, please follow the following external link ...

Find the number of days per month in SQL

0
Dec 08 '16 at 7:39
source share
 DECLARE @m int SET @m = 2 SELECT @m AS [Month], DATEDIFF(DAY, DATEADD(DAY, 0, DATEADD(m, +@m -1, 0)), DATEADD(DAY, 0, DATEADD(m,+ @m, 0)) ) AS [Days in Month] 
0
Aug 29 '19 at 0:59
source share

For any date

 select DateDiff(Day,@date,DateAdd(month,1,@date)) 
-one
May 21 '13 at 14:09
source share
 DECLARE @date nvarchar(20) SET @date ='2012-02-09 00:00:00' SELECT DATEDIFF(day,cast(replace(cast(YEAR(@date) as char)+'-'+cast(MONTH(@date) as char)+'-01',' ','')+' 00:00:00' as datetime),dateadd(month,1,cast(replace(cast(YEAR(@date) as char)+'-'+cast(MONTH(@date) as char)+'-01',' ','')+' 00:00:00' as datetime))) 
-one
Jan 05 '15 at 11:42
source share

simple query in SQLServer2012:

select day (('20 -05-1951 22:00:00 '))

i tested for many dates and always returned the correct result

-one
Apr 18 '16 at 8:10
source share

select first_day = dateadd (dd, -1 * datepart (dd, getdate ()) + 1, getdate ()), LAST_DAY = DateAdd (dd, -1 * DatePart (dd, DateAdd (mm, 1, GETDATE ())) , DateAdd (mm, 1, GETDATE ())), no_of_days = 1 + dateiff (dd, dateadd (dd, -1 * datepart (dd, getdate ()) + 1, getdate ()), dateadd (dd, -1 * datepart (dd, dateadd (mm, 1, getdate ())), DateAdd (mm, 1, GETDATE ())))

replace any date with getdate to get the number of months on that specific date

-one
Jan 9 '17 at 5:17
source share
 DECLARE @date DATETIME = GETDATE(); --or '12/1/2018' (month/day/year) SELECT DAY(EOMONTH ( @date )) AS 'This Month'; SELECT DAY(EOMONTH ( @date, 1 )) AS 'Next Month'; 

result: this month 31

Next month 30

-one
Sep 10 '17 at 10:11 on
source share
 DECLARE @Month INT=2, @Year INT=1989 DECLARE @date DateTime=null SET @date=CAST(CAST(@Year AS nvarchar) + '-' + CAST(@Month AS nvarchar) + '-' + '1' AS DATETIME); DECLARE @noofDays TINYINT DECLARE @CountForDate TINYINT SET @noofDays = DATEPART(MONTH,@date ) SET @CountForDate = 28 + (@noofDays + floor(@noofDays/8)) % 2 + 2 % @noofDays + 2 * floor(1/@noofDays) SET @noofDays= @CountForDate + CASE WHEN @CountForDate = 28 AND DATEPART(YEAR,@date)%4 =0 THEN 1 ELSE 0 END PRINT @noofDays 
-one
Jan 31 '18 at 11:20
source share



All Articles