<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'SQL Server' and 't-sql tuesday'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=SQL+Server,t-sql+tuesday&amp;orTags=0</link><description>Search results matching tags 'SQL Server' and 't-sql tuesday'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>[T-SQL Tuesday] Some code is born crap, some code achieves crapness and some code has crapness thrust upon it!</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2011/08/10/t-sql-tuesday-some-code-is-born-crap-some-code-achieves-crapness-and-some-code-has-crapness-thrust-upon-it.aspx</link><pubDate>Wed, 10 Aug 2011 15:04:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37711</guid><dc:creator>jamiet</dc:creator><description>&lt;p&gt;I recently returned to a client at which I last worked back in 2006 and, as you might imagine, that gives me ample material for a blog post dedicated to Crap Code; conveniently the subject of the latest &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2011/08/03/t-sql-tuesday-21-a-day-late-and-totally-full-of-it.aspx" target="_blank"&gt;T-SQL Tuesday&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I first worked for this client back in 2004 and that is significant because back then we were writing code on SQL Server 2000. Ah, thems were the days!!! I smiled wryly to myself when I encountered the following code in a stored procedure that I once wrote:&lt;/p&gt;
&lt;code style="font-size:12px;"&gt;&lt;span style="color:green;"&gt;--Simplified for convenience/demo&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE PROCEDURE &lt;/span&gt;&lt;span style="color:black;"&gt;[dbo].[usp_PersonInsert]&lt;br&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#434343;"&gt;@pXMLDataInsert&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;TEXT&lt;br&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp; DECLARE &lt;/span&gt;&lt;span style="color:#434343;"&gt;@vDoc&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;br&gt;&amp;nbsp;&amp;nbsp; EXECUTE &lt;/span&gt;&lt;span style="color:darkred;"&gt;sp_xml_preparedocument &lt;/span&gt;&lt;span style="color:#434343;"&gt;@vDoc &lt;/span&gt;&lt;span style="color:black;"&gt;OUTPUT&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:#434343;"&gt;@pXMLDataInsert&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:black;"&gt;ID&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:black;"&gt;Name&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;INTO&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:#434343;"&gt;#Insert&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;OpenXML&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:#434343;"&gt;@vDoc&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:red;"&gt;'NewDataSet/Table'&lt;/span&gt;&lt;span style="color:gray;"&gt;, &lt;/span&gt;&lt;span style="color:black;"&gt;2 &lt;/span&gt;&lt;span style="color:gray;"&gt;) &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:black;"&gt;ID&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:red;"&gt;'ID'&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:black;"&gt;Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;VARCHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;70&lt;/span&gt;&lt;span style="color:gray;"&gt;) &lt;/span&gt;&lt;span style="color:red;"&gt;'Name'&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;EXECUTE &lt;/span&gt;&lt;span style="color:darkred;"&gt;sp_xml_removedocument &lt;/span&gt;&lt;span style="color:#434343;"&gt;@vDoc&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT INTO &lt;/span&gt;&lt;span style="color:black;"&gt;dbo.Person&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;Name&lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;ID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;Name &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:#434343;"&gt;#Insert&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;END&lt;br&gt;&lt;br&gt;&lt;/span&gt;&lt;/code&gt;
&lt;p&gt;If you have never had the pleasure(!!!) of dealing with XML data prior to SQL Server 2005 then you're probably looking at this with a slightly confused look on your face wondering quite why we had to make this so complicated. TEXT datatype? OPENXML? System stored procs? What's in this @vDoc variable? On the other hand if you &lt;i&gt;have&lt;/i&gt; had to deal with XML pre-2005 then right now you're probably nodding sagely to yourself and remembering the not-so-good-old-days!&lt;/p&gt;
&lt;p&gt;No-one would ever write code like this today but back in 2004, before wonderous things like the XML datatype and table-valued-parameters, this was how one passed datasets into a stored procedure (well, its how &lt;i&gt;we&lt;/i&gt; did it anyway). If any of my colleagues were to write this today they would probably be despatched home with a copy of &lt;a href="http://www.amazon.com/Inside-Microsoft%C2%AE-Server%C2%AE-2008-Pro-Developer/dp/0735626022/ref=sr_1_1?ie=UTF8&amp;amp;qid=1312990060&amp;amp;sr=8-1" target="_blank"&gt;Inside SQL Server 2008 : T-SQL Programming&lt;/a&gt; tucked under their arm and ordered not to come back until they could recite it ad infinitum but back in 2004, well, we thought we were pretty cool!&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Times change, people change and coding practices change too. Code isn't born crap, it just tends to crapness the longer that it lives! Bear that in mind when coding today because in five years time you'll probably be coming back and having a little chuckle at it!&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/jamiet" target="_blank"&gt;@Jamiet&lt;/a&gt; &lt;/p&gt;

&lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2011/08/03/t-sql-tuesday-21-a-day-late-and-totally-full-of-it.aspx" target="_blank"&gt;&lt;img src="http://sqlblog.com/blogs/adam_machanic/TSQLWednesday_2C948C01.jpg"&gt;&lt;/a&gt;</description></item><item><title>T-SQL Tuesday #006: Tiger/Line Spatial Data</title><link>http://sqlblog.com/blogs/michael_coles/archive/2010/05/11/t-sql-tuesday-006-tiger-line-spatial-data.aspx</link><pubDate>Tue, 11 May 2010 23:11:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:25058</guid><dc:creator>Mike C</dc:creator><description>&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;This month’s &lt;A href="http://sqlblog.com/blogs/adam_machanic/archive/2009/11/30/invitation-to-participate-in-t-sql-tuesday-001-date-time-tricks.aspx"&gt;T-SQL Tuesday&lt;/A&gt; post is about LOB data &lt;A href="http://sqlblog.com/blogs/michael_coles/archive/2010/05/03/t-sql-tuesday-006-what-about-blob.aspx"&gt;http://sqlblog.com/blogs/michael_coles/archive/2010/05/03/t-sql-tuesday-006-what-about-blob.aspx&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;For this one I decided to post a sample Tiger/Line SQL database I use all the time in live demos. For those who aren't familiar with it, Tiger/Line data is a dataset published by the &lt;A title="...and still counting!" href="http://www.census.gov/geo/www/tiger/tgrshp2009/tgrshp2009.html"&gt;U.S. Census Bureau&lt;/A&gt;. Tiger/Line has a lot of nice detailed geospatial data down to a very detailed level.&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/SPAN&gt;It actually goes from the U.S. state level all the way down to street, feature and landmark&amp;nbsp;level. Tiger/Line data is very complete and detailed--but the best part is it's FREE.&amp;nbsp; There are lots of applications for Tiger/Line, like national [U.S.] and local mapping and&amp;nbsp;geocoding applications &lt;EM&gt;[applications that convert street addresses to (latitude, longitude) coordinates]&lt;/EM&gt;.&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/SPAN&gt;All this great data is distributed in the form of a ton of ESRI shapefiles.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;A shapefile is basically a file format that contains shape objects like points, lines and polygons. &lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/SPAN&gt;SQL Server doesn’t natively understand ESRI shapefiles, but it also stores geospatial objects like lines, points and polygons.&amp;nbsp; There some handy utilities out there for loading these files into SQL Server. &lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/SPAN&gt;Morten Nielsen has a great utility for loading shapefiles into Geometry and Geography data types at &lt;A href="http://www.sharpgis.net/page/Shape2SQL.aspx"&gt;http://www.sharpgis.net/page/Shape2SQL.aspx&lt;/A&gt;.&lt;SPAN style="mso-spacerun:yes;"&gt;&amp;nbsp; Because of the volume of data involved in this project (I loaded hundreds of shapefiles) &lt;/SPAN&gt;I decided to&amp;nbsp;roll my own small set of SSIS custom components that read ESRI shapefiles and convert them to SQL Server spatial data types &lt;EM&gt;[keep an eye out -- these components are scheduled to be published with source code by SQL Server Standard magazine in the near future]&lt;/EM&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;The sample database can be downloaded from &lt;A href="http://www.sqlkings.com/downloads/Tiger_Sample.zip"&gt;http://www.sqlkings.com/downloads/Tiger_Sample.zip&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Download the ZIP file from &lt;A href="http://www.sqlkings.com/downloads/Tiger_Sample.zip"&gt;http://www.sqlkings.com/downloads/Tiger_Sample.zip&lt;/A&gt;.&lt;/FONT&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Extract the database backup file and restore it to a SQL Server 2008 instance.&lt;/FONT&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;&lt;SPAN style="mso-spacerun:yes;"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;In the future I’ll be sharing some code samples on the blog to demonstrate Tiger/Line data (as well as spatial data from other sources) based on this database.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Here are a couple of quick queries you can run against this sample database to view the spatial data in SSMS 2008.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; Tiger&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;State&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;IMG style="WIDTH:690px;HEIGHT:328px;" title="US of A" alt="US of A" src="http://www.sqlkings.com/blog_images/state.png" width=690 height=328&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="mso-no-proof:yes;"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; c&lt;SPAN style="COLOR:gray;"&gt;.*&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; Tiger&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR:blue;"&gt;State&lt;/SPAN&gt; s&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:gray;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;INNER&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;JOIN&lt;/SPAN&gt; Tiger&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;County c&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;ON&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; s&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;STATEFP &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; c&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;STATEFP&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; s&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;STUSPS &lt;SPAN style="COLOR:gray;"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR:red;"&gt;'TX'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;IMG style="WIDTH:690px;HEIGHT:329px;" title="All my ex's live in Texas" alt="All my ex's live in Texas" src="http://www.sqlkings.com/blog_images/county.png" width=690 height=329&gt;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="mso-no-proof:yes;"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; &lt;SPAN style="COLOR:gray;"&gt;*&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-layout-grid-align:none;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;FROM&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; Tiger&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;ZCTA z&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';COLOR:blue;FONT-SIZE:10pt;mso-no-proof:yes;"&gt;WHERE&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY:'Courier New';FONT-SIZE:10pt;mso-no-proof:yes;"&gt; z&lt;SPAN style="COLOR:gray;"&gt;.&lt;/SPAN&gt;ZCTA5CE00 &lt;SPAN style="COLOR:gray;"&gt;LIKE&lt;/SPAN&gt; &lt;SPAN style="COLOR:red;"&gt;'0[7-8]%'&lt;/SPAN&gt;&lt;SPAN style="COLOR:gray;"&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;IMG style="WIDTH:687px;HEIGHT:332px;" title=Jersey--Howyadoin? alt=Jersey--Howyadoin? src="http://www.sqlkings.com/blog_images/zcta.png" width=542 height=268&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="mso-no-proof:yes;"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;This last one works because all of the ZIP Code tabulation areas for the state of New Jersey start with '07' and '08'.&amp;nbsp; There are similar relationships between other ZCTA prefixes&amp;nbsp;and their states.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;Next time we'll look at using SQL Server-based spatial data with online mapping programs like Bing maps.&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&amp;nbsp;&lt;/P&gt;&lt;o:p&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;FONT size=3 face=Calibri&gt;*For more information about Tiger/Line data visit&amp;nbsp;&lt;A href="http://www.census.gov/geo/www/tiger/tgrshp2009/tgrshp2009.html"&gt;http://www.census.gov/geo/www/tiger/tgrshp2009/tgrshp2009.html&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;&lt;/o:p&gt;</description></item><item><title>T-SQL Tuesday #004: Why Doesn't TDE Encrypt My FILESTREAM Data?</title><link>http://sqlblog.com/blogs/michael_coles/archive/2010/03/08/t-sql-tuesday-004-why-doesn-t-tde-encrypt-my-filestream-data.aspx</link><pubDate>Tue, 09 Mar 2010 02:42:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:22972</guid><dc:creator>Mike C</dc:creator><description>&lt;P&gt;This post is my entry for &lt;A title="Adam Machanic's Blog" href="http://sqlblog.com/blogs/adam_machanic/default.aspx" target=_blank&gt;Adam Machanic's&lt;/A&gt; &lt;A title="T-SQL Tuesday #004" href="http://www.straightpathsql.com/archives/2010/03/invitation-for-t-sql-tuesday-004-io/" target=_blank&gt;T-SQL Tuesday #004&lt;/A&gt;, hosted this time by &lt;A title="Mike Walsh" href="http://www.straightpathsql.com/aboutus/" target=_blank&gt;Mike Walsh&lt;/A&gt;. I was at the RSA Conference in San Francisco last week discussing database encryption options in SQL Server 2008 and one question seemed to keep coming up. The question concerns &lt;A title="FILESTREAM Overview" href="http://msdn.microsoft.com/en-us/library/bb933993.aspx" target=_blank&gt;FILESTREAM&lt;/A&gt; and &lt;A title="Understanding TDE" href="http://msdn.microsoft.com/en-us/library/bb934049.aspx"&gt;Transparent Data Encryption&lt;/A&gt; (TDE), but first a little background:&lt;/P&gt;
&lt;P&gt;FILESTREAM is a new&amp;nbsp;SQL Server 2008 feature. When you apply the FILESTREAM attribute to a &lt;A title=varbinary href="http://msdn.microsoft.com/en-us/library/ms188362.aspx"&gt;varbinary(max)&lt;/A&gt; column SQL Server stores your BLOB data in a "FILESTREAM data container" (an NTFS directory structure) instead of directly in the database (the MDF and NDF files that normally hold all your data). The advantages of FILESTREAM are speed (streaming NTFS&amp;nbsp;access for large files) and the ability to store BLOB data larger than 2.1 GB.&lt;/P&gt;
&lt;P&gt;One of the downsides of FILESTREAM concerns another new feature, TDE. TDE&amp;nbsp;transparently encrypts your database, adding a layer of protection against physical theft of your database files and storage devices. But TDE does not encrypt FILESTREAM data. The question I kept getting was "why?"&amp;nbsp; The answer is fairly simple -- but a picture's worth a thousand words:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="WIDTH:441px;HEIGHT:240px;" title=TDE alt=TDE src="http://e60ybw.bay.livefilestore.com/y1pQnPEkhA79JnqanROmfWvN1V2efDqOZYfYWtlVzuhCvibk6fuQMvWa1mqL3pRzsYHUAW6CRFYEtgAdJojBTYaJlzNqpYYX20N/iFTS_TDE.png" width=441 height=240&gt;&lt;/P&gt;
&lt;P&gt;As you can see in the picture,&amp;nbsp;TDE sits midway between your physical storage and SQL Server's IO buffers. This ensures that everything that passes through the IO buffers gets encrypted on its way to persistent storage and decrypted on its way back out of storage. This is also&amp;nbsp;why it's "transparent" to your applications, developers and users. SQL Server reads and writes data through the IO buffers in 8 KB pages. FILESTREAM achieves much of its performance enhancement for BLOB data by simply bypassing the IO buffers. But since TDE acts only on data passing through the IO buffers, it misses FILESTREAM BLOB data completely.&lt;/P&gt;
&lt;P&gt;Fortunately there are plenty of other options for encrypting your FILESTREAM data -- you can&amp;nbsp;use &lt;A title=EFS href="http://msdn.microsoft.com/en-us/library/ms995356.aspx"&gt;Windows Encrypting File System&lt;/A&gt; (EFS), &lt;A title=BitLocker href="http://support.microsoft.com/kb/933246"&gt;BitLocker&lt;/A&gt;&amp;nbsp;or third-party file/folder/volume encrypting software, for instance.&lt;/P&gt;</description></item><item><title>T-SQL Tuesday: Easy Extended Properties</title><link>http://sqlblog.com/blogs/michael_coles/archive/2010/01/12/t-sql-tuesday-easy-extended-properties.aspx</link><pubDate>Tue, 12 Jan 2010 05:18:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20901</guid><dc:creator>Mike C</dc:creator><description>&lt;P&gt;Ahhh, attention to detail.&amp;nbsp;I misread &lt;A href="http://sqlblog.com/blogs/adam_machanic/archive/2010/01/04/invitation-for-t-sql-tuesday-002-a-puzzling-situation.aspx"&gt;Adam's T-SQL Tuesday&lt;/A&gt; rules and posted early.&amp;nbsp;So here I go again :)&amp;nbsp;This time the puzzle is how to add and update metadata via extended properties in the database with minimal headaches.&lt;/P&gt;
&lt;P&gt;SQL Server supports a great feature for storing database object metadata in the database in the form of&amp;nbsp;&lt;A href="http://msdn.microsoft.com/en-us/library/ms190243.aspx"&gt;extended properties&lt;/A&gt;. The main benefits of extended properties over custom solutions are: (1) extended properties are managed and&amp;nbsp;stored internally by SQL Server; (2) many third-party applications retrieve/use extended properties. Perhaps the best known extended property is the "&lt;EM&gt;MS_Description&lt;/EM&gt;" property, which is set by the "&lt;EM&gt;Description&lt;/EM&gt;" field in the SSMS designer pages (shown below):&lt;/P&gt;
&lt;P&gt;&lt;IMG style="WIDTH:582px;HEIGHT:314px;" title="Setting MS_Description property in SSMS" alt="Setting MS_Description property in SSMS" src="http://e60ybw.bay.livefilestore.com/y1pU1-SwphvYGZJq1pYzlS2CJ0JtfHcJxLiBqlztLmlWg3WC9-upPogOMkBsvk768lrUsZSONLG3tPqOd0W40cvOzkCV7I7UNB1/extended.property.description.png" width=951 height=445&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Extended properties are created as user-defined key/value pairs assigned to database objects. You can define the extended propery keys and their associated values to be anything you like, although there are a few commonly used ones (like "&lt;EM&gt;MS_Description&lt;/EM&gt;"). The values are defined as sql_variant data types. Here are some sample key/value pairs:&lt;/P&gt;
&lt;DIV align=center&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV align=center&gt;
&lt;TABLE style="BORDER-BOTTOM:medium none;BORDER-LEFT:medium none;BORDER-COLLAPSE:collapse;BORDER-TOP:medium none;BORDER-RIGHT:medium none;mso-border-top-alt:solid #4F81BD 1.0pt;mso-border-top-themecolor:accent1;mso-border-bottom-alt:solid #4F81BD 1.0pt;mso-border-bottom-themecolor:accent1;mso-yfti-tbllook:1184;mso-padding-alt:0in 5.4pt 0in 5.4pt;" class=MsoTableLightShadingAccent1 cellSpacing=0 cellPadding=0&gt;

&lt;TR style="mso-yfti-irow:-1;mso-yfti-firstrow:yes;"&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:131.4pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#4f81bd 1pt solid;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-top-themecolor:accent1;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:5;" class=MsoNormal&gt;&lt;B&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;Key&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#4f81bd 1pt solid;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-top-themecolor:accent1;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:1;" class=MsoNormal&gt;&lt;B&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;Value&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:0;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:131.4pt;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:68;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;MS_Description&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:64;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;This table represents the culmination of a lifelong dream.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:1;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:131.4pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:4;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;Caption&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;GrandTotal Column&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:2;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:131.4pt;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:68;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;Last_Update&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:64;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;2009-07-09&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:3;mso-yfti-lastrow:yes;"&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:131.4pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:4;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;Version&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT size=3&gt;&lt;FONT face=Cambria&gt;1.0.1&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;/DIV&gt;
&lt;P&gt;Extended properties allow you to store metadata in the database closely tied to database objects. So what's the downside to extended properties? Basically the only real issue with them is the nonintuitive syntax. In order to use extended properties you have to use stored procedures like &lt;STRONG&gt;sp_addextendedproperty&lt;/STRONG&gt;. The following stored procedure call sets the &lt;EM&gt;MS_Description&lt;/EM&gt; extended property on the &lt;EM&gt;&lt;STRONG&gt;bar&lt;/STRONG&gt;&lt;/EM&gt; column of the &lt;STRONG&gt;&lt;EM&gt;dbo.foo&lt;/EM&gt;&lt;/STRONG&gt; table:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;EXECUTE sys.sp_addextendedproperty&lt;BR&gt;&amp;nbsp; @name = N'MS_Description',&lt;BR&gt;&amp;nbsp; @value = N'This is the bar column',&lt;BR&gt;&amp;nbsp; @level0type = N'SCHEMA',&lt;BR&gt;&amp;nbsp; @level0name = N'dbo',&lt;BR&gt;&amp;nbsp; @level1type = N'TABLE',&lt;BR&gt;&amp;nbsp; @level1name = N'foo',&lt;BR&gt;&amp;nbsp; @level2type = N'COLUMN',&lt;BR&gt;&amp;nbsp; @level2name = N'bar';&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;As you can see in the example, this syntax requires you to specify not only the extended property name/value pair and the individual components of the column's fully-qualified name, but also the types of each object.&amp;nbsp;Updating and deleting extended properties using the system stored procedures requires the same type of information.&amp;nbsp;A painful process to say the least.&lt;/P&gt;
&lt;P&gt;The attached code sample creates an extended properties management schema named &lt;STRONG&gt;ExtProps&lt;/STRONG&gt;. The &lt;STRONG&gt;ExtProps&lt;/STRONG&gt; schema contains stored procedures that make it easier to add, update and delete extended properties. With these procedures you specify the object&amp;nbsp;name using the standard SQL Server 1-, 2-, or&amp;nbsp;3-part naming convention along with the extended property name/value pair. The procedure will automatically figure out what type of object you're referencing. The following sample performs the same function as the previous example:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;EXECUTE&amp;nbsp;ExtProps.PropInsert N'dbo.foo.bar',&lt;BR&gt;&amp;nbsp; N'MS_Description',&lt;BR&gt;&amp;nbsp; N'This is the bar column';&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The &lt;STRONG&gt;ExtProps.PropInsert&lt;/STRONG&gt;, &lt;STRONG&gt;ExtProps.PropDelete&lt;/STRONG&gt; and &lt;STRONG&gt;ExtProps.PropUpdate&lt;/STRONG&gt; procedures use the object name you pass in to narrow down the type of object you're referencing. They then use the SQL Server catalog views to figure out exactly which object it should modify the extended properties on. The following table lists the types of object and the names expected by the procedures:&lt;/P&gt;
&lt;P&gt;
&lt;TABLE style="BORDER-BOTTOM:medium none;BORDER-LEFT:medium none;BORDER-COLLAPSE:collapse;BORDER-TOP:medium none;BORDER-RIGHT:medium none;mso-border-top-alt:solid #4F81BD 1.0pt;mso-border-top-themecolor:accent1;mso-border-bottom-alt:solid #4F81BD 1.0pt;mso-border-bottom-themecolor:accent1;mso-yfti-tbllook:1184;mso-padding-alt:0in 5.4pt 0in 5.4pt;" class=MsoTableLightShadingAccent1 cellSpacing=0 cellPadding=0&gt;

