Last active
November 17, 2015 12:14
-
-
Save jspanos71/df449fd19e68ce1c9237 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare | |
@StartDate date | |
, @EndDate date | |
set @StartDate = '2015-11-14' | |
set @EndDate = '2015-11-17'; | |
declare @DateTable table | |
( | |
Date_ date | |
, Day_Of_Week varchar(20) | |
); | |
with | |
date_range AS | |
( | |
select dateadd(day, datediff(day, 0, @EndDate) - datediff(day, @StartDate, @EndDate), 0) as calc_date | |
union all | |
select dateadd(day, 1, calc_date) as calc_date | |
from date_range | |
where dateadd(day, 1, calc_date) <= @EndDate | |
) | |
, day_of_week as | |
( | |
select | |
dr.calc_date | |
, DATENAME(dw, dr.calc_date) as Day_Of_Week | |
from date_range as dr | |
) | |
insert into @DateTable (Date_, Day_Of_Week) | |
select calc_date, Day_Of_Week | |
from day_of_week | |
where not exists (select * from @DateTable d where calc_date = d.Date_ ) | |
option (maxrecursion 0) | |
select DaysOfWeek = | |
stuff( | |
(select ',' + dt.Day_Of_Week from @DateTable as dt order by dt.Date_ FOR XML PATH('') ) | |
,1,1,'') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment