Search

Monday, July 2, 2012

Get number of days in a month using SQL

Use below code to get the number of days in a month, given Month and year.

DECLARE @MonthNumber AS VarChar(3), @YearNumber AS VarChar(4)
SET @MonthNumber = 'Feb'
SET @YearNumber = '2009'


SELECT datediff(day,CAST(('01-' + @MonthNumber + '-' + @YearNumber) AS DateTime),dateadd(day,-1,dateadd(month,1,CAST(('01-' + @MonthNumber + '-' + @YearNumber) AS DateTime)))) + 1

No comments:

Post a Comment