<?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>codegumbo &#187; TSQL2sDay</title>
	<atom:link href="http://codegumbo.com/index.php/category/development/code/sql/tsql2sday/feed/" rel="self" type="application/rss+xml" />
	<link>http://codegumbo.com</link>
	<description>Laissez Les Bon Code Roulez!</description>
	<lastBuildDate>Fri, 03 Feb 2012 00:43:59 +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>#TSQL2sDay Roundup</title>
		<link>http://codegumbo.com/index.php/2011/10/10/tsql2sday-roundup/</link>
		<comments>http://codegumbo.com/index.php/2011/10/10/tsql2sday-roundup/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 14:00:28 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[Blogging is FUN!]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2011/10/10/tsql2sday-roundup/</guid>
		<description><![CDATA[So the launch was early, and the write-up is delayed.&#160; Time has no meaning….&#160; Truthfully, I’m sorry I didn’t manage to squeeze this summary post in a bit sooner; I have the typical DBA excuse: too much to do, and too little time. One nice thing about this topic is that it seemed to resonate [...]]]></description>
			<content:encoded><![CDATA[<p>So the launch was early, and the write-up is delayed.&#160; Time has no meaning….&#160; Truthfully, I’m sorry I didn’t manage to squeeze this summary post in a bit sooner; I have the typical DBA excuse: too much to do, and too little time. </p>
<p>One nice thing about this topic is that it seemed to resonate with several bloggers who had either strayed away from T-SQL Tuesday or had never participated; hopefully, the bug to write will stick (and I’m pointing the finger at myself for this one as well).</p>
<h3>Beginning at the Beginning:</h3>
<p><img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/3ad80e5cd80033c4b574b34cc8ffe419?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://blog.sqlauthority.com">Pinal Dave</a> in his gentle teaching style covers several different <a href="http://blog.sqlauthority.com/2011/10/04/sql-server-quick-note-about-join-common-questions-and-simple-answers/" target="_blank">questions and answers</a> about JOIN techniques. Great way to review the basics and get conversations started with beginners.    </p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/ab17b5e1b1cbc40740e52336482b9807?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://kenj.blogspot.com">KenJ</a> is a bit esoteric, but a great reminder of what resources there are to learn more <a href="http://kenj.blogspot.com/2011/10/join-me.html">about SQL Server.</a></p>
<h3>&#160;</h3>
<h3>JOINs In The Real World:</h3>
<h2></h2>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/2007b7c6d81b6ed86710ece8e2d565e1?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://www.real-sql-guy.com">Tracy McKibben</a> demonstrated a very simple tuning tip based on real production experience: <a href="http://www.real-sql-guy.com/2011/09/t-sql-tuesday-23-nested-joins.html." target="_blank">the Nested JOIN</a></p>
<p>&#160;</p>
<p><a href="http://richbrownesq-sqlserver.blogspot.com">Rich Brown</a> reminds us that good query construction often involves understanding how the optimizer handles <a href="http://richbrownesq-sqlserver.blogspot.com/2011/10/t-sql-helping-optimiser-can-hinder.html">JOINs under the covers.</a></p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/da3d144d9efe5d6b8e93aaaa2cc703a0?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://nebrasql.blogspot.com/">Andy Galbraith</a> (Yet Another SQL Andy!) reminds us to <a href="http://nebrasql.blogspot.com/2011/10/t-sql-tuesday-23-respect-your-joins.html">Respect Your JOINs</a>. It’s funny how often we assume that database queries are logically well-written when we start looking for performance problems.</p>
<p>&#160;</p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/e86f57ad09692dd6d39671898de98256?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://www.bobpusateri.com">Bob Pusateri</a> points out that <a href="http://www.bobpusateri.com/archive/2011/10/t-sql-tuesday-23-where-the-join-ends/">WHERE clauses influence JOIN behaviors</a>. You have to look at the entire SQL statement to make sure you’re getting what you’re supposed to be getting.    </p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/edceb5c6895ba54a06f1486dadeb7433?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://nelsonsweb.net">Matt Nelson</a> reminds us that <a href="http://nelsonsweb.net/2011/10/t-sql-tuesday-23-joins/">unnecessary JOINs are bad</a>, and sometimes you need to dig a little deeper when performance tuning.    </p>
<p>&#160;</p>
<h3>A Little Deeper:</h3>
<p><img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/94ee7f24a49e19b7779edcfe47a68a9e?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://sqlblog.com/blogs/rob_farley">Rob Farley</a> toasted my brain a little on this post about <a href="http://sqlblog.com/blogs/rob_farley/archive/2011/10/04/joins-without-join.aspx">Joins without JOINs</a>.&#160; It’s a well-written explanation of what an Ant-Semi JOIN is. </p>
<p>&#160;</p>
<p><a href="http://bradsruminations.blogspot.com">Brad Schulz</a> finished the cooking job with a <a href="http://bradsruminations.blogspot.com/2011/10/t-sql-tuesday-023-flip-side-of-join.html">take on Semi-JOINs</a>, among other things.&#160; Is Brad really Rob in reverse? </p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/a9569e09c9398884cd72db41a325154e?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /><a href="http://www.sqlserverblogforum.com/">Muthukkumaran Kaliyamoorthy</a> covered the internal join mechanisms, and why the optimizer chooses <a href="http://www.sqlserverblogforum.com/2011/10/merge-join-vs-hash-join-vs-nested-loop-join/">merge, hash, or nested loop joins</a>.    </p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/0edf4d76f04d52cdbb0532ce6f8298f2?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://www.sqlmashup.com">Robert Matthew Cook</a> has a couple of <a href="http://www.sqlmashup.com/t-sql-tuesday-23-joins-tsql2sday">great metaphors</a> for explaining Merge, Hash, and Nested Loop JOINs.</p>
<p>&#160;</p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/bda2a5c5ba5d6ae5b1d3be3be6271224?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://blog.waynesheffield.com/wayne/">Wayne Sheffield</a> pointed out some <a href="http://blog.waynesheffield.com/wayne/archive/2011/10/t-sql-tuesday-23-joins/">interesting syntactical ways of writing</a> JOINs. I’m not sure if I’m comfortable with some of them, but they may be of some use in certain edge scenarios. Or, if you just want to mess with the guy who reviews your code.</p>
<p><img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/7a280962d7c684d86fa58a1c1c289bf5?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://SQL.RichardDouglas.co.uk">Richard Douglas</a> offers up a short-but-sweet explanation of the relationship of <a href="http://sql.richarddouglas.co.uk/archive/2011/10/t-sql-tuesday-23-key-lookups.html">Key Lookups to JOINs</a>. </p>
<p>     <!--EndFragment--><br />
<h3>&#160;</h3>
<h3>The Future is a Ticking Time Bomb…</h3>
<p><img style="display: inline; float: left" alt="" align="left" src="http://0.gravatar.com/avatar/68f3945bf478970197d593c700ed353f?s=48&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" /> <a href="http://thelonedba.wordpress.com">Thomas Rushton</a> points out that Denali may finally force us to <a href="http://thelonedba.wordpress.com/2011/10/04/t-sql-tuesday-23-non-ansi-joins-discontinued-in-denali/">clean up our code.</a></p>
<p>&#160;</p>
<p>&#160;</p>
<h3>My Portugese is Not Up To Par…</h3>
<p>&#160;<img style="display: inline; float: left" alt="" align="left" src="http://1.gravatar.com/avatar/1dea6ab124515201bfb24fd76d4e5228?s=48&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D48&amp;r=G" width="48" height="48" />     <br />Finally, <a href="http://leka.com.br">Ricardo Leka</a> wrote a post that I had to use Google Translate to interpret.&#160; I think I lost something in&#160; translation, because I’m not sure how the example relates to JOIN’s, but thought I would include it anyway.&#160; <a href="http://leka.com.br/2011/10/04/t-sql-tuesday-23-joins/">http://leka.com.br/2011/10/04/t-sql-tuesday-23-joins/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/10/10/tsql2sday-roundup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>#TSQL2sDay T-SQL Tuesday 23&#8211;Early edition</title>
		<link>http://codegumbo.com/index.php/2011/09/27/tsql2sday-t-sql-tuesday-23early-edition/</link>
		<comments>http://codegumbo.com/index.php/2011/09/27/tsql2sday-t-sql-tuesday-23early-edition/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 16:59:13 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[Blogging is FUN!]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[The Social Web]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2011/09/27/tsql2sday-t-sql-tuesday-23early-edition/</guid>
		<description><![CDATA[&#160; Time once again for another edition of T-SQL Tuesday!&#160; What’s that, you say?&#160; Early?&#160; Why yes, indeed.&#160; Due to the potential schedule of conflict of PASS’s Summit 2011 occurring on the second Tuesday of October (the normal date for T-SQL Tuesday), I’ve gotten special permission to bump it up a week.&#160; To participate in [...]]]></description>
			<content:encoded><![CDATA[<p><b><img style="display: inline; float: left" alt="T-SQL Tuesday Logo" align="left" src="http://www.pearlknows.com/sitebuildercontent/sitebuilderpictures/T-SQLLogo.JPG" width="145" height="150" /></b></p>
<p>&#160;</p>
<p>Time once again for another edition of T-SQL Tuesday!&#160; What’s that, you say?&#160; Early?&#160; Why yes, indeed.&#160; Due to the potential schedule of conflict of PASS’s Summit 2011 occurring on the second Tuesday of October (the normal date for T-SQL Tuesday), I’ve gotten special permission to bump it up a week.&#160; </p>
<p><strong>To participate in this month’s T-SQL Tuesday, your post must go live between midnight UTC on Tuesday, October 4, 2011 and midnight UTC on Wednesday, October 5.</strong></p>
<p>&#160;</p>
<p>Your post needs to link back to this blog, and if you use the image, anchor it to this post.&#160; Make sure you leave a comment or a trackback to this post (so I can find it later, when I write up the summary).</p>
<p>Topic d’jour?&#160; <strong>JOINS</strong> (I’m in a fundamentals mood lately).&#160; Note that I also like creative and esoteric posts, so if you can find a way to apply SQL as a metaphorical language for community activity, I’ll read it and enjoy it.&#160; If you just want to tell me in a simple fashion the difference between a HASH and MERGE join, I’m cool with that, too.</p>
<p>Do me a favor, though, and please spread the word about the early date.&#160; Looking forward to reading your submissions.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/09/27/tsql2sday-t-sql-tuesday-23early-edition/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>#TSQL2sday: Emulating a FIRST aggregation</title>
		<link>http://codegumbo.com/index.php/2011/03/08/tsql2sday-emulating-a-first-aggregation/</link>
		<comments>http://codegumbo.com/index.php/2011/03/08/tsql2sday-emulating-a-first-aggregation/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 21:58:58 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2011/03/08/tsql2sday-emulating-a-first-aggregation/</guid>
		<description><![CDATA[Jes Borland is hosting this month’s T-SQL Tuesday, and it’s all about aggregations.&#160; Here’s an old coding trick of mine to emulate a FIRST aggregation in T-SQL.&#160; Say we have a table that has three columns: ID, a uniqueidentifier Name, a varchar that represents something, and DateStored, a datetime that is set when the row [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/come-one-come-all-to"></a>
<p><a href="http://http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/come-one-come-all-to" target="_blank"><img style="border-right-width: 0px; margin: 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="tsql2sday" border="0" alt="tsql2sday" src="http://codegumbo.com/images/TSQL2sdayEmulatingaFIRSTaggregation_EEC4/tsql2sday.jpg" width="154" height="154" /></a></p>
<p><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/come-one-come-all-to" target="_blank">Jes Borland</a> is hosting this month’s T-SQL Tuesday, and it’s all about aggregations.&#160; Here’s an old coding trick of mine to emulate a FIRST aggregation in T-SQL.&#160; Say we have a table that has three columns:</p>
<ul>
<li>ID, a uniqueidentifier </li>
<li>Name, a varchar that represents something, and </li>
<li>DateStored, a datetime that is set when the row is written to the table </li>
</ul>
<p>And we populate that table like so:</p>
<p> <code style="font-size: 12px"><span style="color: blue">CREATE TABLE </span><span style="color: black">TSQL2sDay_FirstAgg      <br />&#160;&#160;&#160; </span><span style="color: gray">(      <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: black">ID </span><span style="color: blue">UNIQUEIDENTIFIER      <br />&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">NAME </span><span style="color: blue">VARCHAR</span><span style="color: gray">(</span><span style="color: black">20</span><span style="color: gray">)      <br />&#160;&#160;&#160; , </span><span style="color: black">DateStored </span><span style="color: blue">DATETIME DEFAULT </span><span style="color: magenta">GETUTCDATE</span><span style="color: gray">()      <br />&#160;&#160;&#160; )       <br />&#160;&#160;&#160;&#160; <br /></span><span style="color: blue">INSERT&#160; INTO </span><span style="color: black">TSQL2sDay_FirstAgg      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">( </span><span style="color: black">ID</span><span style="color: gray">, </span><span style="color: black">NAME </span><span style="color: gray">)      <br /></span><span style="color: blue">VALUES&#160; </span><span style="color: gray">( </span><span style="color: magenta">NEWID</span><span style="color: gray">(), </span><span style="color: red">'Peanut' </span><span style="color: gray">)      </p>
<p></span><span style="color: blue">WAITFOR </span><span style="color: black">DELAY </span><span style="color: red">'00:00:01'      </p>
<p></span><span style="color: blue">INSERT&#160; INTO </span><span style="color: black">TSQL2sDay_FirstAgg      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">( </span><span style="color: black">ID</span><span style="color: gray">, </span><span style="color: black">NAME </span><span style="color: gray">)      <br /></span><span style="color: blue">VALUES&#160; </span><span style="color: gray">( </span><span style="color: magenta">NEWID</span><span style="color: gray">(), </span><span style="color: red">'Peanut' </span><span style="color: gray">)      </p>
<p></span><span style="color: blue">WAITFOR </span><span style="color: black">DELAY </span><span style="color: red">'00:00:01'      </p>
<p></span><span style="color: blue">INSERT&#160; INTO </span><span style="color: black">TSQL2sDay_FirstAgg      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">( </span><span style="color: black">ID</span><span style="color: gray">, </span><span style="color: black">NAME </span><span style="color: gray">)      <br /></span><span style="color: blue">VALUES&#160; </span><span style="color: gray">( </span><span style="color: magenta">NEWID</span><span style="color: gray">(), </span><span style="color: red">'Orange' </span><span style="color: gray">)      <br /></span></code>
<p>&#160;</p>
<p>It’s easy to figure out the number of rows associated with each name:</p>
<p> <code style="font-size: 12px"><span style="color: green">-- SELECT data to verify order of DateStored      <br /></span><span style="color: blue">SELECT&#160; </span><span style="color: black">ID      <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">NAME      <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">DateStored      <br /></span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: black">TSQL2sDay_FirstAgg&#160;&#160;&#160;&#160;&#160;&#160; </p>
<p></span><span style="color: green">-- Basic Row Count by Name      <br /></span><span style="color: blue">SELECT&#160; </span><span style="color: black">NAME      <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">RowCnt </span><span style="color: blue">= </span><span style="color: magenta">COUNT</span><span style="color: gray">(*)      <br /></span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: black">TSQL2sDay_FirstAgg      <br /></span><span style="color: blue">GROUP BY </span><span style="color: black">NAME      <br /></span></code>
<p>&#160;</p>
<p>but how do we figure out what the first ID was for each name along with the number of rows?&#160; You could work something out using the HAVING clause of the SELECT statement, or you could do something like the following:</p>
<p> <code style="font-size: 12px"><span style="color: green">--SELECT first ID and count of rows by Name     <br /></span><span style="color: blue">SELECT&#160; </span><span style="color: black">FirstID </span><span style="color: blue">= </span><span style="color: magenta">CONVERT</span><span style="color: gray">(</span><span style="color: blue">UNIQUEIDENTIFIER</span><span style="color: gray">, </span><span style="color: magenta">RIGHT</span><span style="color: gray">(</span><span style="color: magenta">MIN</span><span style="color: gray">(</span><span style="color: magenta">CONVERT</span><span style="color: gray">(</span><span style="color: blue">VARCHAR</span><span style="color: gray">(</span><span style="color: black">24</span><span style="color: gray">), </span><span style="color: black">DateStored</span><span style="color: gray">, </span><span style="color: black">121</span><span style="color: gray">) + </span><span style="color: magenta">CONVERT</span><span style="color: gray">(</span><span style="color: blue">VARCHAR</span><span style="color: gray">(</span><span style="color: black">36</span><span style="color: gray">), </span><span style="color: black">ID</span><span style="color: gray">)),     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: black">36</span><span style="color: gray">))     <br />&#160;&#160;&#160;&#160;&#160; , </span><span style="color: black">NAME     <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">, </span><span style="color: black">RowCnt </span><span style="color: blue">= </span><span style="color: magenta">COUNT</span><span style="color: gray">(*)     <br /></span><span style="color: blue">FROM&#160;&#160;&#160; </span><span style="color: black">TSQL2sDay_FirstAgg     <br /></span><span style="color: blue">GROUP BY </span><span style="color: black">NAME     </p>
<p></span></code>
<p>&#160;</p>
<p>It looks complicated, but it’s not; let’s step through it.</p>
<ol>
<li>We have to know some basic information about our data; in this case, we know that the datetime value associated with each row with a common name is different.&#160; In other words, there are no two Peanuts with the same DateStored value.&#160; This is important, because in order for there to be a first value, there must be some method of ALWAYS determining which one WAS first.&#160; If two Peanuts showed up at the same time, the model is broken.</li>
<li>The first thing we do is to <a href="http://msdn.microsoft.com/en-us/library/ms187928.aspx" target="_blank">CONVERT</a> the DateStored value to a varchar; this allows us to concatenate it with other values.&#160; The format of that varchar string is important; it must be precise, and it must sort in an ascending order.&#160; The ODBC canonical format (with milliseconds) is a good candidate for this.</li>
<li>We then CONVERT the uniqueidentifer to a varchar, and append it to the DateStored varchar value.&#160; This gives us a lengthy string which can be sorted by the first 24 characters.</li>
<li>We find the MIN of the string we constructed; this MIN value is determined by the optimizer based on the sorting value of the numbers in the DateStored value.</li>
<li>We then take the RIGHT-most 36 characters (the length of a uniqueidentifier), and convert it back to a uniqueidentifier (so that we have our type back).</li>
</ol>
<p>There are probably better solutions for this, but this is a simple trick that works under certain circumstances and is portable to several flavors of SQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/03/08/tsql2sday-emulating-a-first-aggregation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#TSQL2sDay: Resolutions</title>
		<link>http://codegumbo.com/index.php/2011/01/11/tsql2sday-resolutions/</link>
		<comments>http://codegumbo.com/index.php/2011/01/11/tsql2sday-resolutions/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 19:11:14 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[The Social Web]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2011/01/11/tsql2sday-resolutions/</guid>
		<description><![CDATA[For this month’s T-SQL Tuesday, Jen McCown asks: So tell us: what techie resolutions have you been pondering, and why?&#160; Are you heading for a certification? An award? Are you looking to pick up CLR because that guy at the Summit said it’s “bitchin’”? Go crazy… I’ve already covered a lot of my techie resolutions [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.midnightdba.com/Jen/2011/01/tsql-tuesday-014/" target="_blank"><img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 2px 0px; display: inline; border-top: 0px; border-right: 0px" title="tsql2sday" border="0" alt="tsql2sday" align="left" src="http://codegumbo.com/images/TSQL2sDayResolutions_C774/tsql2sday.jpg" width="154" height="154" /></a> For this month’s T-SQL Tuesday, <a href="http://www.midnightdba.com/Jen/2011/01/tsql-tuesday-014/" target="_blank">Jen McCown</a> asks:</p>
<blockquote><p>So tell us: what techie resolutions have you been pondering, and why?&#160; Are you heading for a certification? An award? Are you looking to pick up CLR because that guy at the Summit said it’s “bitchin’”? Go crazy…</p>
</blockquote>
<p>I’ve already covered a lot of my techie resolutions <a href="http://codegumbo.com/index.php/2011/01/05/the-ubiquitous-resolution-post/" target="_blank">in this post</a>, but here’s a recap, with some expanded thoughts:</p>
<ul>
<li><strong>I vow to learn something new every month.</strong>&#160; <a href="http://codegumbo.com/index.php/category/development/something-new/" target="_blank">I’ve already started on this one</a>, but I need to keep working on it.&#160;&#160; For example, I’m working on XML and XQuery this month; next month, I’m thinking SSIS.</li>
<li><strong>I vow to be more involved in the technical community.</strong>&#160; I’ve slipped out of tweeting (mostly because it’s blocked on our corporate network); I will do more.&#160; I also want to read more blogs, as well as do a LOT more blogging myself.&#160; For example, I plan to participate in every T-SQL Tuesday for 2011.&#160; I also plan to present at least 6 times this year.</li>
<li><strong>I will earn my MCITP: Database Developer certification this year.</strong>&#160; Been meaning to do it; just haven’t invested the time to do so.</li>
</ul>
<p>On a personal note, I want to tackle a few more technical projects that have been hovering over my head:</p>
<ul>
<li><strong>I want to do more with pictures and videos.</strong>&#160; I have a nice digital camera, and a nice Flip video camera, but I don’t do squat with them.&#160; I’m horrible about leaving them behind when I travel; I will use them as needed.</li>
<li><strong>My fiancée is an iPod user (like 90% of the world); I am not (I have an Archos).</strong>&#160; Merging our music into iTunes is not going to be fun (especially since I’ve never used it), but in the long run, it’ll be the right thing to do for us.</li>
<li><strong>I want to work smarter, not harder, so I can play more.</strong>&#160; There’s lots of little services out there (like Remember the Milk, Yodlee.com, Google calendars, etc) which will help me manage my life on the move (shuttling between my apartment, my fiancée’s house, and my office).</li>
</ul>
<p>Short, sweet, but at least it’s submitted <img src='http://codegumbo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2011/01/11/tsql2sday-resolutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#TSQL2sDay &#8211; My Least Favorite SQL Server Myth</title>
		<link>http://codegumbo.com/index.php/2010/10/12/tsql2sday-my-least-favorite-sql-server-myth/</link>
		<comments>http://codegumbo.com/index.php/2010/10/12/tsql2sday-my-least-favorite-sql-server-myth/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 19:14:35 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2010/10/12/tsql2sday-my-least-favorite-sql-server-myth/</guid>
		<description><![CDATA[&#160; It’s time again for another T-SQL Tuesday, hosted this month by Sankar Reddy; the topic is misconceptions in SQL Server.&#160; It’s been a while since I wrote one of these (I usually forget about them until the following Wednesday), but this topic is a good one.&#160; I’ve had many discussions with people about the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://SankarReddy.com/2010/10/invitation-to-participate-in-t-sql-tuesday-11-misconceptions-in-sql-server/" target="_blank" jquery1286906356588="9"><img style="border-right-width: 0px; margin: 0px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="TSQL2sDay150x150" border="0" alt="TSQL2sDay150x150" align="left" src="http://SankarReddy.com/wp-content/uploads/2010/10/TSQL2sDay150x150.jpg" width="150" height="150" /></a>
<p>&#160;</p>
<p>It’s time again for another <a href="http://sankarreddy.com/2010/10/invitation-to-participate-in-t-sql-tuesday-11-misconceptions-in-sql-server" target="_blank">T-SQL Tuesday</a>, hosted this month by Sankar Reddy; the topic is misconceptions in SQL Server.&#160; It’s been a while since I wrote one of these (I usually forget about them until the following Wednesday), but this topic is a good one.&#160; I’ve had many discussions with people about the following myth for a long time, so it’s nice to be able to put it to rest again.</p>
<p>The myth?&#160; “<strong>You should always use stored procedures in your code, because SQL Server is optimized for stored procedure re-use.</strong>”&#160; Don’t get me wrong; there are lots of arguments to use stored procedures (security, obfuscation, code isolation), but performance is not necessarily a good one.&#160;&#160; This myth has been around for a long time (since SQL Server 7), and Binging “<a href="http://www.bing.com/search?q=stored+procedures+SQL+Server+performance&amp;src=IE-SearchBox&amp;FORM=IE8SRC" target="_blank">stored procedures SQL Server performance</a>” yields such gems as the following:</p>
<blockquote><p><strong><a href="http://www.sql-server-performance.com/tips/stored_procedures_p2.aspx" target="_blank">SQL Server Performance Tuning for Stored Procedures</a></strong><strong></strong></p>
<p>As you know, one of the biggest reasons to use <strong>stored</strong> <strong>procedures</strong> instead of ad-hoc queries is the <strong>performance</strong> gained by using them. The problem that is that <strong>SQL</strong> <strong>Server</strong> will only &#8230; <cite>www.<strong>sql</strong>-<strong>server</strong>-<strong>performance</strong>.com/tips/<strong>stored</strong>_<strong>procedures</strong>_p2.aspx</cite> · <a href="http://cc.bingj.com/cache.aspx?q=stored+procedures+SQL+Server+performance&amp;d=4683047790577126&amp;mkt=en-US&amp;setlang=en-US&amp;w=56232362,36277df">Cached page</a></p>
<p><a href="http://articles.techrepublic.com.com/5100-10878_11-1045447.html"><strong>Increase SQL Server stored procedure performance with these tips</strong></a></p>
<p>Database developers often use <strong>stored</strong> <strong>procedures</strong> to increase <strong>performance</strong>. Here are three tips to help you get the most from your <strong>SQL</strong> <strong>Server</strong> <strong>stored</strong> &#8230; <cite>articles.techrepublic.com.com/5100-10878_11-1045447.html</cite> · <a href="http://cc.bingj.com/cache.aspx?q=stored+procedures+SQL+Server+performance&amp;d=4874762243998158&amp;mkt=en-US&amp;setlang=en-US&amp;w=86f6a2c8,7c2c440c">Cached page</a></p>
<p>&#160;</p>
</blockquote>
<p>The guts of this myth originate from the fact that prior to version 7 (released in 1998), SQL Server WOULD precompile stored procedures and save an execution plan for future reuse of that procedure, BUT THAT CHANGED AS OF VERSION 7.0.&#160; Here’s a quote from <a href="http://msdn.microsoft.com/en-us/library/aa174792(SQL.80).aspx" target="_blank">Books Online (SQL Server 2000)</a> that tries to explain what happened (emphasis added by me):</p>
<blockquote><h6>Stored Procedures and Execution Plans</h6>
<p><strong>In SQL Server version 6.5 and earlier, stored procedures were a way to partially precompile an execution plan</strong>. At the time the stored procedure was created, a partially compiled execution plan was stored in a system table. Executing a stored procedure was more efficient than executing an SQL statement because SQL Server did not have to compile an execution plan completely, it only had to finish optimizing the stored plan for the procedure. Also, the fully compiled execution plan for the stored procedure was retained in the SQL Server procedure cache, meaning that subsequent executions of the stored procedure could use the precompiled execution plan.</p>
<p>SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. <strong>A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans.</strong> The database engine uses an efficient algorithm for comparing new Transact-SQL statements with the Transact-SQL statements of existing execution plans. If the database engine determines that a new Transact-SQL statement matches the Transact-SQL statement of an existing execution plan, it reuses the plan. This reduces the relative performance benefit of precompiling stored procedures by extending execution plan reuse to all SQL statements.</p>
<p>SQL Server 2000 and SQL Server version 7.0 offer new alternatives for processing SQL statements. For more information, see <a href="http://msdn.microsoft.com/en-us/library/aa175261(v=SQL.80).aspx">Query Processor Architecture</a>.</p>
</blockquote>
<p>Note from the above quote that the query optimizer now uses execution plans for ALL T-SQL statements, not just stored procedures.&#160; The perceived performance gain from stored procedures stems not from some magic use of CREATE PROC, but rather in plan re-use, which is available to ad-hoc queries as well.&#160; </p>
<p>So what promotes plan re-use?&#160; The simplest answer is parameterization; SQL statements which use parameters efficiently (which includes many stored procedures) will be more likely to reuse a plan.&#160; Developers should focus on making the most out of parameters, rather than simply assuming that a stored procedure will be efficient simply because of some magical aspect of said procs.</p>
<p>A final thought: For a great starting place on understanding SQL Server plan reuse, see <a title="http://msdn.microsoft.com/en-us/library/ee343986(SQL.100).aspx" href="http://msdn.microsoft.com/en-us/library/ee343986(SQL.100).aspx">http://msdn.microsoft.com/en-us/library/ee343986(SQL.100).aspx</a>, including the Appendix A: When Does SQL Server Not Auto-Parameterize Queries.&#160; Also, <a href="http://weblogs.asp.net/zeeshanhirani/archive/2008/08/07/linq-to-sql-query-plans-not-being-reused.aspx" target="_blank">this post</a> by Zeeshan Hirani explains why LINQ to SQL query plans don’t get reused.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2010/10/12/tsql2sday-my-least-favorite-sql-server-myth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#TSQL2sDay 003: Maslow and relational design</title>
		<link>http://codegumbo.com/index.php/2010/02/09/tsql2sday-003-maslow-and-relational-design/</link>
		<comments>http://codegumbo.com/index.php/2010/02/09/tsql2sday-003-maslow-and-relational-design/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 05:34:43 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2010/02/09/tsql2sday-003-maslow-and-relational-design/</guid>
		<description><![CDATA[Rob Farley is hosting the third installment of TSQL Tuesday, and it’s a fun one: relationships (in honor of Valentine’s Day).&#160;&#160; While I’m not currently in much of a mood to opine on the virtues of love and databases, I did think I wanted to post something a bit more esoteric this time.&#160; Not many [...]]]></description>
			<content:encoded><![CDATA[<p>Rob Farley is hosting the <a href="http://msmvps.com/blogs/robfarley/archive/2010/02/02/invitation-for-t-sql-tuesday-003-relationships.aspx" target="_blank">third installment of TSQL Tuesday</a>, and it’s a fun one: relationships (in honor of Valentine’s Day).&#160;&#160; While I’m <a href="http://codegumbo.com/index.php/2009/11/30/ot-endings-and-new-beginnings/" target="_blank">not currently in much of a mood</a> to opine on the virtues of love and databases, I did think I wanted to post something a bit more esoteric this time.&#160; Not many of you may know that I don’t have a formal background in Information Technology (some of my more <a href="http://www.danlerner.net" target="_blank">sarcastic friends</a> just held their tongues at that one); I actually have a Master of Arts in Communication, and a Master’s of Education in Instructional Technology.&#160; I tripped into IT when I failed my comprehensive exams for the doctoral program in <a href="http://www.uga.edu/spc/" target="_blank">Speech Communication at the University of Georgia</a>.&#160; Awful time, but ultimately one of the best things to ever happen to me.</p>
<p>Anyway, why is this relevant?&#160; Because the goal of this post is to attempt to extend one of the more famous models of social psychology and communication to database design; bear with me (I’m assuming that many of you either have no background in social psych or slept through it), but I’m hoping that this extension to the metaphor will benefit you in terms of your application design.</p>
<h3></h3>
<h3>Maslow: the crash course.</h3>
<p>The following is a BRIEF introduction to the theory; if you want more details, <a href="http://en.wikipedia.org/wiki/Maslow's_hierarchy_of_needs" target="_blank">Wikipedia</a> is your friend. In a nutshell, Abraham Maslow proposed that humans, as a social animal, were driven to fulfill certain basic needs in a quest for self-actualization or enlightenment.&#160; He proposed a pyramidic model of five sets (or stages) of these needs, with the four lowest ones being required to achieve before attempting the fifth; few people ever attain the fifth level, but the quest to reach that is part of our collective experience.&#160; I’ve defined the five stages below:</p>
<h2></h2>
<h4><a href="http://www.codegumbo.com/images/TSQL2sDay003Maslowandrelationaldesign_B47C/maslows_hierarchy_of_needssvg.jpg"><img style="border-right-width: 0px; margin: 5px 5px 5px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="maslows_hierarchy_of_needssvg" border="0" alt="maslows_hierarchy_of_needssvg" align="right" src="http://www.codegumbo.com/images/TSQL2sDay003Maslowandrelationaldesign_B47C/maslows_hierarchy_of_needssvg_thumb.jpg" width="413" height="318" /></a> Physiological:</h4>
<p>The basic requirements for human existence; food, water, etc.</p>
<h4>Safety:</h4>
<p>This often translates into security, but it’s different than the term we use in information technology careers; safety is the ability to acquire and maintain goods for ongoing existence.&#160; The Physiological elements are immediate needs; Safety elements are the ability to fulfill those immediate needs at a future date.</p>
<h4>Social:</h4>
<p>Where do we belong?&#160; How do we interact with others who need us (and we need)?&#160; What is our role, and how does that affect our definition of the self?</p>
<h4>Esteem:</h4>
<p>Esteem stems from the social need; once our relationship with others has been established, we can truly begin to define ourselves and the virtue of our importance in the world.</p>
<h4>Self-Actualization:</h4>
<p>Self-actualization is the ultimate fulfillment of one’s potential; to be what one is, without need for constant reinforcement from other beings, yet able to exist in harmony with purpose.&#160; Few people have ever attained this stage, but as stated before, the quest to reach the top of the pyramid drives human development.</p>
<h3>So what does this mean to the database designer?</h3>
<p>Why is all of this important?&#160; This is not a perfect analogy, but if we extend Maslow’s model to the area of database design, some interesting questions arise (particularly in the third and fourth stages, which is why I felt like this point would be relevant to the TSQL Tuesday challenge of relationships).&#160; Let’s take each stage, and step through them again.</p>
<h4>Physiological:</h4>
<p>While applications don’t have physiological needs, they DO require certain basic elements for long term survival.&#160; Questions to consider at this stage are things like: How much space will I need?&#160; What are the server requirements?&#160; Can my database live in cloud or a mobile device?&#160;&#160; What sort of I/O concerns do I have?</p>
<h4>Safety:</h4>
<p>Recall that safety is NOT security (in terms of who has access to the data), but it is security in terms of long-term survival of the application.&#160; Is the database you’re designing intended for a long-term project, or is it “throw-away” code?&#160; Have you designed it in such a way so that it’s easy to replace without impacting the dependent application?</p>
<h4>Social:</h4>
<p>Speaking of dependent applications (and herein lies the relationship aspect of this post), is your database application designed so that it is loosely related and decoupled from the application?&#160; Does the database fulfill the needed role within the relationship (data storage), without treading too far into business logic?&#160; Can the database handle multiple relationships with various applications (UI/reporting/business services).</p>
<h4>Esteem:</h4>
<p>Closely related to the social nature of the database within the application stack is the need for self-esteem within the database; can the database meet the the needs of the dependent applications WHILE retaining enough information to establish new relationships?&#160; A classic example of this is the lookup table; a database with low self-esteem will only store the enumerated values provided to it by some other application.&#160; </p>
<p>Without the enabling application, the database lacks sufficient internal definition to validate meaning; in practical terms, this means that the database is not decoupled from the application enough to enable the development of alternate accessing applications.&#160; For example, my day job is to reverse engineer vendor databases; few things in the world are more disturbing than a table full of numbers without any sort of category associated with that number.&#160; The application designer decided to store that enumeration in the application; security through obfuscation IS a method of securing your database, but not a very effective one.</p>
<p>A high-self esteem database will store all of the appropriate lookup values (complete with constraints) in order to provide complete validity within the structure.&#160; The database can then be reused by several different applications, without requiring a complete set of business rules to determine those relationships.&#160;&#160;&#160; The data layer is definitional; the business layer should be procedural.</p>
<h4><a href="http://www.codegumbo.com/images/TSQL2sDay003Maslowandrelationaldesign_B47C/hal1.jpg"><img style="border-right-width: 0px; margin: 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="hal[1]" border="0" alt="hal[1]" align="left" src="http://www.codegumbo.com/images/TSQL2sDay003Maslowandrelationaldesign_B47C/hal1_thumb.jpg" width="240" height="240" /></a> Self-Actualization:</h4>
<p>I have to admit that discussing self-actualization in regards to application design makes me think of HAL.&#160; “I’m sorry, Dave….”</p>
<p>To try and stay on track with this metaphor, self-actualization is the basic premise of BI; when your database can start providing you knowledge instead of just data, it has attained the highest level of potential.&#160; Few apps make it that far without requiring substantial redesign, but the ones that do are invaluable to the enterprise they support.</p>
<h3>So where are we?</h3>
<p>Dunno.&#160; I hope this little exercise made your brain hurt just a bit, and opened up a new metaphor for understanding database design issues within the application stack.&#160;&#160; If you have more questions than answers, that’s a good place to be.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2010/02/09/tsql2sday-003-maslow-and-relational-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>#TSQL2sDay  001:  Date/Time Issues and Data Modeling</title>
		<link>http://codegumbo.com/index.php/2009/12/08/tsql2sday-001-datetime-issues-and-data-modeling/</link>
		<comments>http://codegumbo.com/index.php/2009/12/08/tsql2sday-001-datetime-issues-and-data-modeling/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 21:28:26 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[TSQL2sDay]]></category>

		<guid isPermaLink="false">http://codegumbo.com/index.php/2009/12/08/tsql2sday-001-datetime-issues-and-data-modeling/</guid>
		<description><![CDATA[This is my first post for T-SQL Tuesday (thanks, Adam, for the idea!); I’m hoping this will be a continuing block party, and hopefully connect a lot of us SQL bloggers together.&#160; There’s a lot of great content out there, and sometimes it’s just tough to find. Just a few notes before I get started [...]]]></description>
			<content:encoded><![CDATA[<p>This is my first post for <a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/11/30/invitation-to-participate-in-t-sql-tuesday-001-date-time-tricks.aspx" target="_blank">T-SQL Tuesday</a> (thanks, <a href="http://sqlblog.com/blogs/adam_machanic/default.aspx" target="_blank">Adam</a>, for the idea!); I’m hoping this will be a continuing block party, and hopefully connect a lot of us SQL bloggers together.&nbsp; There’s a lot of great content out there, and sometimes it’s just tough to find.</p>
<p>Just a few notes before I get started on this; for this particular post, I’m pulling from my experiences as a data architect for the last few years.&nbsp; I spend a lot of time reverse-engineering other databases in order to centralize and analyze data from a variety of sources.&nbsp; Most of my efforts pre-date SQL 2008, so I don’t have a lot of experience with the newer datetime data types introduced in that version; however, I’ll point you in that direction if I think it might address an issue that I raise.&nbsp; Also, there’s no particular order to my list of notes; think of it as a random collection of date-time issues.&nbsp; Some of them may be specific to T-SQL and SQL Server, and others may just be procedural notes about data integration (and thus may be applicable to other platforms).</p>
<p>Ready?&nbsp; Let’s get started.</p>
<h3>1.&nbsp; If you are a database professional that works with SQL Server, strike the word “timestamp” from your vocabulary.</h3>
<p>Way back when SQL Server was in its infancy, some developer decided that timestamp would be a good name for a binary column that changes whenever the row was updated.&nbsp; I don’t know the origins of the timestamp datatype; all I know is that it has nothing to do with date or time.&nbsp; So why am I including it in a post about Date and Times?&nbsp; Because for most speakers of the English Language, a timestamp is a marker that indicates when an event happens.&nbsp; Many junior SQL Server developers and even senior developers on other platforms will often be confused by the timestamp datatype, and wonder what’s going on with that column that they just added.</p>
<p>My recommendation?&nbsp; Avoid using the word timestamp in design discussions.&nbsp;&nbsp; When someone mentions “we need to add a timestamp to that row”, make sure that you understand what they mean.&nbsp; Usually they want you to add a column to capture what datetime the event happened, NOT an indicator of when the row will change.&nbsp; Note that I’m suggesting that you avoid the WORD timestamp; obviously, if a database requires a timestamp column, use it.</p>
<h3></h3>
<h3>2.&nbsp; Does anybody really know what time it is?</h3>
<p>One of the benefits of a distributed architecture is that your database server doesn’t have to live on the same box as your application server; in fact, your database server may not be in the same timezone as your application server.&nbsp; And, if you’re using a webserver as an application host, your user may not be in the same timezone as either your application or your database.</p>
<p>I mention this because as a database designer, you may need to capture multiple points-in-time (or you may not); you cannot assume that the application is consistently providing the same time reference for all users.&nbsp; If you’re using a truly distributed design, you may have one host in New York and one in Los Angeles, and your users may connect to either one to enter in data; what date do you use?</p>
<p>For this reason, I usually advocate two things: 1) always include a database-centric referential date of event (in other words, include a column with a default value of the database time for important data), and 2) always use UTC for saving time.&nbsp; That second statement’s important enough to warrant a note of its own.</p>
<h3>3.&nbsp; When in doubt, use UTC.</h3>
<p>Time is relative to geography; the application you build today in New York may eventually have clients in New Delhi.&nbsp; If your processes could ever have the potential of crossing multiple time zones (for example, you want to track shipping from Atlanta to Oregon), you’d get the most benefit out of storing your data as UTC in order to understand the relationship between event A (which occurred in EST) and event B (in PST).&nbsp; It’s a lot easier to change the display of events to a local time from UTC than it is to convert from local time to UTC, do the math, and then convert back.</p>
<p>One problem with recording data with UTC times that I’ve encountered is that is you have centralized data, but distributed users that want to see their data in local time, building OLAP cubes is a pain.&nbsp; If you’re wanting to analyze data for Atlanta and data for Oregon, Atlanta’s day begins 3 hours earlier than Oregon’s.&nbsp; I have not found an easy solution to this yet, but am starting to investigate the new datetimeoffset data type in SQL 2008.</p>
<h3>4.&nbsp; Use a standard notation for calculating differences between dates.</h3>
<p>Obviously the new date and time datatypes in SQL 2008 address a desire of many in the development community for a method of storing ONLY the date or time of an event, but if you’re using an application that still stores the datetime of an event and you need to round the data to the nearest day, hour, week, whatever unit of time, then use&nbsp; a standard method for doing so.</p>
<p>(I am assuming that you are using date-based arithmetic rather than converting to a varchar and back).</p>
<p>SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)</p>
<p>is the preferred method; you can read <a href="http://codegumbo.com/index.php/2009/07/24/you-can-teach-an-old-dog/" target="_blank">why at my previous post</a>.</p>
<h3>5.&nbsp; Use a standard notation for referring to dates.</h3>
<p>Americans are weird about dates; we’re one of the few countries that puts the middle container before the smallest container followed by the largest container.&nbsp; Clueless?&nbsp; When people ask you what’s your birthdate, how do you answer?&nbsp; Most people rattle off Month-Day-Year.&nbsp;&nbsp; An international notation would be Year, Month, Day.</p>
<p>Again, if you’re designing databases and queries that might cross international borders, you’ll save yourself some debugging time in the future if you can get into the habit of using an international standard notation for referring to datetimes.&nbsp;&nbsp; A query that asks for data WHERE DateOfEvent &gt; ‘11/1/2009’ may work fine in the US (returning data newer than November 1, 2009), but may blow up in the UK (returning data greater than January 11, 2009).</p>
<p>How do you know if you’re a DBA that’s been bitten by this?&nbsp; You start dating everything (including personal checks) using a notation like ‘2009-11-01’.&nbsp; I&nbsp; get odd looks all the time (probably not just because of the way I date my checks).</p>
<h3>6.&nbsp; Finally, get used to the fact that there’s lots of methods to store datetimes.</h3>
<p>If you’re doing any sort of database integration, you’re probably familiar with many of the same issues I laid out.&nbsp; There are others that I haven’t covered, but the important thing I want to focus on is that just because I think these are good habits to get into, others have different opinions.&nbsp; Some db pro’s prefer to use epoch time rather than a standard datetime; I not only have to be familiar with my own methods of doing something (and prepared to defend those methods), but I also have to be willing to understand other methodologies so that I can do the conversion. </p>
]]></content:encoded>
			<wfw:commentRss>http://codegumbo.com/index.php/2009/12/08/tsql2sday-001-datetime-issues-and-data-modeling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

