<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Made2Mentor &#187; SQL</title>
	<atom:link href="http://www.made2mentor.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.made2mentor.com</link>
	<description>Data Warehousing, Microsoft Business Intelligence, and Other Cool Stuff</description>
	<lastBuildDate>Thu, 19 Jan 2012 19:41:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>User Defined Data Type Definition Script</title>
		<link>http://www.made2mentor.com/2012/01/user-defined-data-type-definition-script/</link>
		<comments>http://www.made2mentor.com/2012/01/user-defined-data-type-definition-script/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 19:41:18 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Code Samples]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6641</guid>
		<description><![CDATA[
			
				
			
		
<p>Recently I worked on a project where every data type was user defined. In case you weren&#8217;t aware, SQL Server supports the use of User Defined Data Types. These are custom data types which are based on the standard types. For example, if your data always uses a two character string for states, you may [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2012%2F01%2Fuser-defined-data-type-definition-script%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2012%2F01%2Fuser-defined-data-type-definition-script%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Recently I worked on a project where every data type was user defined. In case you weren&#8217;t aware, SQL Server supports the use of <a href="http://www.mssqltips.com/sqlservertip/1628/sql-server-user-defined-data-types-rules-and-defaults/" title="UDTs" target="_blank">User Defined Data Types</a>. These are custom data types which are based on the standard types. For example, if your data always uses a two character string for states, you may want to create a data type called UDT_State. </p>
<p>However, this project had 40 UDTs and their names were fairly ambiguous. So, I created a &#8220;cheat sheet&#8221; script to refer to when working in that environment. </p>
<p>First, let&#8217;s create some user defined data types.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">use</span> tempdb
go
<span style="color: #0000FF;">CREATE</span> TYPE <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>UDT_State<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>;
GO
<span style="color: #0000FF;">CREATE</span> TYPE <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>UDT_User_Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">Int</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>;
GO
<span style="color: #0000FF;">CREATE</span> TYPE <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>UDT_Cost<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>;
GO</pre></div></div>

<p>The following script is compatible with SQL 2005 through 2008 R2. I believe it also works on SQL 2000 but don&#8217;t have access to a server to test it.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> ST.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> UDTName,
       <span style="color: #0000FF;">CASE</span>
          <span style="color: #0000FF;">WHEN</span> ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%char'</span>
          <span style="color: #0000FF;">THEN</span>
             ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'('</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>ST.<span style="color: #202020;">max_length</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">')'</span>
          <span style="color: #0000FF;">WHEN</span> ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'numeric'</span> <span style="color: #808080;">OR</span> ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'decimal'</span>
          <span style="color: #0000FF;">THEN</span>
               ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span>
             <span style="color: #808080;">+</span> <span style="color: #FF0000;">'('</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>ST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">precision</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
             <span style="color: #808080;">+</span> <span style="color: #FF0000;">','</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span> <span style="color: #808080;">&#40;</span>ST.<span style="color: #202020;">scale</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">')'</span>
          <span style="color: #0000FF;">ELSE</span>
             ST1.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span>
       <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> BaseType,
       <span style="color: #0000FF;">CASE</span> ST.<span style="color: #202020;">is_nullable</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'NULL'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'NOT NULL'</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> Nullable
  <span style="color: #0000FF;">FROM</span>    sys.<span style="color: #202020;">types</span> ST
       <span style="color: #808080;">JOIN</span>
          sys.<span style="color: #202020;">types</span> ST1
       <span style="color: #0000FF;">ON</span> ST1.<span style="color: #202020;">user_type_id</span> <span style="color: #808080;">=</span> ST.<span style="color: #202020;">system_type_id</span>
 <span style="color: #0000FF;">WHERE</span> ST.<span style="color: #202020;">is_user_defined</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span></pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2012/01/UDT-Results.png"><img src="http://www.made2mentor.com/wp-content/uploads/2012/01/UDT-Results.png" alt="" title="UDT Results" width="310" height="122" class="alignleft size-full wp-image-6658" /></a></p>
<p>Special thanks to Jeff Rush (<a href="http://www.bidn.com/blogs/JeffRush">Blog</a>/<a href="http://twitter.com/jeffrush">Twitter</a>) for the idea to write this script. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2012/01/user-defined-data-type-definition-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Pass GetDate() as a Stored Procedure Parameter</title>
		<link>http://www.made2mentor.com/2011/11/how-to-pass-getdate-as-a-stored-procedure-parameter/</link>
		<comments>http://www.made2mentor.com/2011/11/how-to-pass-getdate-as-a-stored-procedure-parameter/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 16:00:56 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6590</guid>
		<description><![CDATA[
			
				
			
		
The Problem
<p>When you pass GetDate() as a parameter to a stored procedure, the following error is returned: </p>
<p>
EXEC dbo.ProcDate GetDate()</p>
<p>Msg 102, Level 15, State 1, Line 1
Incorrect syntax near &#8216;)&#8217;</p>
<p>When I ran into this problem, I found this helpful article which suggests that I should assign the value of GetDate() to variable and then pass [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F11%2Fhow-to-pass-getdate-as-a-stored-procedure-parameter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F11%2Fhow-to-pass-getdate-as-a-stored-procedure-parameter%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h3>The Problem</h3>
<p>When you pass GetDate() as a parameter to a stored procedure, the following error is returned: </p>
<blockquote><p>
EXEC dbo.ProcDate GetDate()</p>
<p>Msg 102, Level 15, State 1, Line 1<br />
Incorrect syntax near &#8216;)&#8217;</p></blockquote>
<p>When I ran into this problem, I found this <a href="http://blog.ninethsense.com/t-sql-pass-getdate-as-a-parameter-for-stored-procedure/" title="helpful article">helpful article</a> which suggests that I should assign the value of GetDate() to variable and then pass the variable as the parameter. While that does work, I wasn&#8217;t in a position where I could use a variable so I needed to find another solution. </p>
<h3>The Solution</h3>
<p>My solution was fairly simple. Define a default value for the parameter as GetDate() like so.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span>
      <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> name
         <span style="color: #0000FF;">FROM</span> sysobjects
        <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'ProcDate'</span> <span style="color: #808080;">AND</span> type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'P'</span><span style="color: #808080;">&#41;</span>
 <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">PROCEDURE</span> ProcDate
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> ProcDate
   @<span style="color: #0000FF;">Date</span> <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span>
<span style="color: #0000FF;">AS</span>
   <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">Date</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">coalesce</span> <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">Date</span>, <span style="color: #FF00FF;">Getdate</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
   <span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">Date</span>
GO</pre></div></div>

<p>Now the stored procedure works correctly. If a date is passed as a parameter, the stored procedure uses it, otherwise it defaults to GetDate().</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Exec</span>.<span style="color: #202020;">ProcDate</span> <span style="color: #FF0000;">'11/23/2011'</span> <span style="color: #008080;">-- returns 2011-11-23 00:00:00.000</span>
&nbsp;
<span style="color: #0000FF;">Exec</span>.<span style="color: #202020;">ProcDate</span>              <span style="color: #008080;">-- returns 2011-11-12 15:57:27.623</span></pre></div></div>

<p>Incidentally, I realize that this stored procedure isn&#8217;t &#8220;production ready&#8221; because it will fail if someone passes &#8220;Bleh&#8221; in as the parameter. I deliberately left out the error checking to give avoid clutter.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/11/how-to-pass-getdate-as-a-stored-procedure-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Ask for Technical Help</title>
		<link>http://www.made2mentor.com/2011/09/how-to-ask-for-technical-help/</link>
		<comments>http://www.made2mentor.com/2011/09/how-to-ask-for-technical-help/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 13:52:36 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Philosophy]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5364</guid>
		<description><![CDATA[
			
				
			
		
<p>Like many other technical bloggers, I receive requests for help on a regular basis. Since I&#8217;m constantly learning and evolving I approach others for help as well. A couple of years ago, I received an email from a new reader which began with: </p>
<p>Always two there are, no more, no less: a master and an [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F09%2Fhow-to-ask-for-technical-help%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F09%2Fhow-to-ask-for-technical-help%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Like many other technical bloggers, I receive requests for help on a regular basis. Since I&#8217;m constantly learning and evolving I approach others for help as well. A couple of years ago, I received an email from a new reader which began with: </p>
<blockquote><p>Always two there are, no more, no less: a master and an apprentice.</p></blockquote>
<p>The line is from Star Wars, of course. Being a huge geek, it&#8217;s easy to understand how his email got my attention right away. His approach was polite, respectful, and most importantly demonstrated that he had in fact read some of my blog. That got me thinking, what are some tips I could give my readers on how to ask for help? Of course, I couldn&#8217;t just produce a list. In my geekitude, I have prepared a list of movie scenes to explain my suggestions.</p>
<h3>Show Respect</h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/respect.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/respect.jpg" alt="" title="respect" width="330" height="200" class="aligncenter size-full wp-image-6458" /></a></p>
<blockquote><p>What have I ever done to make you treat me so disrespectfully? &#8211; Godfather </p></blockquote>
<p>Be respectful in your request and if at all possible, offer your friendship before you need help. Attend user group meetings and get to know people. Volunteer. If it&#8217;s a forum, do your best to participate and help others in the forum. As in the movie, you&#8217;re more likely to get help when you need it if you already belong to a group. </p>
<h3>Be Nice (Politeness)</h3>
<div class="wp-caption aligncenter" style="width: 330px"><a href="http://www.made2mentor.com/Images/Swayze.jpg"><img alt="" src="http://www.made2mentor.com/Images/Swayze.jpg" title="Dalton" width="320" height="240" /></a><p class="wp-caption-text">Be Nice</p></div>
<blockquote><p> If somebody gets in your face and calls you a &#038;^*%*$!!!, I want you to be nice. Ask him to walk. Be nice. If he won&#8217;t walk, walk him. But be nice. If you can&#8217;t walk him, one of the others will help you, and you&#8217;ll both be nice. I want you to remember that it&#8217;s a job. It&#8217;s nothing personal.- Dalton in Road House </p></blockquote>
<p>Don&#8217;t go overboard but a complimentary remark never hurts when asking for help. If you&#8217;ve seen them present, read their blog, or answer questions in forums, complement them on their abilities. Just make sure that your compliments are sincere. I can&#8217;t tell you how many &#8220;requests&#8221; I&#8217;ve gotten for help which sounded more like demands for help. People don&#8217;t tend to respond well to demands. </p>
<h3>Communicate Clearly </h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/tucker.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/tucker.jpg" alt="Chris Tucker" title="tucker" width="281" height="211" class="aligncenter size-full wp-image-6401" /></a></p>
<blockquote><p>Do you understand the words that are coming out of my mouth?!?!? &#8211; Detective Carter in Rush Hour</p></blockquote>
<p>Take some time and compose your question so it can be easily read and understood. If I have to waste cycles trying to decode your question, or need additional information, chances are that I won&#8217;t bother. In my experience, most people are like this. Depending on your problem, explain the steps you have taken to remedy your issue and the result of those attempts. Take time to use a code prettify-er before posting it and if possible the code to generate a sample data set people can use to help you. Make it easy for people to assist you. Now that I&#8217;ve just told you how complete your questions should be, remember that at the same time you should&#8230;.</p>
<h3>Be Brief</h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/inigo1.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/inigo1.jpg" alt="" title="inigo1" width="329" height="285" class="aligncenter size-full wp-image-6442" /></a></p>
<blockquote><p>Let me &#8216;splain&#8230; No, there is too much. Let me sum up. Buttercup is marry&#8217; Humperdinck in a little less than half an hour. So all we have to do is get in, break up the wedding, steal the princess, make our escape&#8230; after I kill Count Rugen &#8211; Inigo Montoya in The Princess Bride </p></blockquote>
<p>The longer your question, the less likely people are to read it. Therefore, it&#8217;s in your best interest to be as brief as possible while still providing the background information to solve your problem. Try to leave rants or opinion out of it and just state the facts. </p>
<h3>Work for Your Own Answers</h3>
<p><div id="attachment_6405" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/wax_on.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/wax_on.jpg" alt="" title="wax_on" width="400" height="300" class="size-full wp-image-6405" /></a><p class="wp-caption-text">Wax On Wax Off</p></div><br />
Before asking for help, do your due diligence. <a href="http://www.sqlskills.com/BLOGS/PAUL/post/RTFM-No-seriously-RTFM-Then-ask-your-question.aspx" title="RTFM" target="_blank">RTFM</a>, <a href="http://lmgtfy.com/" title="Google it">Google it</a>, and try to find the answer yourself. Nothing exasperates people more than being asked questions with simple answers that can be found in two minutes with a search engine. Make sure you indicate that you have already searched for the answer so others will take time to answer you. Sometimes the answer to a problem is as simple as knowing the right search terms. </p>
<h3>Ask for Guidance, Not Solutions</h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/finger.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/finger.jpg" alt="" title="finger" width="240" height="130" class="aligncenter size-full wp-image-6406" /></a></p>
<blockquote><p>
It is like a finger pointing away to the moon. Don&#8217;t concentrate on the finger or you will miss all that heavenly glory. Bruce Lee</p></blockquote>
<p>This goes along with RTFM. People are more likely to help you if you ask for general direction and not the complete solution to your problem. People do not want to do your homework, but they will direct you to information sources that will help you. A question framed like, &#8220;Can anyone recommend a resource where I can learn to&#8230;..&#8221; is likely to be answered. </p>
<h3>Be Patient</h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/bauer.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/bauer.jpg" alt="" title="bauer" width="385" height="280" class="aligncenter size-full wp-image-6414" /></a></p>
<blockquote><p>We&#8217;re running out of time!!!! &#8211; Jack Bauer in 24</p></blockquote>
<p>You often see this in forums. &#8220;Help!!!! My Server is down and I have no backups!!!!&#8221; Then five minutes later the person bumps their topic again because people haven&#8217;t responded fast enough. If you have a time sensitive emergency, then pay for immediate support. Don&#8217;t expect a forum to substitute for that kind of assistance. Your behavior won&#8217;t get your question answered any faster, and may in fact alienate people who would normally be willing to help you. </p>
<h3>Don&#8217;t be Greedy</h3>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/greed.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/greed.jpg" alt="" title="greed" width="262" height="174" class="aligncenter size-full wp-image-6456" /></a></p>
<blockquote><p>The point is ladies and gentlemen that greed, for lack of a better word, is good.- Gordon Gekko in Wall Street</p></blockquote>
<p>Yes, I know that the movie quote says the opposite, but stay with me. Similar to my last point, if you continuously ask questions without reciprocating, you may find that the answers stop coming. People don&#8217;t want to feel used, so you should prioritize your issues/questions and use community resources sparingly.  </p>
<h3>Don&#8217;t Argue</h3>
<div id="attachment_6447" class="wp-caption aligncenter" style="width: 360px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/paimei-2.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/paimei-2.jpg" alt="" title="paimei-2" width="350" height="190" class="size-full wp-image-6447" /></a><p class="wp-caption-text">Pai Mei</p></div>
<blockquote><p>That, my dearest, depends entirely on you. Now, remember: no sarcasm, no backtalk. At least not for the first year or so. You&#8217;re gonna have to let him warm up to you. &#8211; Bill in Kill Bill, Volume 2 (speaking about Pai Mei, plucker of eyes.)</p></blockquote>
<p>I&#8217;ve seen this more times than I can count and it&#8217;s often hilarious. Some newbie asks for help on a forum regarding how often he should be shrinking his database. Paul Randal (<a href="http://www.sqlskills.com/blogs/paul/">Blog</a>/<a href="http://twitter.com/paulrandal">Twitter</a>), who has forgotten more about SQL Server than most will ever learn, advises him that shrinking should be avoided. The newbie then proceeds to argue because he heard somewhere that database shrinking is akin to nirvana. Will Paul pluck your eye out like the infamous Pai Mei? Probably not, though I wouldn&#8217;t bet against him having those skills. However, if you argue with folks trying to help you, you&#8217;ll most likely get less assistance next time.  </p>
<h3>If Possible, Offer to Pay</h3>
<div id="attachment_6454" class="wp-caption aligncenter" style="width: 260px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/08/lone_starr.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/08/lone_starr.jpg" alt="" title="lone_starr" width="250" height="189" class="size-full wp-image-6454" /></a><p class="wp-caption-text">Give Me Paw!!!</p></div>
<blockquote><p>Listen! We’re not just doing this for the money! We’re doing this for a S*** LOAD of money! -Lone Starr in Spaceballs</p></blockquote>
<p>I&#8217;m not suggesting that everyone is motivated to help others by money, however it doesn&#8217;t hurt. In fact, you&#8217;ll most often find that people will refuse money that is coming out of our personal pocket. However, it does indicate to them that you are serious about getting help and answers. Also, offering to buy someone lunch or a drink or whatever goes a long way. I&#8217;ve bought more than a few meals in my day as a way of thanking people who helped me. </p>
<h3>Follow Up</h3>
<blockquote><p> &#8220;Just pay it forward.&#8221; &#8211; Thorsen in Pay It Forward</p></blockquote>
<p>If you ask a question on a forum, and you find the answer yourself, make sure to go back and update the thread with the answer you found. This gesture doesn&#8217;t benefit you personally, but it will help others when they have a similar problem. </p>
<p>Also, if a answer or suggestion helped you, follow up with the person and let them know. This seems obvious, but you&#8217;d be surprised how often people neglect to do this. Let&#8217;s face it, it feels good to receive a thank you message, and you&#8217;re more likely to get help with your next question. </p>
<p>So, that&#8217;s my list. Do you folks have any other suggestions? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/09/how-to-ask-for-technical-help/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Un-SQL Friday: Bad Interviews</title>
		<link>http://www.made2mentor.com/2011/07/un-sql-friday-bad-interviews/</link>
		<comments>http://www.made2mentor.com/2011/07/un-sql-friday-bad-interviews/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 13:33:31 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Consulting]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6333</guid>
		<description><![CDATA[
			
				
			
		
<p>I haven&#8217;t been participating in many of these blog memes because I have been so busy lately. A lot of my time was occupied with job hunting and since I just finished a series of interviews with different companies, this meme is perfectly timed. My good friend Jen McCown (Blog/Twitter), the fairer half of the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F07%2Fun-sql-friday-bad-interviews%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F07%2Fun-sql-friday-bad-interviews%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I haven&#8217;t been participating in many of these blog memes because I have been so busy lately. A lot of my time was occupied with job hunting and since I just finished a series of interviews with different companies, this meme is perfectly timed. My good friend Jen McCown (<a href="http://www.midnightdba.com/Jen/">Blog</a>/<a href="http://twitter.com/midnightdba">Twitter</a>), the fairer half of the Midnight DBAs, tagged the SQL Community asking for our <a href="http://www.midnightdba.com/Jen/2011/07/un-sql-friday-005-bad-interviews/" title="bad interview stories">bad interview stories</a>. I&#8217;m going to give two bad experiences and one surprisingly good experience. </p>
<p>First, I&#8217;ve been putting this announcement off, but I have changed employers. <strong>I am now a Senior Business Intelligence Consultant</strong> for a specialized consulting firm in Irving Texas. I want to thank the entire SQL Community for their help, training, and friendship on my way to this goal. To my Made2Manage friends, let me just say that this isn&#8217;t the end. I&#8217;ve started doing formal M2M consulting as part of this new position as well and still intend to release <a href="http://www.made2mentor.com/2010/10/announcing-project-m-data-analytics/">M-Data Analytics</a>. More information on that will be coming soon, so stay tuned.    </p>
<p>So, on with the stories.  </p>
<h3>Two Men Enter, One Man Leaves</h3>
<p>I answered a post from a local company that was looking for a Business Intelligence Developer with Data Warehousing experience. I had a first interview (telephone) that went very well and was called back for a second. They were very accommodating in that they scheduled the interviews at the end of the day. It&#8217;s easier to duck out at the end of the day for a &#8220;Doctor&#8217;s appointment&#8221; rather than losing 4 hours in the middle of the day. If an employee has a string of &#8220;appointments,&#8221; the employer starts to wonder. </p>
<p>Anyway, I arrived a few minutes early but there was a guy in a business suit already getting out of the car next to mine. He looked nervous and obviously there for an interview as well. He signed in at the front desk right before me and was seeing the same person. I immediately got a weird feeling. </p>
<p>We rode upstairs and sat in a small reception area for 15 or 20 minutes (though we were both on time) which was uncomfortable. I broke the ice and joked with him about whether he or I had made a scheduling mistake or perhaps we were going to have a cage match to see who got the position. We then discussed our work experience and I convinced him that he should be going to our local SSUG meetings. By the way, the receptionist wasn&#8217;t very good at hiding the fact that she was listening to everything we said. </p>
<p><object width="500" height="400"><param name="movie" value="http://www.youtube.com/v/3hQC3nkftrk?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3hQC3nkftrk?version=3" type="application/x-shockwave-flash" width="500" height="400" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>The BI Director finally came to get us and I asked him in a humorous way if there had been a scheduling mistake or if we were about to fight to the death like <a href="http://www.imdb.com/title/tt0089530/">Mad Max Beyond Thunderdome</a>. The director indicated that having both candidates interact was intentional and meant to raise stress levels. I said that it hadn&#8217;t raised my stress level, as I wasn&#8217;t there to compete with anyone else and had in fact recruited another PASS community member to boot. </p>
<h3>Liar Liar</h3>
<p>My second experience involves a recruiter. In the IT world, recruiters are a fact of life and Brent Ozar (<a href="http://www.brentozar.com/">Blog</a>/<a href="http://twitter.com/brento">Twitter</a>) has written a couple of <a href="http://www.brentozar.com/archive/2009/04/recruiters-are-not-your-friends-or-your-enemies/">wonderful articles</a> about <a href="http://www.brentozar.com/archive/2010/02/working-with-recruiters/">dealing with them</a>. </p>
<p>I spoke with a recruiter about a Data Warehouse position that they were trying to fill, but it would involve relocation. Relocation is problematic because of the costs involved, but even more so if the new location is in an economically depressed area. The job market in Dallas is very strong, particularly for business intelligence people, and I was reluctant to relocate somewhere with limited employment alternatives. I aced the technical telephone interview, largely thanks to all of the help and training I receive from other <a href="http://www.sqlpass.org/">PASS</a> members. However, when we discussed the terms of the job, I was informed that it would be a contract to hire and that relocation costs were not part of the package. </p>
<p>This was unacceptable to me as I wasn&#8217;t going to pay several thousands of dollars moving everything I own to an economic wasteland for a contracted hourly rate. There is no commitment on the employer&#8217;s part, and if things didn&#8217;t work out, I&#8217;d have to pay to move again to find a good position. Umm&#8230; no. </p>
<p>The recruiter called their client to discuss this and said that since the client was so impressed with my resume, they&#8217;d make an exception, hire me as an employee, and compensate me for relocation. I arranged an in person interview with the client, and paid the costs to travel to it. This wasn&#8217;t a big deal because I was traveling to this area anyway. </p>
<p>The interview went well and I found the hiring manager to be a straight forward and nice guy. He then offered me the job and asked when I could start. I was rather taken aback because we hadn&#8217;t discussed the actual salary, benefits, and relocation package. When I asked him about those, he was suprised and said, &#8220;What do you mean? This is a contract to hire job and there is no relocation package. We don&#8217;t have to provide that because the job market here is so poor.&#8221; Ugh. </p>
<p>I then explained to him exactly what the recruiter told me and offered to send him a copy of the e-mail to corroborate my &#8220;story.&#8221; He asked me to wait a moment and as I sat there, he called the recruiter. The conversation went like this. </p>
<p>&#8220;Ms. Smith (name withheld to protect the guilty), what did you tell Mr. Stein about my job terms?&#8221; Pause&#8230;. and then more forcefully, &#8220;No, what EXACTLY did you tell Mr. Stein?&#8221; Longer Pause&#8230; &#8220;Precisely what did you hope to gain by wasting his time and mine this way?&#8221; Pause&#8230;. &#8220;Well Ms. Smith, have Mr. Jones, the head of your company, call me later so I can explain to him why we will never use your company again.&#8221; Pause&#8230; &#8220;No, you simply cannot treat people this way as it reflects poorly on me and my company. Good Day.&#8221; </p>
<p>After he hung up we shook hands and he apologized for wasting my time. I told him that I enjoyed speaking with him anyway and eventually added him to my LinkedIn network. It may not have resulted in a job, but it was a hilarious experience anyway. </p>
<h3>Diamond in the Rough</h3>
<p>A few weeks ago, I went to a Qlikview demonstration at the request of my previous employer. To set the scene, I brought my laptop and such because I was going to a <a href="http://northtexas.sqlpass.org/">North Texas SQL Server User Group</a> meeting immediately afterward, but was dressed very casually in shorts and a video gaming t-shirt for the same reason. I wasn&#8217;t trying to impress anyone and certainly didn&#8217;t expect to find a potential employer there.<br />
<div id="attachment_6365" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/07/8_bit_imagination.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2011/07/8_bit_imagination.jpg" alt="http://www.thinkgeek.com/tshirts-apparel/unisex/gaming/9890/" title="8_bit_imagination" width="400" height="520" class="size-full wp-image-6365" /></a><p class="wp-caption-text">Perfect Interview Attire</p></div></p>
<p>Anyway, I was surrounded by a group of business people, typically dressed in suits or other business dress and obviously stood out. I noticed a a guy with brown hair, goatee, and glasses who entered with a group of people and I did a double take. I thought he was Andy Leonard (<a href="http://sqlblog.com/blogs/andy_leonard/">Blog</a>/<a href="http://twitter.com/andyleonard">Twitter</a>). By chance they sat down near me so I struck up a conversation with the guy asking him what he did. He&#8217;s a Microsoft BI DBA, so I asked him if he had heard of Andy, and he gave me a weird look, because this whole situation was fairly unusual. I opened up my laptop and found a picture of Andy on the internet and the guy agreed that they were likely twins separated at birth. Anyway, the gentleman next to him, who was obviously the doppleganger&#8217;s Director, was listening to our conversation and asked me about my background. I told him what I did, how I knew Andy, my work with the <a href="http://northtexas.sqlpass.org/">North Texas SQL Server User Group</a>, etc. I didn&#8217;t realize it, but I was being interviewed. The company was looking for a talented, outgoing, business intelligence person and the Director saw something in me that he liked. </p>
<p>The Director sent me an email asking me to lunch and I had my second interview at Chili&#8217;s while wearing business casual dress. In fact, I was wearing my <a href="http://www.sqlsaturday.com/67/eventhome.aspx" title="Chicago SQL Saturday">Chicago SQL Saturday</a> Presenter&#8217;s shirt. The interview had a technical portion, but mostly it was about my personality, the current team, and how to mesh the two together. We were just two guys talking about our mutual love of all things Data. </p>
<p>The rest is history. I now work for the new firm, and am getting the experience and being challenged every day.</p>
<p>So, what about you? Any of you have any bad or good interview stories?  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/07/un-sql-friday-bad-interviews/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating a Date Table/Dimension for SQL Server 2008.</title>
		<link>http://www.made2mentor.com/2011/06/creating-a-date-tabledimension-for-sql-server-2008/</link>
		<comments>http://www.made2mentor.com/2011/06/creating-a-date-tabledimension-for-sql-server-2008/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 14:00:50 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6176</guid>
		<description><![CDATA[
			
				
			
		
<p>In a previous article, I listed the benefits of using a dedicated date table and included a customizable script which enables you to quickly create your own version. One of my readers pointed out that he uses the date datatype, rather than using the smart integer key method, when working with SQL 2008+ databases. The [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F06%2Fcreating-a-date-tabledimension-for-sql-server-2008%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F06%2Fcreating-a-date-tabledimension-for-sql-server-2008%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In a <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">previous article</a>, I listed the benefits of using a dedicated date table and included a customizable script which enables you to quickly create your own version. One of my readers pointed out that he uses the date datatype, rather than using the smart integer key method, when working with SQL 2008+ databases. The smart date key is recommended by the Kimball Group and others.  </p>
<p>I performed <a href="http://www.made2mentor.com/2011/05/date-vs-integer-datatypes-as-primary-key-for-date-dimensions/">several tests</a> comparing the performance of Date vs. Integer datatype joins and found the Date joins to perform faster as well. </p>
<p>As a result, I&#8217;ve modified my script to create a Date datatype dimension have begun using it. As before, it is in two parts. The first script can be <a href='http://www.made2mentor.com/wp-content/uploads/2011/06/Date-Dimension-Base-Script-for-2008.txt'>found here</a>. Open the script, replace DateDatabase with the database name of your choice, and run it to create the function. </p>
<p>Once again, I need to state that I created this script from a function called <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519">F_TABLE_DATE by Michael Valentine Jones</a> from <a href="http://www.sqlteam.com/">SQLTeam</a> and have since gotten his permission to distribute it. That function is called in the statement below to create the date table/dimension.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span>
     <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>
        <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
       <span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span> <span style="color: #808080;">AND</span> TABLE_NAME <span style="color: #808080;">=</span> <span style="color: #FF0000;">'DimDate'</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">BEGIN</span>
        <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DimDate</span>;
  <span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> DTE.<span style="color: #0000FF;">Date</span>,
       DTE.<span style="color: #202020;">NextDayDate</span>,
       DTE.<span style="color: #202020;">CalendarYear</span>,
       DTE.<span style="color: #202020;">CalendarYearQuarter</span>,
       DTE.<span style="color: #202020;">CalendarYearMonth</span>,
       DTE.<span style="color: #202020;">CalendarYearDayOfYear</span>,
       DTE.<span style="color: #202020;">CalendarQuarter</span>,
       DTE.<span style="color: #202020;">CalendarMonth</span>,
       DTE.<span style="color: #202020;">CalendarDayOfYear</span>,
       DTE.<span style="color: #202020;">CalendarDayOfMonth</span>,
       DTE.<span style="color: #202020;">CalendarDayOfWeek</span>,
       DTE.<span style="color: #202020;">CalendarYearName</span>,
       DTE.<span style="color: #202020;">CalendarYearQuarterName</span>,
       DTE.<span style="color: #202020;">CalendarYearMonthName</span>,
       DTE.<span style="color: #202020;">CalendarYearMonthNameLong</span>,
       DTE.<span style="color: #202020;">CalendarQuarterName</span>,
       DTE.<span style="color: #202020;">CalendarMonthName</span>,
       DTE.<span style="color: #202020;">CalendarMonthNameLong</span>,
       DTE.<span style="color: #202020;">WeekdayName</span>,
       DTE.<span style="color: #202020;">WeekdayNameLong</span>,
       DTE.<span style="color: #202020;">CalendarStartOfYearDate</span>,
       DTE.<span style="color: #202020;">CalendarEndOfYearDate</span>,
       DTE.<span style="color: #202020;">CalendarStartOfQuarterDate</span>,
       DTE.<span style="color: #202020;">CalendarEndOfQuarterDate</span>,
       DTE.<span style="color: #202020;">CalendarStartOfMonthDate</span>,
       DTE.<span style="color: #202020;">CalendarEndOfMonthDate</span>,
       DTE.<span style="color: #202020;">QuarterSeqNo</span>,
       DTE.<span style="color: #202020;">MonthSeqNo</span>,
       DTE.<span style="color: #202020;">FiscalYearName</span>,
       DTE.<span style="color: #202020;">FiscalYearPeriod</span>,
       DTE.<span style="color: #202020;">FiscalYearDayOfYear</span>,
       DTE.<span style="color: #202020;">FiscalYearWeekName</span>,
       DTE.<span style="color: #202020;">FiscalSemester</span>,
       DTE.<span style="color: #202020;">FiscalQuarter</span>,
       DTE.<span style="color: #202020;">FiscalPeriod</span>,
       DTE.<span style="color: #202020;">FiscalDayOfYear</span>,
       DTE.<span style="color: #202020;">FiscalDayOfPeriod</span>,
       DTE.<span style="color: #202020;">FiscalWeekName</span>,
       DTE.<span style="color: #202020;">FiscalStartOfYearDate</span>,
       DTE.<span style="color: #202020;">FiscalEndOfYearDate</span>,
       DTE.<span style="color: #202020;">FiscalStartOfPeriodDate</span>,
       DTE.<span style="color: #202020;">FiscalEndOfPeriodDate</span>,
       DTE.<span style="color: #202020;">ISODate</span>,
       DTE.<span style="color: #202020;">ISOYearWeekNo</span>,
       DTE.<span style="color: #202020;">ISOWeekNo</span>,
       DTE.<span style="color: #202020;">ISODayOfWeek</span>,
       DTE.<span style="color: #202020;">ISOYearWeekName</span>,
       DTE.<span style="color: #202020;">ISOYearWeekDayOfWeekName</span>,
       DTE.<span style="color: #202020;">DateFormatYYYYMMDD</span>,
       DTE.<span style="color: #202020;">DateFormatYYYYMD</span>,
       DTE.<span style="color: #202020;">DateFormatMMDDYEAR</span>,
       DTE.<span style="color: #202020;">DateFormatMDYEAR</span>,
       DTE.<span style="color: #202020;">DateFormatMMMDYYYY</span>,
       DTE.<span style="color: #202020;">DateFormatMMMMMMMMMDYYYY</span>,
       DTE.<span style="color: #202020;">DateFormatMMDDYY</span>,
       DTE.<span style="color: #202020;">DateFormatMDYY</span>,
       DTE.<span style="color: #202020;">WorkDay</span>,
       DTE.<span style="color: #202020;">IsWorkDay</span>
       <span style="color: #0000FF;">into</span> dbo.<span style="color: #202020;">DimDate</span>
 <span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">F_TABLE_DATE</span> <span style="color: #808080;">&#40;</span> <span style="color: #FF0000;">'20000101'</span>,<span style="color: #FF0000;">'20351231'</span> <span style="color: #808080;">&#41;</span> DTE
 <span style="color: #0000FF;">order</span> <span style="color: #0000FF;">by</span> <span style="color: #000;">1</span>
 Go
&nbsp;
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DimDate</span>
<span style="color: #0000FF;">ADD</span>  <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Date</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">FILLFACTOR</span> <span style="color: #808080;">=</span> <span style="color: #000;">100</span>
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">INDEX</span> idx_Dates
<span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">DimDate</span> 
	<span style="color: #808080;">&#40;</span>NextDayDate<span style="color: #808080;">&#41;</span>
GO</pre></div></div>

<p>My example above generates every day through 2035, but nearly any date range can be used. As before, if you don&#8217;t want to have such a comprehensive Date Dimension or there are fields you don&#8217;t need, simply comment out items from the Select into query. </p>
<p>Querying against the Date Dimension is done the same way as with my previous script.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  MY.<span style="color: #0000FF;">date</span>
 ,DTE.<span style="color: #202020;">CalendarYear</span>
 ,DTE.<span style="color: #202020;">CalendarMonthName</span>
<span style="color: #0000FF;">FROM</span>
    MyTable MY
  <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
    DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> DTE
  <span style="color: #0000FF;">ON</span> MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&gt;=</span> DTE.<span style="color: #0000FF;">date</span> <span style="color: #808080;">AND</span>
     MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&lt;</span> DTE.<span style="color: #202020;">NextDayDate</span></pre></div></div>

<p>Any questions or suggestions for improvement? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/06/creating-a-date-tabledimension-for-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLRally 2012 is Coming to Big D!</title>
		<link>http://www.made2mentor.com/2011/06/sqlrally-2012-is-coming-to-big-d/</link>
		<comments>http://www.made2mentor.com/2011/06/sqlrally-2012-is-coming-to-big-d/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 13:14:24 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[PASS]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6153</guid>
		<description><![CDATA[
			
				
			
		
<p>Last week PASS announced that Dallas and the North Texas SQL Server User Group have been awarded SQLRally 2012. This is great news for all of the nearby PASS User Groups and DBA&#8217;s.   </p>
<p>We&#8217;ve put on three successful SQL Saturday events in 18 months, and I&#8217;m proud to have been on the planning [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F06%2Fsqlrally-2012-is-coming-to-big-d%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F06%2Fsqlrally-2012-is-coming-to-big-d%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Last week <a href="http://www.sqlpass.org/">PASS</a> announced that Dallas and the <a href="http://northtexas.sqlpass.org/">North Texas SQL Server User Group</a> have been awarded <a href="http://www.sqlpass.org/Community/PASSBlog/entryid/127/Introducing-The-PASS-SQLRally.aspx">SQLRally 2012</a>. This is great news for all of the nearby PASS User Groups and DBA&#8217;s.   </p>
<p>We&#8217;ve put on three successful SQL Saturday events in 18 months, and I&#8217;m proud to have been on the planning committee for each. We&#8217;re a dedicated, cohesive team, so SQLRally is in good hands. I&#8217;m so geeked that we&#8217;re hosting the event and I just wanted to take a moment to thank the other members of the team who&#8217;ve worked so hard to build a strong community in Dallas.<br />
<div id="attachment_3987" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.made2mentor.com/wp-content/uploads/2010/10/dave_ryan_vic.jpg"><img src="http://www.made2mentor.com/wp-content/uploads/2010/10/dave_ryan_vic.jpg" alt="Dave Ryan Vic" title="dave_ryan_vic" width="640" height="427" class="size-full wp-image-3987" /></a><p class="wp-caption-text">Ryan, Vic, and yours truly at SQL Sat 56 BI Edition. </p></div></p>
<ul>
<li>Sri Sridhara (<a href="http://sqlrocks.com/">Blog</a>/<a href="http://twitter.com/sqlrocks">Twitter</a>). Sri deserves a lot of the credit since he worked so hard on the proposals.</li>
<li>Sean McCown (<a href="http://www.midnightdba.com/DBARant/">Blog</a>) and Jen McCown (<a href="http://www.midnightdba.com/Jen/">Blog</a>/<a href="http://twitter.com/midnightdba">Twitter</a>)</li>
<li>Tim Mitchell (<a href="http://www.timmitchell.net/">Blog</a>/<a href="http://twitter.com/tim_mitchell">Twitter</a>)</li>
<li>Ryan Adams (<a href="http://www.ryanjadams.com/">Blog</a>/<a href="http://twitter.com/ryanjadams">Twitter</a>)</li>
<li>Vic Prabhu (<a href="https://twitter.com/SQLJackal">Twitter</a>)</li>
<li>Paul Hunter (<a href="https://twitter.com/SqlNightOwl">Twitter</a>)</li>
<li>Andy Eggers</li>
<li>Ganesh Gopalakrishnan</li>
</ul>
<p>You folks are awesome, and I&#8217;m glad to call you friends. </p>
<p>Bring on SQLRally! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/06/sqlrally-2012-is-coming-to-big-d/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Date vs Integer Datatypes as Primary Key for Date Dimensions</title>
		<link>http://www.made2mentor.com/2011/05/date-vs-integer-datatypes-as-primary-key-for-date-dimensions/</link>
		<comments>http://www.made2mentor.com/2011/05/date-vs-integer-datatypes-as-primary-key-for-date-dimensions/#comments</comments>
		<pubDate>Fri, 20 May 2011 16:59:06 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Performance Tuning]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=6003</guid>
		<description><![CDATA[
			
				
			
		
<p>Every Kimball Group book I&#8217;ve read, as well as every Data Warehouse class I&#8217;ve attended, has indicated that a Date Dimension Primary Key should be a smart integer key in the format YYYYMMDD (20110518) so I&#8217;ve always built my Date tables that way. However, Barnaby (Blog/Twitter) pointed out that I should be using a Date [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F05%2Fdate-vs-integer-datatypes-as-primary-key-for-date-dimensions%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F05%2Fdate-vs-integer-datatypes-as-primary-key-for-date-dimensions%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Every Kimball Group book I&#8217;ve read, as well as every Data Warehouse class I&#8217;ve attended, has indicated that a Date Dimension Primary Key should be a smart integer key in the format YYYYMMDD (20110518) so I&#8217;ve always built my Date tables that way. However, Barnaby (<a href="http://www.b4pjs.co.uk/">Blog</a>/<a href="http://twitter.com/B4PJS">Twitter</a>) pointed out that I should be using a Date Datatype key in SQL 2008 instead. His reasoning is sound in that the new Date datatype requires 3 bytes while an Int requires 4. This saves space and memory during processing and still facilitates table partitioning by year. The most obvious downside is that an unknown member value is required for Fact Table processing. However, I always use 19000101 as my unknown member, which can be entered as a date value anyway. </p>
<p>When I inquired on the internets about this prospect, I found conflicting opinions. One cited downside of using Date datatype was that the SQL Query Optimizer was more efficient when joining on Integers rather than dates or character types. However, I couldn&#8217;t find any test results involving the Date datatype to back that up so I thought I&#8217;d test it myself. </p>
<p>Let me preface the rest of this article with a disclaimer. I am not a query tuning expert, nor do I play one on TV. However, I ran the basics of these tests against the great Grant Fritchey (<a href="http://www.scarydba.com/">Blog</a>/<a href="https://twitter.com/gfritchey">Twitter</a>) and am grateful to him for the guidance. </p>
<h3>Setup</h3>
<p>I used my <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">Date Table Script</a> to generate two date tables, one with a smart integer key called DateTableInteger and another called DateTableDate with a Date datatype primary key. The primary key field for each is called DateID. Each table included an index on the [Date] column, which is a DateTime type in DateTableInteger and a Date datatype in DateTableDate. Each table is identical in every other way. </p>
<p>I created two source tables utilizing code written by Michael Valentine Jones from <a href="http://www.sqlteam.com/">SQLTeam</a> using his <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685">method of generating random numbers</a> which can be used to <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65910">create a column of random dates</a>. For this test I created two tables with a single field of two million records each with dates ranging from 1/1/2000 to 12/31/2020. The table with the Date datatype is called DateSource and the other with smart integer keys is called IntSource. Each source table has exactly the same list of dates as well for test conformity.</p>
<h3>Tested Queries and Methodology</h3>
<p>I used three simple queries for my tests. Each represents a common scenario where the user needs to select records from a specific year.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">set</span> <span style="color: #0000FF;">statistics</span> io <span style="color: #0000FF;">on</span>
<span style="color: #0000FF;">set</span> <span style="color: #0000FF;">statistics</span> <span style="color: #0000FF;">time</span> <span style="color: #0000FF;">on</span>
&nbsp;
<span style="color: #0000FF;">Select</span> DT.<span style="color: #0000FF;">Date</span> <span style="color: #0000FF;">From</span> IntSource ISO <span style="color: #008080;">-- Integer Type Source</span>
join DateTableInteger DT <span style="color: #0000FF;">on</span> DT.<span style="color: #202020;">DateId</span> <span style="color: #808080;">=</span> ISO.<span style="color: #202020;">IntColumn</span>
<span style="color: #0000FF;">Where</span> DT.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'1/1/2010'</span> and DT.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">&lt;</span> <span style="color: #FF0000;">'1/1/2011'</span>
&nbsp;
<span style="color: #0000FF;">Select</span> DT.<span style="color: #0000FF;">Date</span> <span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">DateSource</span> DS <span style="color: #008080;">-- Date Type Source</span>
join DateTableDate DT <span style="color: #0000FF;">on</span> DT.<span style="color: #202020;">DateId</span> <span style="color: #808080;">=</span> DS.<span style="color: #202020;">DateColumn</span>
<span style="color: #0000FF;">Where</span> DT.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'1/1/2010'</span> and DT.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">&lt;</span> <span style="color: #FF0000;">'1/1/2011'</span>
&nbsp;
<span style="color: #0000FF;">Select</span> DS.<span style="color: #202020;">DateColumn</span> <span style="color: #0000FF;">From</span> dbo.<span style="color: #202020;">DateSource</span> DS <span style="color: #008080;">-- Date Type bypassing join. </span>
<span style="color: #0000FF;">Where</span> DS.<span style="color: #202020;">DateColumn</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF0000;">'1/1/2010'</span> and DS.<span style="color: #202020;">DateColumn</span> <span style="color: #808080;">&lt;</span> <span style="color: #FF0000;">'1/1/2011'</span></pre></div></div>

<p>The first query is an integer join, the second utilizes a Date datatype join, and the third is Date datatype but doesn&#8217;t require a join. I realize that the two field values in the second query, DateID and Date, are identical but I used it for consistency and it represents a scenario where the join is used to pull additional information such as Calendar Month. In my experience many queries against a Data Warehouse don&#8217;t require additional information about a date anyway, and I suspected that the greatest performance benefit would be found when the extraneous join was removed. In my testing, the Integer Join represents my control, the base to which I compare other values. Each query returns the same 100,086 records. </p>
<p>I performed two rounds of tests. The first used non-indexed source values and in the second I added a clustered index to each of the source tables, as Fact Tables often use a date field for their clustered indexes.</p>
<p>Prior to testing each query set, I called dbcc freeproccache and dbcc dropcleanbuffers. I then ran the query set once to eliminate compile time which I was not interested in. Each query was executed 10 times and I averaged the CPU and elapsed time to eliminate other factors. Note that this is a production level server, Dell R510 with Raid 10 arrays of 15K rpm drives, that has not yet been put in production so nothing should be running while I tested. The server has a setting of MAXDOP 1.</p>
<h3>Results</h3>
<p>All recorded times are in milliseconds (ms).<br />
<a href="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype11.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype11.png" alt="" title="date_datatype1" width="698" height="204" class="aligncenter size-full wp-image-6090" /></a></p>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype_3.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype_3.png" alt="" title="date_datatype_3" width="472" height="236" class="aligncenter size-full wp-image-6062" /></a></p>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype21.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype21.png" alt="" title="date_datatype2" width="754" height="196" class="aligncenter size-full wp-image-6091" /></a><br />
<a href="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype_4.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/05/date_datatype_4.png" alt="" title="date_datatype_4" width="462" height="231" class="aligncenter size-full wp-image-6063" /></a></p>
<h3>Conclusions</h3>
<p>Obviously using a Date datatype instead of the smart integer will save space on disk and in memory, however the savings are not dramatic. For example, the space difference is less than 2MB with two million records. </p>
<p>Also, in my opinion, the performance differences between the Integer and Date datatype joins isn&#8217;t statistically relevant. I consider performance deltas of less than 5% to be a wash because my testing methodology isn&#8217;t exhaustive. The clustered index date join outperformed the integer join by 24% but trailed it by 5% when not indexed. However, I find it interesting that in both sets of tests the number of logical reads is less for the Date datatype join than the integer join. </p>
<p>As I expected, <strong>the Date datatype key method easily outperforms the other two when a join is not necessary</strong>. The 52% performance boost without indexes and 48% for clustered indexes is considerable and were reflected in a second test run I performed to verify. </p>
<p>Based on these results, I am going to switch to using the Date datatype in my data warehouse projects using SQL Server 2008. Next week, I&#8217;ll post an updated script to reflect this. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/05/date-vs-integer-datatypes-as-primary-key-for-date-dimensions/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Come See Me Present Wednesday at the Fort Worth SSUG.</title>
		<link>http://www.made2mentor.com/2011/05/come-see-me-present-wednesday-at-the-fort-worth-ssug/</link>
		<comments>http://www.made2mentor.com/2011/05/come-see-me-present-wednesday-at-the-fort-worth-ssug/#comments</comments>
		<pubDate>Mon, 16 May 2011 16:54:42 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[Presenting]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5982</guid>
		<description><![CDATA[
			
				
			
		
<p>This Wednesday I&#8217;ll be presenting at the Fort Worth SQL Server User Group. Come see me present:</p>
Data Warehousing &#8211; How to Convince &#8220;The Bobs&#8221;
<p class="wp-caption-text">I got a meeting with the Bobs in a couple of minutes... </p>
<p>Building your first Data Warehouse is a long, and often difficult process. How can you get your boss to [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F05%2Fcome-see-me-present-wednesday-at-the-fort-worth-ssug%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F05%2Fcome-see-me-present-wednesday-at-the-fort-worth-ssug%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This Wednesday I&#8217;ll be presenting at the <a href="http://fwssug.org/wfw/">Fort Worth SQL Server User Group</a>. Come see me present:</p>
<h3>Data Warehousing &#8211; How to Convince &#8220;The Bobs&#8221;</h3>
<div id="attachment_5656" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.made2mentor.com/wp-content/uploads/2011/03/office_space_legos.jpg"><img class="size-full wp-image-5656" title="office_space_legos" src="http://www.made2mentor.com/wp-content/uploads/2011/03/office_space_legos.jpg" alt="" width="500" height="375" /></a><p class="wp-caption-text">I got a meeting with the Bobs in a couple of minutes... </p></div>
<blockquote><p>Building your first Data Warehouse is a long, and often difficult process. How can you get your boss to approve a Data Warehouse project? What’s the best way to explain dimensional modeling and the benefits of a Data Warehouse to a business person? What are the best/most cost effective ways of learning it? What kind of materials, hardware, software, etc do you need? What’s the best way to build a proof of concept that will impress your boss, as rapidly as possible?</p>
<p>&nbsp;</p>
<p>Come to this presentation, and I’ll answer all of these questions plus the most important question of all.</p>
<p>“Is this good for the company?” Absolutely.</p></blockquote>
<p>This presentation went over very well at SQL Saturday Chicago, so come laugh and learn with me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/05/come-see-me-present-wednesday-at-the-fort-worth-ssug/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using a Date Table to Track Custom Fiscal Years/Periods</title>
		<link>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/</link>
		<comments>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 18:25:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Data Warehouse]]></category>
		<category><![CDATA[How To]]></category>
		<category><![CDATA[Reporting]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5918</guid>
		<description><![CDATA[
			
				
			
		
<p>Calendar Tables are only useful if they reflect how your company evaluates its data. Therefore, any company using non-standard financial fiscal years and periods will require customization with their Calendar or Date Table. In this post, I&#8217;ll provide an example of how to do so using the M2M ERP System Database. For those who don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fusing-a-date-table-to-track-custom-fiscal-yearsperiods%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fusing-a-date-table-to-track-custom-fiscal-yearsperiods%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Calendar Tables are only useful if they reflect how your company evaluates its data. Therefore, any company using non-standard financial fiscal years and periods will require customization with their <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">Calendar or Date Table</a>. In this post, I&#8217;ll provide an example of how to do so using the M2M ERP System Database. For those who don&#8217;t use M2M, the notes should serve as an example of how to do so with other systems. </p>
<p>The most effective way to determine the fiscal setup of a company is to determine how its major systems track the data. In M2M the source is the GL Rules and Periods (GLRule) table. There are four primary fields of interest, which I&#8217;ve aliased, and they are listed in the following query and screenshot.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> GLR.<span style="color: #202020;">fcname</span> AcctYearName
      ,GLR.<span style="color: #202020;">fnnumber</span> PeriodNo
      ,GLR.<span style="color: #202020;">fdstart</span> FirstDayOfPeriod
      ,GLR.<span style="color: #202020;">fdend</span> LastDayOfPeriod
  <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">glrule</span> GLR</pre></div></div>

<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/04/acct_year.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/04/acct_year.png" alt="" title="acct_year" width="422" height="290" class="aligncenter size-full wp-image-5921" /></a></p>
<p>As you can see, there is one record for each fiscal period in GLRule.  FCName is a Char(20) field and typically contains values like &#8220;FY 2005&#8243; or &#8220;Fiscal Year 2005&#8243;. FNNumber contains the period number and is an Int datatype. FDStart and FDEnd are Datetime fields and represent the first and last days of each period. </p>
<p>What follows are the update statements which I use to customize the fiscal periods of my calendar table. For your convenience, you can access the entire script here****. Copy and paste the entire query, replace the database and table name, and run it. I&#8217;ve dissected each step of the script below so those not using M2M can understand my logic and mimic those steps with their own systems. </p>
<p>Six fields are updated by the first statement. Notice that I&#8217;ve joined the Calendar Table to GLRule with a between statement. I&#8217;m using the pattern matching of Patindex to find the four digit number for Fiscal Year. However, if the user entered something like &#8220;Two Thousand Five&#8221; as a Fiscal Year, this method will not work. Start and end of period fields are taken directly out of GLRule as well. FiscalDayOfPeriod is calculated in this step as well for good measure.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Use</span> M2MDataXX
GO
<span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalYearName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalYearPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fnnumber</span> <span style="color: #808080;">/</span> <span style="color: #000;">100.00</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fnnumber</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalStartOfPeriodDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fdstart</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalEndOfPeriodDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> GLR.<span style="color: #202020;">fdend</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalDayOfPeriod<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span> <span style="color: #808080;">&#40;</span>dd, GLR.<span style="color: #202020;">fdstart</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">FROM</span>  DateTestStnd.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
        glrule GLR
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">BETWEEN</span> GLR.<span style="color: #202020;">fdstart</span> <span style="color: #808080;">AND</span> GLR.<span style="color: #202020;">fdend</span></pre></div></div>

<p>In this step, I&#8217;ve calculated the beginning and end of Fiscal Years. I group the record set in GLRule by Fiscal Year and then use min/max to determine the first and last day in each group.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
        <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fcName</span>, <span style="color: #FF00FF;">patindex</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[1,2][9,0,1,2][0-9][0-9]%'</span>, GLR.<span style="color: #202020;">fcName</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>
                 FiscalYearName
               ,<span style="color: #FF00FF;">min</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fdstart</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>
               ,<span style="color: #FF00FF;">max</span> <span style="color: #808080;">&#40;</span>GLR.<span style="color: #202020;">fdend</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#91;</span>FiscalEndOfYearDate<span style="color: #808080;">&#93;</span>
           <span style="color: #0000FF;">FROM</span> glrule GLR
         <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> GLR.<span style="color: #202020;">fcname</span><span style="color: #808080;">&#41;</span> M2MYEARS
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #202020;">FiscalYearName</span> <span style="color: #808080;">=</span> M2MYEARS.<span style="color: #202020;">FiscalYearName</span></pre></div></div>

<p>In the next step, I calculate the FiscalDayofYear and FiscalYearDayOfYear field. While I could have included this code in the previous step, it&#8217;s much easier to read and understand as a separate step. The FiscalDayOfYear calculation is simple and is simply the number of days between the current record and the first day Fiscal Year calculated previously. The FiscalYearDayOfYear is the same calculation, but I convert the integer (day) to a decimal and add it to the Fiscal Year integer. This makes drill down and grouping easier.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #808080;">&#91;</span>FiscalDayOfYear<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEDIFF</span> <span style="color: #808080;">&#40;</span>dd, <span style="color: #808080;">&#91;</span>FiscalStartOfYearDate<span style="color: #808080;">&#93;</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
      ,TST.<span style="color: #808080;">&#91;</span>FiscalYearDayOfYear<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">datepart</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">year</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>
            <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">datediff</span> <span style="color: #808080;">&#40;</span>dd, <span style="color: #FF00FF;">dateadd</span> <span style="color: #808080;">&#40;</span>yy, <span style="color: #FF00FF;">datediff</span> <span style="color: #808080;">&#40;</span>yy, <span style="color: #000;">0</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>, TST.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATE</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
            <span style="color: #808080;">/</span> <span style="color: #000;">1000.00</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">NUMERIC</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
   <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST</pre></div></div>

<p>The next section looks complex because I have to determine whether the number of Fiscal Periods can be broken into Quarters and Semesters. If a company used 10 Fiscal Periods per year, the concept of Quarters doesn&#8217;t make sense, but Semesters do. To make the code easier to understand, I&#8217;ve numbered the sections. </p>
<p>Section 1 checks whether the number of periods per fiscal year can be divided into quarters and semesters and the number of periods in each. Section 2 includes the period start and end dates so that the values of quarter and semester can be related back to the date table. Values for quarter or semester will be set to 0 if they aren&#8217;t valid in a particular M2M install.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> <span style="color: #808080;">&#91;</span>FiscalQuarter<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span> M2MYEARS.<span style="color: #202020;">Quarterific</span>
         <span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">ceiling</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span> <span style="color: #808080;">&#40;</span>M2MYEARS.<span style="color: #202020;">PeriodNo</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> M2MYEARS.<span style="color: #202020;">QuarterSize</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span>
        <span style="color: #0000FF;">END</span>
      ,<span style="color: #808080;">&#91;</span>FiscalSemester<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span> M2MYEARS.<span style="color: #202020;">Semesterific</span>
         <span style="color: #0000FF;">WHEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">ceiling</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span> <span style="color: #808080;">&#40;</span>M2MYEARS.<span style="color: #202020;">PeriodNo</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">DECIMAL</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> M2MYEARS.<span style="color: #202020;">SemesterSize</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
         <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span>
        <span style="color: #0000FF;">END</span> 
  <span style="color: #0000FF;">FROM</span>  DateTestStnd.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
 <span style="color: #008080;">-------------- 2. Include Start and End of Period Values. </span>
        <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> fcname FYName
               ,fdstart PeriodStart
               ,fdend PeriodEnd
               ,fnnumber PeriodNo
               ,GLRAGG.<span style="color: #202020;">QuarterSize</span>
               ,GLRAGG.<span style="color: #202020;">Quarterific</span>
               ,GLRAGG.<span style="color: #202020;">SemesterSize</span>
               ,GLRAGG.<span style="color: #202020;">Semesterific</span>
           <span style="color: #0000FF;">FROM</span>  dbo.<span style="color: #202020;">glrule</span> GLR
                <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
  <span style="color: #008080;">-------------- 1. Calculate whether Quarters and Semesters are Valid (and their values)</span>
                 <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> fcname AGGFYName
                        ,<span style="color: #FF00FF;">MAX</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> PeriodCount
                        , <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> QuarterSize
                        ,<span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">4</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'No'</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> Quarterific
                        , <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> SemesterSize
                        ,<span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">Max</span> <span style="color: #808080;">&#40;</span>fnnumber<span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">2</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Yes'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'No'</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> Semesterific
                    <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">glrule</span>
                  <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> fcname<span style="color: #808080;">&#41;</span> GLRAGG
                <span style="color: #0000FF;">ON</span> GLR.<span style="color: #202020;">FCNAME</span> <span style="color: #808080;">=</span> GLRAGG.<span style="color: #202020;">AGGFYNAME</span><span style="color: #808080;">&#41;</span> M2MYEARS
 <span style="color: #008080;">-------------- End of 1</span>
       <span style="color: #0000FF;">ON</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">&gt;=</span> M2MYEARS.<span style="color: #202020;">PeriodStart</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">DATE</span> <span style="color: #808080;">&lt;=</span> M2MYEARS.<span style="color: #202020;">PeriodEnd</span>
  <span style="color: #008080;">-------------- End of 2</span></pre></div></div>

<p>Workday calculation is also somewhat difficult to understand. In M2M this information is kept in the Production Calendar (SCCALN) table. The fields we care about are: </p>
<ul>
<li>fcYrMon &#8211; Year and month in the format of YYYY/MM (2011/02). Unfortunately these are not per Fiscal Year or Period, they are by Calendar Year and Month. </li>
<li>fcShifts &#8211; A Varchar (31) field with the number of Shifts per day. </li>
</ul>
<p><a href="http://www.made2mentor.com/wp-content/uploads/2011/04/Workdays.png"><img src="http://www.made2mentor.com/wp-content/uploads/2011/04/Workdays.png" alt="" title="Workdays" width="254" height="316" class="aligncenter size-full wp-image-5942" /></a><br />
Needless to say, we need an easier way than parsing through this table every time we need to calculate work days. The join is a little strange because the Date Table CalendarYear and CalendarMonth fields are SmallInt and need to be cast as Varchar. I&#8217;ve used a left join because many M2M companies don&#8217;t use the Production Calendar so SCCALN may lack records for fiscal periods. </p>
<p>Basically the code checks for a corresponding record in SCCALN. If one does not exist, then the typical Monday through Friday work week is used by default. If it does exist the fcShifts field is parsed, and if there are one or more shifts for a particular day, then the date is designated a work day.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> WorkDay <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span>
         <span style="color: #0000FF;">WHEN</span> wc.<span style="color: #202020;">fcShifts</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span>
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> TST.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'No Work'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'Work Day'</span> <span style="color: #0000FF;">END</span>
         <span style="color: #0000FF;">ELSE</span>
           <span style="color: #0000FF;">CASE</span>
            <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>WC.<span style="color: #202020;">fcShifts</span>, TST.<span style="color: #202020;">CalendarDayOfMonth</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'No Work'</span>
            <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">'Work Day'</span>
           <span style="color: #0000FF;">END</span>
        <span style="color: #0000FF;">END</span>
      ,IsWorkDay <span style="color: #808080;">=</span>
        <span style="color: #0000FF;">CASE</span>
         <span style="color: #0000FF;">WHEN</span> WC.<span style="color: #202020;">fcShifts</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> 
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> TST.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">END</span>
         <span style="color: #0000FF;">ELSE</span> 
           <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">substring</span> <span style="color: #808080;">&#40;</span>WC.<span style="color: #202020;">fcShifts</span>, TST.<span style="color: #202020;">CalendarDayOfMonth</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">1</span> 
           <span style="color: #0000FF;">END</span>
        <span style="color: #0000FF;">END</span>
  <span style="color: #0000FF;">FROM</span>  DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
       <span style="color: #0000FF;">LEFT</span> <span style="color: #808080;">JOIN</span>
        dbo.<span style="color: #202020;">ScCaln</span> WC
       <span style="color: #0000FF;">ON</span> WC.<span style="color: #202020;">fcYrMon</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">CalendarYear</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
           <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span>
           <span style="color: #808080;">+</span> <span style="color: #0000FF;">right</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">CalendarMonth</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span></pre></div></div>

<p>The final bit of code updates the FiscalWeekName and FiscalYearWeekName. This code is <strong>tailored to my current employer, and I consider it optional</strong> because it may not pertain to you. The logic is as follows: </p>
<ol>
<li>The first fiscal week starts on the first day of the fiscal period.</li>
<li>Fiscal weeks end on Friday and begin on Saturday.</li>
<li>A &#8220;partial week&#8221; where the week starts on a day other than Saturday is added to the next full week. Partial weeks ending a period are considered their own week.</li>
<li>Week names follow the pattern of [FiscalPeriod].[WeekNumber] both of which are integers. For example, the second week of the fifth period would be designated as &#8220;5.2&#8243;. </li>
</ol>
<p>Again, this is a specific requirement and it may not apply to most companies.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> FiscalWeekName <span style="color: #808080;">=</span>
        TST.<span style="color: #202020;">FiscalPeriod</span>
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> <span style="color: #808080;">&#40;</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">count</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
                    <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TMP
                   <span style="color: #0000FF;">WHERE</span> TMP.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span>
                         <span style="color: #808080;">AND</span> TMP.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">BETWEEN</span> TST.<span style="color: #202020;">FiscalStartOfPeriodDate</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">Date</span><span style="color: #808080;">&#41;</span>
                <span style="color: #808080;">/</span> <span style="color: #000;">10.0</span><span style="color: #808080;">&#41;</span>
           <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span>
            .1
           <span style="color: #0000FF;">ELSE</span>
            <span style="color: #808080;">&#40;</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">count</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span>
                 <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TMP
                <span style="color: #0000FF;">WHERE</span> TMP.<span style="color: #202020;">CalendarDayOfWeek</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span>
                      <span style="color: #808080;">AND</span> TMP.<span style="color: #0000FF;">Date</span> <span style="color: #808080;">BETWEEN</span> TST.<span style="color: #202020;">FiscalStartOfPeriodDate</span> <span style="color: #808080;">AND</span> TST.<span style="color: #0000FF;">Date</span><span style="color: #808080;">&#41;</span>
             <span style="color: #808080;">/</span> <span style="color: #000;">10.0</span><span style="color: #808080;">&#41;</span>
          <span style="color: #0000FF;">END</span>
  <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST
<span style="color: #008080;">-- Updates FiscalYearWeekName from FiscalWeekName</span>
<span style="color: #0000FF;">UPDATE</span> TST
   <span style="color: #0000FF;">SET</span> TST.<span style="color: #202020;">FiscalYearWeekName</span> <span style="color: #808080;">=</span>
          <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalYearName</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span>
        <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CASE</span> <span style="color: #FF00FF;">len</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalWeekName</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'0'</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">END</span><span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">cast</span> <span style="color: #808080;">&#40;</span>TST.<span style="color: #202020;">FiscalWeekName</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">CHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">FROM</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> TST</pre></div></div>

<p>In most systems, new fiscal years and periods are added once a year. The update script will need to be run after that happens. With Made2Manage, and presumably other systems, triggers could be used to detect those changes and then run the update script. In my own situation, I&#8217;ve chosen to save this update script as a scheduled job which runs weekly. This may seem wasteful, but the update script completes very quickly. </p>
<p>Technically, I could have integrated this script with the <a href="http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/">one I posted previously</a> which would have been faster and more efficient. However, I use two scripts for several reasons. First, this method is more flexible as I can create any Fiscal Update script I want without editing the main calendar script. Second, the Fiscal Update script will need to be run periodically to get updates and dropping/recreating the entire date table would be wasteful. Finally, most systems do not track Fiscal Years and Periods a decade or more into the future. Therefore, the first script is required to set default values for the Fiscal fields and only those that exist in the source system are updated. The user will not receive an error on a report because a future fiscal date value was not supplied.  </p>
<p>Well, there you have it. The last two articles have described how I create date tables and customize the Fiscal fields from a source system, in this case Made2Manage. </p>
<p>Any questions or suggestions to make the process better? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Calendar Tables and Why You Need One.</title>
		<link>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/</link>
		<comments>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 15:00:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Made2Manage]]></category>
		<category><![CDATA[Report Customization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Data Warehouse]]></category>

		<guid isPermaLink="false">http://www.made2mentor.com/?p=5870</guid>
		<description><![CDATA[
			
				
			
		
What is a Date (Calendar) Table?
<p>For the purposes of this article a Date Table is a dedicated table containing a single record for each day in a defined range. They include fields with descriptive attributes for each day such as Year, Month, Week, and whether a particular day is a work day or not. Date [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fcalendar-tables-why-you-need-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.made2mentor.com%2F2011%2F04%2Fcalendar-tables-why-you-need-one%2F&amp;source=Made2Mentor&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<h3>What is a Date (Calendar) Table?</h3>
<p>For the purposes of this article a Date Table is a dedicated table containing a single record for each day in a defined range. They include fields with descriptive attributes for each day such as Year, Month, Week, and whether a particular day is a work day or not. Date Tables (Dimensions) are an integral part of every Data Warehouse, but they can be used with a regular (OLTP) database as well. </p>
<h3>Why do I need a Date Table?</h3>
<p>There are several situations in which a date table is extremely helpful. Let&#8217;s consider just a few. Suppose you need to&#8230;. </p>
<ul>
<li>Generate a running sales total per day and include days in which you have no sales such as weekends. </li>
<li>Calculate the number of workdays between two dates. </li>
<li>Calculate projected ship dates for products based upon lead times. </li>
<li>Aggregate data by non-standard Fiscal Years or Periods. Perhaps your company uses the Fiscal Year of the US Government which runs from October 1st until September 30th. My current employer uses <a href="http://en.wikipedia.org/wiki/4-4-5_Calendar">the 4-4-5 Calendar</a> methodology, which is what originally led me to create this Date Table script. </li>
<li>Need to track data by seasons, whatever that means to your company. For example, if you manufactured bicycles you&#8217;d certainly want to compare sales figures in the spring versus fall.</li>
<li>Need to compare data using rolling months/periods. Suppose your company wants to know how March sales for this year compared to March for the past 5 years. Or you want to compare sales which occurred on Fridays to each other to look for trends. </li>
<li>Need to create <a href="http://en.wikipedia.org/wiki/Gantt_chart">Gantt Charts</a>.</li>
</ul>
<p>While many of these issues can be handled by writing T-SQL code, they&#8217;re much easier with a dedicated date table. Also, the Date Table can be customized to track anything about a particular day you want. </p>
<h3>What about Space Considerations?</h3>
<p>One concern that people have when creating a Date Table is the &#8220;wasted&#8221; space since each day is its own record. The space used is negligible, especially when compared to the benefits of having a dedicated date table. For example, when I execute my script for the years 2000 through 20035 the space used is less than 250MB. </p>
<h3>How do I create a Date Table?</h3>
<p><strong>Kimball Date Dimensions</strong><br />
In the <a href="http://www.amazon.com/gp/product/0470640383?ie=UTF8&#038;tag=made2-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=0470640383">The Microsoft Data Warehouse Toolkit: With SQL Server 2008 R2</a><img src="http://www.assoc-amazon.com/e/ir?t=made2-20&#038;l=as2&#038;o=1&#038;a=0470640383" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />and the previous 2005 version, the Kimball Group recommends that you create a Date Dimension using Excel. In the latest book, they provide a Date Dimension Spreadsheet which is <a href="http://www.kimballgroup.com/html/booksMDWTtools.html">available for Download</a> and utilizes VBA to generate the table. They recommend you create a SSIS package to load that data into SQL Server. </p>
<p>While this will work, and some non-technical people might prefer this method, I prefer to use SQL Script, because I may not always have access to SSIS.</p>
<p><strong>Using a SQL Script</strong><br />
When I searched for a Date Table Script a few years ago, I couldn&#8217;t find something suitable for my needs. Most of them include only one set of fields (regular Calendar) and I to report via custom fiscal calendars. </p>
<p>I created this script from a function called <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519">F_TABLE_DATE by Michael Valentine Jones</a> from <a href="http://www.sqlteam.com/">SQLTeam</a> and have since gotten his permission to distribute it. I&#8217;ve modified the script so that it generates a Kimball style date dimension and it&#8217;s very comprehensive, yet simple to use and customize. </p>
<ol>
<li>Create a database called DateDatabase or whatever you&#8217;d like to call it. We&#8217;ll install F_TABLE_DATE in it and presumably the Date Table as well. </li>
<li><a href='http://www.made2mentor.com/wp-content/uploads/2011/04/Master-Date-Dimension-Script-Base-Ver-5.1-.txt'>Open the following script</a> and replace &#8220;DateDatabase&#8221; (Find and Replace) with whatever database name you chose in step 1. </li>
<li>Run the Script to create the function. </li>
</ol>
<p>Your database should now contain the function used to create Date Tables. As a test, run the following script:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">F_TABLE_DATE</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'20000101'</span>, <span style="color: #FF0000;">'20101231'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #000;">1</span></pre></div></div>

<p>You should receive 4019 Rows, 11 years worth of days plus a record for unknown dates. Notice the extensive list of fields built into the function. All of them are documented in the script. </p>
<p>Use a Select Into statement to create the Date Table like so. Replace dbo.DimDate with whatever you&#8217;d like to call your Date Table.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #808080;">EXISTS</span>
     <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span>
        <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span>
       <span style="color: #0000FF;">WHERE</span> TABLE_TYPE <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span> <span style="color: #808080;">AND</span> TABLE_NAME <span style="color: #808080;">=</span> <span style="color: #FF0000;">'DimDate'</span><span style="color: #808080;">&#41;</span>
  <span style="color: #0000FF;">BEGIN</span>
        <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">DimDate</span>;
  <span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">Select</span>
  DateId
 ,<span style="color: #0000FF;">Date</span>
 ,NextDayDate
 ,CalendarYear
 ,CalendarYearQuarter
 ,CalendarYearMonth
 ,CalendarYearDayOfYear
 ,CalendarQuarter
 ,CalendarMonth
 ,CalendarDayOfYear
 ,CalendarDayOfMonth
 ,CalendarDayOfWeek
 ,CalendarYearName
 ,CalendarYearQuarterName
 ,CalendarYearMonthName
 ,CalendarYearMonthNameLong
 ,CalendarQuarterName
 ,CalendarMonthName
 ,CalendarMonthNameLong
 ,WeekdayName
 ,WeekdayNameLong
 ,CalendarStartOfYearDate
 ,CalendarEndOfYearDate
 ,CalendarStartOfQuarterDate
 ,CalendarEndOfQuarterDate
 ,CalendarStartOfMonthDate
 ,CalendarEndOfMonthDate
 ,QuarterSeqNo
 ,MonthSeqNo
 ,FiscalYearName
 ,FiscalYearPeriod
 ,FiscalYearDayOfYear
 ,FiscalYearWeekName
 ,FiscalSemester
 ,FiscalQuarter
 ,FiscalPeriod
 ,FiscalDayOfYear
 ,FiscalDayOfPeriod
 ,FiscalWeekName
 ,FiscalStartOfYearDate
 ,FiscalEndOfYearDate
 ,FiscalStartOfPeriodDate
 ,FiscalEndOfPeriodDate
 ,ISODate
 ,ISOYearWeekNo
 ,ISOWeekNo
 ,ISODayOfWeek
 ,ISOYearWeekName
 ,ISOYearWeekDayOfWeekName
 ,DateFormatYYYYMMDD
 ,DateFormatYYYYMD
 ,DateFormatMMDDYEAR
 ,DateFormatMDYEAR
 ,DateFormatMMMDYYYY
 ,DateFormatMMMMMMMMMDYYYY
 ,DateFormatMMDDYY
 ,DateFormatMDYY
 ,WorkDay
 ,IsWorkDay
<span style="color: #0000FF;">into</span>
  dbo.<span style="color: #202020;">DimDate</span> <span style="color: #008080;">-- Choose whatever name you like for your table.</span>
<span style="color: #0000FF;">From</span>
  dbo.<span style="color: #202020;">F_TABLE_DATE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'20000101'</span>,<span style="color: #FF0000;">'20251231'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- Edit the parameters for a custom date range.</span>
  <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> <span style="color: #000;">1</span></pre></div></div>

<p>Don&#8217;t panic, you don&#8217;t need to use all of the fields which is why this script is so highly customizable. Any fields you don&#8217;t want can be commented out before running the Select Into statement, which allows you to create your own custom Date Table. </p>
<p>The final step is to add a Primary Key to the DateID and an index for the Date and NextDayDate fields. As I can&#8217;t predict the usage patterns for your date table, you&#8217;ll have to implement further indexing if necessary.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>DimDate<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">ADD</span>  <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>DateId<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">FILLFACTOR</span> <span style="color: #808080;">=</span> <span style="color: #000;">100</span>
 <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span>
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">INDEX</span> id_Dates
<span style="color: #0000FF;">ON</span> DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> 
	<span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Date</span>, 
	 NextDayDate<span style="color: #808080;">&#41;</span></pre></div></div>

<h3>How do I use the Date Table? </h3>
<p>Using the date table is simple, you simply link to it from the query from your source system. Use the following template.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
  MY.<span style="color: #0000FF;">date</span>
 ,DTE.<span style="color: #202020;">CalendarYear</span>
 ,DTE.<span style="color: #202020;">CalendarMonthName</span>
<span style="color: #0000FF;">FROM</span>
    MyTable MY
  <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>
    DateDatabase.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">DimDate</span> DTE
  <span style="color: #0000FF;">ON</span> MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&gt;=</span> DTE.<span style="color: #0000FF;">date</span> <span style="color: #808080;">AND</span>
     MY.<span style="color: #0000FF;">date</span> <span style="color: #808080;">&lt;</span> DTE.<span style="color: #202020;">NextDayDate</span></pre></div></div>

<p>Both DTE.Date and DTE.NextDayDate represent midnight of the two consecutive days. Therefore the time portion of a Datetime field can be ignored. </p>
<h3> What about the fiscal fields?</h3>
<p>After running these scripts, and examining the Date Table, you&#8217;ll notice that the fiscal fields mostly mirror the calendar fields. This is by design. <a href="http://www.made2mentor.com/2011/04/using-a-date-table-to-track-custom-fiscal-yearsperiods/">In the next article</a> I&#8217;ll explain how to customize the fiscal fields and a script to extract the fiscal information out of Made2Manage. Even if you don&#8217;t use M2M, you can still use the fiscal script as an example of how to do it. </p>
<p>Any questions? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.made2mentor.com/2011/04/calendar-tables-why-you-need-one/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

