<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: TSQL-Tuesday # 18: Using a Recursive CTE to Create a Calendar Table</title>
	<atom:link href="http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/feed/" rel="self" type="application/rss+xml" />
	<link>http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/</link>
	<description></description>
	<lastBuildDate>Mon, 02 Jul 2012 14:36:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Jason Cox</title>
		<link>http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/#comment-38</link>
		<dc:creator><![CDATA[Jason Cox]]></dc:creator>
		<pubDate>Tue, 15 Nov 2011 14:35:11 +0000</pubDate>
		<guid isPermaLink="false">https://joecasella.wordpress.com/?p=200#comment-38</guid>
		<description><![CDATA[Thanks for the nice Calendar table! 
Based on the 2nd comment, the following corrects the problem with the row assignments of the days in the rendered calendar grid:

CREATE PROCEDURE dbo.spGetMonthCalendar
	@Month INT = 1,
	@Year INT = 2011
AS BEGIN

	DECLARE @StartDate DATE
	DECLARE @EndDate DATE

	-- Create the start date value
	SET @StartDate = CONVERT(DATE, RIGHT(&#039;0000&#039; + CONVERT(VARCHAR, @Year),4) + &#039;-&#039; + RIGHT(&#039;00&#039; + CONVERT(VARCHAR, @Month), 2) + &#039;-01&#039;)
	-- Create the end date
	SET @EndDate = DATEADD(M,1,@StartDate)

	;WITH Sales_CTE (MonthDate)
	AS
	(
	Select 
		@StartDate
	UNION ALL
	Select DATEADD(dd, 1, MonthDate) as MonthDate 
		from Sales_CTE 
	WHERE 
		DATEADD(dd, 1, MonthDate) &lt; @EndDate
	)
	select 
		[1] as sun, 
		[2] as mon, 
		[3] as tue, 
		[4] as wed, 
		[5] as thu, 
		[6] as fri, 
		[7] as sat
	FROM
		(
		select 
			day(MonthDate) as MONTHDATE,
			datepart(weekday, MonthDate) AS DAYOFWEEK,
			DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, MonthDate), 0), MonthDate) +1 as ROW  -- this correctly determine which week of the month the day falls on (from http://www.berezniker.com/content/pages/sql/microsoft-sql-server/week-number-month)
			--row_number() over(order by MonthDate)/7 as ROW --this renders days to the wrong row sometimes
		from Sales_CTE 
		) as source_table
	pivot (
		sum(monthdate)
		for dayofweek in ([1],[2],[3],[4],[5],[6],[7])
		) as pivottable
END]]></description>
		<content:encoded><![CDATA[<p>Thanks for the nice Calendar table!<br />
Based on the 2nd comment, the following corrects the problem with the row assignments of the days in the rendered calendar grid:</p>
<p>CREATE PROCEDURE dbo.spGetMonthCalendar<br />
	@Month INT = 1,<br />
	@Year INT = 2011<br />
AS BEGIN</p>
<p>	DECLARE @StartDate DATE<br />
	DECLARE @EndDate DATE</p>
<p>	&#8211; Create the start date value<br />
	SET @StartDate = CONVERT(DATE, RIGHT(&#8217;0000&#8242; + CONVERT(VARCHAR, @Year),4) + &#8216;-&#8217; + RIGHT(&#8217;00&#8242; + CONVERT(VARCHAR, @Month), 2) + &#8216;-01&#8242;)<br />
	&#8211; Create the end date<br />
	SET @EndDate = DATEADD(M,1,@StartDate)</p>
<p>	;WITH Sales_CTE (MonthDate)<br />
	AS<br />
	(<br />
	Select<br />
		@StartDate<br />
	UNION ALL<br />
	Select DATEADD(dd, 1, MonthDate) as MonthDate<br />
		from Sales_CTE<br />
	WHERE<br />
		DATEADD(dd, 1, MonthDate) &lt; @EndDate<br />
	)<br />
	select<br />
		[1] as sun,<br />
		[2] as mon,<br />
		[3] as tue,<br />
		[4] as wed,<br />
		[5] as thu,<br />
		[6] as fri,<br />
		[7] as sat<br />
	FROM<br />
		(<br />
		select<br />
			day(MonthDate) as MONTHDATE,<br />
			datepart(weekday, MonthDate) AS DAYOFWEEK,<br />
			DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, MonthDate), 0), MonthDate) +1 as ROW  &#8212; this correctly determine which week of the month the day falls on (from <a href="http://www.berezniker.com/content/pages/sql/microsoft-sql-server/week-number-month" rel="nofollow">http://www.berezniker.com/content/pages/sql/microsoft-sql-server/week-number-month</a>)<br />
			&#8211;row_number() over(order by MonthDate)/7 as ROW &#8211;this renders days to the wrong row sometimes<br />
		from Sales_CTE<br />
		) as source_table<br />
	pivot (<br />
		sum(monthdate)<br />
		for dayofweek in ([1],[2],[3],[4],[5],[6],[7])<br />
		) as pivottable<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abk</title>
		<link>http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/#comment-36</link>
		<dc:creator><![CDATA[abk]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 14:07:35 +0000</pubDate>
		<guid isPermaLink="false">https://joecasella.wordpress.com/?p=200#comment-36</guid>
		<description><![CDATA[WITH Sales_CTE (date)
AS
-- Define the CTE query.
(
    Select cast(&#039;1 aug 2011&#039; as datetime) as date
    UNION ALL
    Select date + 1 as date from Sales_CTE where date +1 &lt; &#039;1 sep 2011&#039;
)
select [1] as sun, [2] as mon, [3] as tue, [4] as wed, [5] as thu, [6] as fri , [7] as sat
FROM 
(select day(date) as MONTHDATE,datepart(weekday,date) AS DAYOFWEEK,row_number() over(order by date)/7 as ROW from Sales_CTE ) as source_table
pivot ( sum(monthdate)
for dayofweek in ([1],[2],[3],[4],[5],[6],[7])) as pivottable]]></description>
		<content:encoded><![CDATA[<p>WITH Sales_CTE (date)<br />
AS<br />
&#8211; Define the CTE query.<br />
(<br />
    Select cast(&#8217;1 aug 2011&#8242; as datetime) as date<br />
    UNION ALL<br />
    Select date + 1 as date from Sales_CTE where date +1 &lt; &#039;1 sep 2011&#039;<br />
)<br />
select [1] as sun, [2] as mon, [3] as tue, [4] as wed, [5] as thu, [6] as fri , [7] as sat<br />
FROM<br />
(select day(date) as MONTHDATE,datepart(weekday,date) AS DAYOFWEEK,row_number() over(order by date)/7 as ROW from Sales_CTE ) as source_table<br />
pivot ( sum(monthdate)<br />
for dayofweek in ([1],[2],[3],[4],[5],[6],[7])) as pivottable</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Pusateri</title>
		<link>http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/#comment-13</link>
		<dc:creator><![CDATA[Bob Pusateri]]></dc:creator>
		<pubDate>Wed, 11 May 2011 02:55:19 +0000</pubDate>
		<guid isPermaLink="false">https://joecasella.wordpress.com/?p=200#comment-13</guid>
		<description><![CDATA[Hi Joe - 

Thanks for contributing both great posts to T-SQL Tuesday!]]></description>
		<content:encoded><![CDATA[<p>Hi Joe &#8211; </p>
<p>Thanks for contributing both great posts to T-SQL Tuesday!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
