ajlearningcom (4/18/2014)
declare @DateFrom DateTime = CONVERT(DATETIME, '01/01/2014', 103)declare @DateTo DateTime = CONVERT(DATETIME, '01/03/2014', 103)
;WITH CTE(dt)
AS
(
Select @DateFrom
Union All
Select DATEADD(d,1,dt)FROM CTE
Where dt<@DateTo
)
select DayName,CONVERT(varchar(50), dt, 103)as Date from (select 'Saturday' as DayName,dt from CTE
where DATENAME(dw,dt)In('Saturday') group by dt
Union
select 'Sunday'as DayName,dt from CTE
where DATENAME(dw,dt)In('Sunday'))as result order by dt
Reference : http://aj-learning.com/blog/get-weekend-date-sql-server-specific-date-range/
Please read the following article for why you should generally avoid rCTEs that "count" by 1 like yours does. There are several much more efficient ways to do the same thing.
[font="Arial Black"]Hidden RBAR: Counting with Recursive CTE's[/font][/url]
--Jeff Moden
Change is inevitable... Change for the better is not.