How many weekdays are there in a given YEAR, MONTH ?

Pandians
Posted by Pandians under Sql Server category on | Points: 40 | Views : 1756
Declare @Year Int, @Month Int

Select @Year = 2012
Select @Month = 2

;With CTEs
As
(
Select 0 Slno
Union All
Select Slno + 1 From CTEs Where Slno <=30
),
[Days]
As
(
Select DateName(Weekday,DATEADD(Day,Slno,Cast(@Year as varchar)+ '-' + Cast(@Month as varchar) + '-01')) [Day], DATEADD(Day,Slno,Cast(@Year as varchar)+ '-' + Cast(@Month as varchar) + '-01') [Date] From CTEs
)

select [Day], COUNT(1) [Days] from [Days] Where Month([Date]) =@Month
Group by [Day]
Result
Day	Days
---- -----
Friday 4
Monday 4
Saturday 4
Sunday 4
Thursday 4
Tuesday 4
Wednesday 5

Comments or Responses

Login to post response