&lt;TR style="mso-yfti-irow:-1;mso-yfti-firstrow:yes;"&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:162.9pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#4f81bd 1pt solid;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-top-themecolor:accent1;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:5;" class=MsoNormal&gt;&lt;B&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Name&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#4f81bd 1pt solid;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-top-themecolor:accent1;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:1;" class=MsoNormal&gt;&lt;B&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Object Type&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:0;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:162.9pt;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:68;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;NULL&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:64;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Current database&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:1;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:162.9pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:4;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;1-part&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Schema&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:2;"&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:162.9pt;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:68;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;2-part (&lt;I style="mso-bidi-font-style:normal;"&gt;schema.object&lt;/I&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#f0f0f0;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BACKGROUND:#d3dfee;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-background-themecolor:accent1;mso-background-themetint:63;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:64;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Table, View, Procedure, Function, Aggregate, Synonym, Queue&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR style="mso-yfti-irow:3;mso-yfti-lastrow:yes;"&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:162.9pt;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;mso-yfti-cnfc:4;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;mso-bidi-font-weight:bold;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;3-part (&lt;I style="mso-bidi-font-style:normal;"&gt;schema.object.column&lt;/I&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD style="BORDER-BOTTOM:#4f81bd 1pt solid;BORDER-LEFT:#f0f0f0;PADDING-BOTTOM:0in;BACKGROUND-COLOR:transparent;PADDING-LEFT:5.4pt;WIDTH:2.5in;PADDING-RIGHT:5.4pt;BORDER-TOP:#f0f0f0;BORDER-RIGHT:#f0f0f0;PADDING-TOP:0in;mso-border-bottom-themecolor:accent1;"&gt;
&lt;P style="LINE-HEIGHT:normal;MARGIN:0in 0in 0pt;" class=MsoNormal&gt;&lt;SPAN style="COLOR:#365f91;mso-themecolor:accent1;mso-themeshade:191;"&gt;&lt;FONT face=Cambria&gt;&lt;FONT size=3&gt;Column&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;/P&gt;
&lt;P&gt;Note that these procedures do not yet handle all the different objects to which you can assign extended properties. For instance, it does not currently handle extended properties for procedure parameters, event notifications, and a handful of others.&lt;/P&gt;
&lt;P&gt;When you want to retrieve extended properties from the database you can use the standard system view, &lt;STRONG&gt;sys.extended_properties&lt;/STRONG&gt;, or the &lt;STRONG&gt;fn_listextendedproperty&lt;/STRONG&gt; system function. As an alternative, the &lt;STRONG&gt;ExtProps&lt;/STRONG&gt; schema includes a new view -- &lt;STRONG&gt;ExtProps.Properties&lt;/STRONG&gt;. This view has two advantages over the built-in system views:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV&gt;In addition to all the data returned by the &lt;STRONG&gt;sys.extended_properties&lt;/STRONG&gt; view the &lt;STRONG&gt;ExtProps.Properties&lt;/STRONG&gt; view also includes schema, object, and column names.&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;The &lt;STRONG&gt;ExtProps.Properties&lt;/STRONG&gt; view has triggers built on it that allow you to insert, delete, and update extended properties using standard SQL DML statements. The triggers actually call the relevant stored procedures, once for each row of extended property data being modified.&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The figure below show sample output from the &lt;STRONG&gt;ExtProps.Properties&lt;/STRONG&gt; view:&lt;/P&gt;
&lt;P&gt;&lt;IMG style="WIDTH:706px;HEIGHT:230px;" title="ExtProps.Properties View" alt="ExtProps.Properties View" src="http://e60ybw.bay.livefilestore.com/y1pVXI1Cqet03aAPUr1YX_VFtvoL6dU1i97dqeG3XMQE9RXSxUcgvTG9BcCqa7X3PQl9ptAQ_oNk1ld4r37aca9PXEGj4wOQSW1/extended-properties-view.png" width=1204 height=352&gt;&lt;/P&gt;
&lt;P&gt;The script attached to this posting creates the &lt;STRONG&gt;ExtProps&lt;/STRONG&gt; schema, procedures and view.&lt;/P&gt;</description></item><item><title>Unambiguous date formats : T-SQL Tuesday #001</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2009/12/08/unambiguous-date-formats-t-sql-tuesday-001.aspx</link><pubDate>Tue, 08 Dec 2009 20:23:41 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:19629</guid><dc:creator>jamiet</dc:creator><description>&lt;p&gt;One of the most commonly used data types in SQL Server is &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; which unfortunately has some vagaries around how values get casted. A typical method for defining a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; literal is to write it as a character string and then cast it appropriately. The cast syntax looks something like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;       &lt;tr&gt;         &lt;td&gt;           &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE &lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;NVARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;19&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;);
&lt;br /&gt;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately in SQL Server 2005 the result of the cast operation may be dependent on your current language setting. You can discover your current language setting by executing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;@@LANGUAGE&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;To demonstrate how your language setting can influence the results of a cast take a look at the following code:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER&amp;#160; DATABASE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;COMPATIBILITY_LEVEL &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;90 &lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Behave like SQL Server 2005
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;USE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE &lt;/font&gt;&lt;font color="#434343"&gt;@t &lt;/font&gt;&lt;font color="blue"&gt;TABLE &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;dateString&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;NVARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;19&lt;/font&gt;&lt;font color="gray"&gt;)
&lt;br /&gt;);
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;@t &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString&lt;/font&gt;&lt;font color="gray"&gt;)
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;VALUES &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="green"&gt;--'yyyy-MM-dd hh24:mi:ss'
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;/font&gt;&lt;font color="red"&gt;'2009-12-08T18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="green"&gt;--'yyyy-MM-ddThh24:mi:ss'
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;/font&gt;&lt;font color="red"&gt;'20091208 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;)&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="green"&gt;--'yyyyMMdd hh24:mi:ss'
&lt;br /&gt;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE french&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'french' &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;lang
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;q.[dt]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;mnth
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;q.[dt]
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;DATETIME&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;dt
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@t
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="black"&gt;q&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE us_english&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'us_english' &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;lang
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;q.[dt]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;mnth
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;q.[dt]
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;DATETIME&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;dt
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@t
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="black"&gt;q&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are taking the value which can be described in words as “6pm on 8th December 2009”, defining it in three different ways, then seeing how the &lt;font color="#ff00ff" face="Courier New"&gt;@@LANGUAGE&lt;/font&gt; setting can affect the results. Here are those results:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_1CC2DDFC.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="french language datetime" border="0" alt="french language datetime" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_3B2D1EE5.png" width="321" height="203" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Notice how the interpretation of the month can change depending on &lt;font color="#ff00ff" face="Courier New"&gt;@@LANGUAGE&lt;/font&gt;. If &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’french’ &lt;/font&gt;then the string '2009-12-08 18:00:00' is interpreted as 12&lt;sup&gt;th&lt;/sup&gt; August 2009 (‘août’ is French for August for those that don’t know) whereas if &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’us_english’ &lt;/font&gt;it is interpreted as 8&lt;sup&gt;th&lt;/sup&gt; December 2009.&lt;/p&gt;

