This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this puzzle, You have only two dates value, start-date and end-date. Find a total number of Sundays between given dates.
Output: Total 53 Sundays in year 2017
1 2 3 |
NumOfSunday ----------- 53 |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
declare @startdate datetime declare @enddate datetime set @startdate = '2017-01-01' set @EndDate = '2017-12-31' ;with datecte as ( select @startdate DateValue union all select DateValue + 1 from datecte where DateValue + 1 <= @enddate ) SELECT COUNT(1) as NumOfSunday FROM datecte WHERE DATENAME(weekday,dateValue)='Sunday' OPTION (maxrecursion 366) |
Leave a Reply