﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Todd Fifield  / Calendar Tables III - Changing Business Rules / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 14:45:40 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>[quote][b]James A Skipwith (2/17/2011)[/b][hr]Todd,Nice article. I also have large calendar tables to deal with - as one of my clients uses a rolling four week cycle starting the first saturday before February the 2nd, so as well as fiscal weeks to deal with I also have week 1-4 four each four week block! I too do not worry about optimising the SQL to populate this, as performance is not a considuration here, accuracy is.Plus how come you get away with one page of forum questions where I have 4+ each time!Look forward to more articles.James[/quote]James,Don't know how I only got one page.That rolling 4 week block is an interesting business requirement.  I can see how a calendar table would make life easier.  I've found that with proper indexing of both the calendar table and the data tables performance can be better in most casesI'm planning on doing the next calendar table article on the dreaded 4-4-5 accounting periods where a fiscal quarter is composed of 13 weeks and it bears no relationship to calendar months.Todd Fifield</description><pubDate>Thu, 17 Feb 2011 12:25:32 GMT</pubDate><dc:creator>tfifield</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>[quote][b]ziangij (2/16/2011)[/b][hr]thanks for this post :-)One question - will it be good to add a column instead - say IsWeekEnd, IsWeekStart. Will it fare better or worse than the current query ?Thanks in advance.[/quote]Ziangij,I've tried it both ways.  Usually just using the date itself works better and you don't have to add any unnecessary flags to the tables.Todd Fifield</description><pubDate>Thu, 17 Feb 2011 12:20:06 GMT</pubDate><dc:creator>tfifield</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>[quote][b]Nicole Bowman (2/16/2011)[/b][hr]Thanks for the clear explanation. It is good to see a clean, simple way to implement a change in business rules without having to change reams of front end code. It shows good planning, and understanding of the business needs, and reminds me about the importance of the design phase of my projects - measure twice, cut once!Cheers,[/quote]Nicole,I love that little quote - measure twice, cut once!Glad you enjoyed the article.Todd Fifield</description><pubDate>Thu, 17 Feb 2011 12:18:15 GMT</pubDate><dc:creator>tfifield</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>[quote][b]CirquedeSQLeil (2/16/2011)[/b][hr]Thanks for continuing the series.[/quote]Jason,That means a lot to me coming from you.Todd Fifield</description><pubDate>Thu, 17 Feb 2011 12:16:23 GMT</pubDate><dc:creator>tfifield</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>Todd,Nice article. I also have large calendar tables to deal with - as one of my clients uses a rolling four week cycle starting the first saturday before February the 2nd, so as well as fiscal weeks to deal with I also have week 1-4 four each four week block! I too do not worry about optimising the SQL to populate this, as performance is not a considuration here, accuracy is.Plus how come you get away with one page of forum questions where I have 4+ each time!Look forward to more articles.James</description><pubDate>Thu, 17 Feb 2011 02:17:48 GMT</pubDate><dc:creator>James A Skipwith</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>thanks for this post :-)One question - will it be good to add a column instead - say IsWeekEnd, IsWeekStart. Will it fare better or worse than the current query ?Thanks in advance.</description><pubDate>Wed, 16 Feb 2011 22:26:28 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>Thanks for the clear explanation. It is good to see a clean, simple way to implement a change in business rules without having to change reams of front end code. It shows good planning, and understanding of the business needs, and reminds me about the importance of the design phase of my projects - measure twice, cut once!Cheers,</description><pubDate>Wed, 16 Feb 2011 14:19:37 GMT</pubDate><dc:creator>Nicole Bowman</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>Thanks for continuing the series.</description><pubDate>Wed, 16 Feb 2011 13:26:02 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>abacrotto,Thank you for your reply.  Yes, the creation of the table could be done set-based, however, it is only done once so I don't pay much attention to performance for just creating the Calendar Table.  Also, When I create a Calendar table I usually put more data into it.  For example I might add '2010 - Week 1' and so forth as a column for display purposes.  For a daily table I might add 'Monday', 'Tuesday', etc., so I can put whatever I want for a day name rather than depend on the DATENAME() function.  The while loop makes it easier to add these information only type columns.I hope you find calendar tables useful.Todd Fifield</description><pubDate>Wed, 16 Feb 2011 10:51:11 GMT</pubDate><dc:creator>tfifield</dc:creator></item><item><title>RE: Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>Hey!!! I liked the way you worked out the problem. I have a little comment for some of the code. You are using while loops in some of the code that you can replace with pure SQL code.The example is when creating the table with the weeks. The program cycles and inserts data into a table. I thinks this algorithm would help you in two ways. It's faster and easier to read.DECLARE @FromDate DATETIME, @ToDate DATETIMESELECT @FromDate = '20091228', @ToDate = '20111231'-- Number of days between the from and to datesDECLARE @Diff INTSELECT @Diff = DATEDIFF(DAY, @FromDate, @ToDate)SELECT TOP (@Diff / 7 + 1)	-- ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) gives us the week number	-- First day of week gets calculated with offset 0	DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 0, @FromDate) AS DateStart,	-- Last day of the week gets calculates with offset 6	DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 6, @FromDate) AS DateEndFROM	Sys.columns C1, Sys.columns C2As you can see I base it in ROW_NUMBER() function so, it will just run on SQL Server 2005 or above.Regards.Ariel from Argentina.</description><pubDate>Wed, 16 Feb 2011 08:28:22 GMT</pubDate><dc:creator>abacrotto</dc:creator></item><item><title>Calendar Tables III - Changing Business Rules</title><link>http://www.sqlservercentral.com/Forums/Topic1064742-2740-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/articles/T-SQL/72345/"&gt;Calendar Tables III - Changing Business Rules&lt;/A&gt;[/B]</description><pubDate>Wed, 16 Feb 2011 00:01:22 GMT</pubDate><dc:creator>tfifield</dc:creator></item></channel></rss>