&lt;p&gt;Clearly this is a problem because the results of our queries have a dependency on a server-level or connection-level setting and that is NOT a good thing. Hence I recommend that you only define &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; literals in one of the two unambiguous date formats:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;yyyy-MM-dd&lt;strong&gt;&lt;u&gt;T&lt;/u&gt;&lt;/strong&gt;HH24:mi:ss &lt;/li&gt;

  &lt;li&gt;yyyyMMdd HH24:mi:ss &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;That was going to be the end of this blog post but then I found out that this behaviour changed slightly in SQL Server 2008. Take the following code (see if you can figure out what the results will be before I tell you):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER&amp;#160; DATABASE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;COMPATIBILITY_LEVEL &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;100 &lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Behave like SQL Server 2008
&lt;br /&gt;&lt;/font&gt;&lt;font color="black"&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;USE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE french&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="magenta"&gt;NCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;10&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Ambiguous date format
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[ExplicitCast]
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@dt&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[MonthFromImplicitCast]
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;)) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[MonthFromExplicitCast]&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we are doing three different things with our nchar literal:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;explicitly cast it as a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;extract the month name from the char literal using the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function (which results in an under-the-covers implicit cast) &lt;/li&gt;

  &lt;li&gt;extract the month name from the char literal using the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function after it has been explicitly casted as a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the compatibility level is set to SQL Server 2008 and &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’french’&lt;/font&gt;. Here are the results:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_79B26C8B.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_519FBD6C.png" width="431" height="87" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Were you correct?)&lt;/p&gt;

