To calculate the month number and number of days in the provided month in SQL Server.

Amatya
Posted by Amatya under Sql Server category on | Points: 40 | Views : 1176
declare @InputYear int
declare @InputMonth int
declare @InputDate date
select @InputYear = 2016
select @InputMonth = DATEPART(mm,CAST('february'+ ' 2016' AS DATETIME))
select @InputDate = cast(cast(@InputMonth as varchar(20)) + '/1/' + cast(@InputYear as varchar(4)) as datetime)

select @InputMonth as Month, datediff(day, dateadd(day, 1-day(@InputDate), @InputDate),
dateadd(month, 1, dateadd(day, 1-day(@InputDate), @InputDate))) as Days


Thanks
Amatya

Comments or Responses

Login to post response