&lt;p&gt;Let’s take a look at what is happening here. The behaviour when we are explicitly casting as &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; hasn’t changed, our nchar literal is still getting interpreted as 12th August rather than 8th December when &lt;font face="Courier New"&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;&lt;/font&gt;=’french’&lt;/font&gt;. The &lt;font face="Courier New"&gt;[MonthFromExplicitCast]&lt;/font&gt; field is interesting though, it seems as though the implicit cast has resulted in the desired value of 8th December. Why is that?&lt;/p&gt;

&lt;p&gt;To get the answer we can turn to &lt;a href="http://msdn.microsoft.com/en-us/library/ms174395.aspx"&gt;BOL’s description&lt;/a&gt; of the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function syntax:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_6C6BE378.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_6B937D8E.png" width="244" height="85" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The implicit cast is not casting to &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; at all, it is actually casting to [date] which is a new datatype in SQL Server 2008. The new date-related datatypes in SQL Server 2008 (i.e. &lt;font face="Courier New"&gt;[date], [datetime2], [time], [datetimeoffset]&lt;/font&gt;) dis&lt;em&gt;regard&lt;/em&gt;&amp;#160;&lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;&lt;/font&gt; and hence we get behaviour that is more predictable and, frankly, better.&lt;/p&gt;

&lt;p&gt;These new behaviours for SQL Server 2008 were unknown to me when I began this blog post so I have learnt something in the course of authoring it, I hope it has helped you too. No doubt someone somewhere is going to get nastily burnt by this at some point, make sure that it isn’t you by always using unambiguous date formats:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;yyyy-MM-ddTHH24:mi:ss &lt;/li&gt;

  &lt;li&gt;yyyyMMdd HH24:mi:ss &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;regardless of which version you are on!&lt;/p&gt;

&lt;p&gt;Don’t forget to check out other &lt;a href="http://sqlblog.com/tags/T-SQL+Tuesday/default.aspx"&gt;T-SQL Tuesday&lt;/a&gt; blog posts, see Adam Machanic’s blog post &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/11/30/invitation-to-participate-in-t-sql-tuesday-001-date-time-tricks.aspx"&gt;Invitation to Participate in T-SQL Tuesday #001: Date/Time Tricks&lt;/a&gt; for details!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/jamiet"&gt;@Jamiet&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>