<?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>A Developer&#039;s Blog &#187; collation</title>
	<atom:link href="http://blog.hoegaerden.be/tag/collation/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hoegaerden.be</link>
	<description>SQL Server, BI, .NET, IT and anything else I have been playing with.</description>
	<lastBuildDate>Wed, 01 Sep 2010 06:27:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fun With Strings</title>
		<link>http://blog.hoegaerden.be/2009/10/04/fun-with-strings/</link>
		<comments>http://blog.hoegaerden.be/2009/10/04/fun-with-strings/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 11:07:48 +0000</pubDate>
		<dc:creator>Valentino Vranken</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[collation]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[Integration Services]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://blog.hoegaerden.be/2009/10/04/fun-with-strings/</guid>
		<description><![CDATA[Initially I was going to call this article &#8220;Struggling With Collation: The SeQueL&#8221;, but it just doesn&#8217;t have the same ring to it as &#8220;Fun With Strings&#8221;.  In that previous article I showed how you might get different results when loading data from a temporary table or table variable and I suggested that one way [...]]]></description>
			<content:encoded><![CDATA[<p>Initially I was going to call this article &#8220;<a title="Struggling With Collation" href="http://blog.hoegaerden.be/2009/09/20/struggling-with-collation/" target="_blank">Struggling With Collation</a>: The SeQueL&#8221;, but it just doesn&#8217;t have the same ring to it as &#8220;Fun With Strings&#8221;.  In that previous article I showed how you might get different results when loading data from a temporary table or table variable and I suggested that one way of solving this is by switching your data type to nvarchar.</p>
<h2>Unicode Or Not?</h2>
<h3>Reason #1 For Not</h3>
<p>Today I&#8217;m going to show you that nvarchar is not always what we want to use, especially if we don&#8217;t need to support Unicode strings.  Imagine a staging scenario when loading a data warehouse.  Often the Business Keys (BK) are strings, and depending on the source system, sometimes very long strings &#8211; I&#8217;ve seen situations with a combined business key of over 500 bytes!  (You can’t imagine what some data sources look like but that’s another story.)  Do we really want to convert these to Unicode, and thus double their size?  Furthermore, to improve lookups we put indexes on those BKs.  These indexes would double in size as well.  So no, we don&#8217;t really want to make these fields Unicode, and certainly not when we want our ETLs to perform as fast as possible.</p>
<h3>Reason #2 For Not</h3>
<p>That was reason number one why nvarchar is not always the solution.  And here comes reason number two.  In my scenario, the source tables are located in an Oracle database.  And guess what: by default Oracle&#8217;s ORDER BY behaves different than SQL Server&#8217;s ORDER BY (when using the regular Latin1_General_CI_AS or SQL_Latin1_General_CP1_CI_AS collations)!  By default Oracle uses binary string comparison to sort its data and the reason for it appears to be that that&#8217;s the only way to prevent a full table scan.  I&#8217;m no Oracle expert but that&#8217;s <a title="NLS_SORT specifies the collating sequence for ORDER BY queries." href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/initparams130.htm" target="_blank">what the documentation states</a>.</p>
<p>Here’s a little demonstration.  The following script prepares a table variable and selects the data from it, sorted ascending.</p>
<div class="code"><span style="color: #0000ff;">declare</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">table</span><span style="color: #000000;">(</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">varchar</span><span style="color: #000000;">(</span><span style="color: #800000;">20</span><span style="color: #000000;">))</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;AA&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A-&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A A&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;BA&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;0&#8242;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;1&#8242;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-0&#8242;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-1&#8242;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A0&#8242;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;0A&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-A&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-B&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;a&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;b&#8217;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">insert</span><span style="color: #808080;"> </span><span style="color: #0000ff;">into</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216; &#8216;</span><span style="color: #0000ff;">;</span><span style="color: #808080;"> </span></p>
<p><span style="color: #808080;"> </span><span style="color: #0000ff;">select</span><span style="color: #808080;"> </span><span style="color: #000000;">*</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">order</span><span style="color: #808080;"> </span><span style="color: #0000ff;">by</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">asc;</span></div>
<p>I have executed it once just as stated above (while connected to a database that uses the SQL_Latin1_General_CP1_CI_AS collation) and once more while using nvarchar as data type for the column in the table variable.  The first execution will sort the data using a non-Unicode sorting algorithm, while the second execution will order the data according to the Unicode sorting method.  The results will be shown further below for easier comparison.</p>
<p>On Oracle I performed a similar procedure, as shown in following script.</p>
<div class="code"><span style="color: #0000ff;">select</span><span style="color: #808080;"> </span><span style="color: #000000;">cast(</span><span style="color: #808080;">&#8216;AA&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #0000ff;">varchar</span><span style="color: #000000;">(</span><span style="color: #800000;">20</span><span style="color: #000000;">))</span><span style="color: #808080;"> </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A-&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A A&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;BA&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;0&#8242; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;1&#8242; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-0&#8242; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-1&#8242; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;A0&#8242; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;0A&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-A&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;-B&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;a&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216;b&#8217; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"> </span><span style="color: #0000ff;">union</span></div>
<div class="code"><span style="color: #0000ff;">select</span><span style="color: #808080;"> &#8216; &#8216; </span><span style="color: #0000ff;">as</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">Dual</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">order</span><span style="color: #808080;"> </span><span style="color: #0000ff;">by</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">asc;</span></div>
<p>The Oracle script doesn’t use a table variable, it just creates a result set using several select statements with a union in between.  But for our test that doesn’t matter, the results using this method are suitable.</p>
<p>In the table below you can see the result of the three executions.</p>
<table style="width: 272pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0" width="362">
<colgroup>
<col style="width: 90pt;" width="120"></col>
<col style="width: 68pt;" width="90"></col>
<col style="width: 67pt;" width="89"></col>
<col style="width: 47pt;" width="63"></col>
</colgroup>
<tbody>
<tr style="height: 15pt;" height="20">
<td class="xl65" style="background: #4f81bd none repeat scroll 0% 0%; width: 90pt; font-family: calibri; height: 15pt; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium 0.5pt 1.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" width="120" height="20">SQL non-Unicode</td>
<td class="xl65" style="background: #4f81bd none repeat scroll 0% 0%; width: 68pt; font-family: calibri; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium 0.5pt 1.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" width="90">SQL Unicode</td>
<td class="xl65" style="background: #4f81bd none repeat scroll 0% 0%; width: 67pt; font-family: calibri; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium 0.5pt 1.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" width="89">Oracle Binary</td>
<td style="background: #4f81bd none repeat scroll 0% 0%; width: 47pt; font-family: calibri; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium medium 1.5pt none none solid -moz-use-text-color -moz-use-text-color white;" width="63"><span style="mso-spacerun: yes"> </span></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20"><span style="mso-spacerun: yes"> </span></td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"><span style="mso-spacerun: yes"> </span></td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"><span style="mso-spacerun: yes"> </span></td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">&lt; space</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">-0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">-0</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">-1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-1</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">-A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">0A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">-A</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">-B</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-B</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">-1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">0</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">0A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">0A</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">a</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">1</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">a</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A-</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A A</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">A A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A-</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">A-</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A0</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">A0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">AA</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">AA</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">AA</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">b</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">BA</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" height="20">b</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-B</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">a</td>
<td style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: medium none; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl67" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt medium medium none solid none none -moz-use-text-color white -moz-use-text-color -moz-use-text-color;" height="20">BA</td>
<td class="xl66" style="border-bottom: medium none; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">BA</td>
<td class="xl66" style="border-bottom: medium none; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">b</td>
<td style="font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none"></td>
</tr>
</tbody>
</table>
<p>As you can see, they only agree on one thing: space really is the smallest character in my test set!  And that’s not what I want, I want all the data to be sorted consistently, no matter what the source is.</p>
<h2>But Why Sorted?</h2>
<p>You may wonder why I need to sort the data.  Well, some components in Integration Services expect the incoming data flows to be ordered.  One of the standard components that requires this is the <a title="BOL 2008 - Merge Transformation" href="http://msdn.microsoft.com/en-us/library/ms141703.aspx" target="_blank">Merge Transformation</a>.  Another (custom!) component is <a title="TableDifference - a custom SSIS component" href="http://www.sqlbi.eu/Projects/TableDifference/tabid/74/language/en-US/Default.aspx" target="_blank">Table Difference</a>.  I could of course add a Sort Transformation to my Data Flow, but that would not be interesting for performance.  I want the data to come from the database server in the expected order.  So now I&#8217;ll show you how you can do that.</p>
<h2>Taking Control!</h2>
<h3>SQL Server: ORDER BY … COLLATE …</h3>
<p>On SQL Server this was fairly easy.  The <a title="BOL 2008 - ORDER BY Clause (Transact-SQL)" href="http://msdn.microsoft.com/en-us/library/ms188385.aspx" target="_blank">ORDER BY clause</a> has a COLLATE part where you can specify what collation should be used to order the data.  Because Oracle sorts its data using a binary algorithm, I’ll tell SQL Server to do that as well.  More precisely I’ll tell SQL Server to use the Latin1_General_BIN collation.  The updated SELECT statement from the T-SQL script above looks like this:</p>
<div class="code"><span style="color: #0000ff;">select</span><span style="color: #808080;"> </span><span style="color: #000000;">*</span><span style="color: #808080;"> </span><span style="color: #0000ff;">from</span><span style="color: #808080;"> </span><span style="color: #000000;">@tbl</span><span style="color: #808080;"><br />
</span><span style="color: #0000ff;">order</span><span style="color: #808080;"> </span><span style="color: #0000ff;">by</span><span style="color: #808080;"> </span><span style="color: #000000;">col1</span><span style="color: #808080;"> </span><span style="color: #0000ff;">collate</span><span style="color: #808080;"> </span><span style="color: #000000;">Latin1_General_BIN</span><span style="color: #808080;"> </span><span style="color: #0000ff;">asc;</span></div>
<h3>Oracle: ORDER BY NLSSORT()</h3>
<p>To ensure that results from Oracle are always returned using the same sorting algorithm, I will also tell the Oracle server to sort it’s data using the binary algorithm.</p>
<p>The first way I came up with was to change the <a title="NLS_SORT" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/initparams130.htm" target="_blank">NLS_SORT</a> setting on the session.  That can be done by executing the following command before the SELECT statement:</p>
<div class="code"><span style="color: #0000ff;">ALTER</span><span style="color: #808080;"> </span><span style="color: #000000;">SESSION</span><span style="color: #808080;"> </span><span style="color: #0000ff;">SET</span><span style="color: #808080;"> </span><span style="color: #000000;">NLS_SORT</span><span style="color: #0000ff;">=BINARY;</span></div>
<p>This method is fine when you’re running the queries manually from a client such as Oracle SQL Developer.  However, in SSIS the OLE DB Source component will not accept anything else besides the SELECT statement.</p>
<p>Then I found another way.  There’s a function called <a title="NLSSORT function" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions099.htm" target="_blank">NLSSORT()</a> which you can apply to a column in the ORDER BY clause.  The following statement demonstrates how to use this function.  (I only show the ORDER BY clause as it can be applied to the Oracle script mentioned earlier.)</p>
<div class="code"><span style="color: #0000ff;">ORDER</span><span style="color: #808080;"> </span><span style="color: #0000ff;">BY</span><span style="color: #808080;"> </span><span style="color: #000000;">NLSSORT(col1,</span><span style="color: #808080;"> &#8216;NLS_SORT=BINARY&#8217;</span><span style="color: #000000;">)</span></div>
<p>The following table shows the results from both binary sort queries:</p>
<table style="width: 135pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0" width="179">
<colgroup>
<col style="width: 68pt;" width="90"></col>
<col style="width: 67pt;" width="89"></col>
</colgroup>
<tbody>
<tr style="height: 15pt;" height="20">
<td class="xl65" style="background: #4f81bd none repeat scroll 0% 0%; width: 68pt; font-family: calibri; height: 15pt; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium 0.5pt 1.5pt none solid solid -moz-use-text-color white white;" width="90" height="20">SQL Binary</td>
<td class="xl65" style="background: #4f81bd none repeat scroll 0% 0%; width: 67pt; font-family: calibri; color: white; font-size: 11pt; font-weight: 700; text-decoration: none; border: medium 0.5pt 1.5pt medium none solid solid none -moz-use-text-color white white -moz-use-text-color;" width="89">Oracle Binary</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20"><span style="mso-spacerun: yes"> </span></td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none"><span style="mso-spacerun: yes"> </span></td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">-0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">-0</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">-1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-1</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">-A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">-A</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">-B</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">-B</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">0</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">0A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">0A</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">1</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">1</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">A A</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A A</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">A-</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">A-</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">A0</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">A0</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">AA</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">AA</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">BA</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">BA</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #b8cce4 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt 0.5pt none solid solid -moz-use-text-color white white;" height="20">a</td>
<td class="xl66" style="border-bottom: white 0.5pt solid; border-left: medium none; font-family: calibri; background: #b8cce4; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #b8cce4 none">a</td>
</tr>
<tr style="height: 15pt;" height="20">
<td class="xl66" style="background: #dbe5f1 none repeat scroll 0% 0%; font-family: calibri; height: 15pt; color: black; font-size: 11pt; font-weight: 400; text-decoration: none; border: medium 0.5pt none solid -moz-use-text-color white;" height="20">b</td>
<td class="xl66" style="border-bottom: medium none; border-left: medium none; font-family: calibri; background: #dbe5f1; color: black; font-size: 11pt; border-top: medium none; font-weight: 400; border-right: white 0.5pt solid; text-decoration: none; text-underline-style: none; text-line-through: none; mso-pattern: #dbe5f1 none">b</td>
</tr>
</tbody>
</table>
<p>Finally I am able to get data from both Oracle and SQL Server using a consistent sort order.</p>
<h3>But, How Big Is NULL?</h3>
<p>However, even on this straightforward request, both database servers do not fully agree!  Here’s what they have to say about the topic:</p>
<p><a title="BOL 2008 - Null Values" href="http://msdn.microsoft.com/en-us/library/ms191504.aspx" target="_blank">“NULL</a> is the smallest.”</p>
<p>“No, it’s the largest.”</p>
<p>“No, smallest!”</p>
<p>“Largest!!”</p>
<p>“Smallest.”</p>
<p>“Largest I tell you!!!”</p>
<p>“Bladiebla, not hearing you, anyway, it’s NOTHING!”</p>
<p>“No, it isn’t!”</p>
<p><em>*discussion goes on and on*</em></p>
<p>If I add NULL to my test data set, SQL Server will sort it first (thus NULL is the smallest value in my test set), while Oracle will put it last.  In my situation it wasn’t really an issue (the BKs are not supposed to be NULL), but it’s quite important to remember in cases where NULLs are actually possible.</p>
<h2>Conclusion</h2>
<p>When working with strings, always keep collation in mind.  And even more so when dealing with several different source systems!</p>
<p><strong>Additional reference material:</strong></p>
<p><a title="The Globalization of Language in Oracle - The NLS_COMP and NLS_SORT variables" href="http://www.databasejournal.com/features/oracle/article.php/3488751/The-Globalization-of-Language-in-Oracle---The-NLSCOMP-and-NLSSORT-variables.htm" target="_blank">Database Journal: The Globalization of Language in Oracle &#8211; The NLS_COMP and NLS_SORT variables</a></p>
<p><a title="How to: Sort Data for the Merge and Merge Join Transformations" href="http://msdn.microsoft.com/en-us/library/ms137653.aspx" target="_blank">BOL 2008: How to: Sort Data for the Merge and Merge Join Transformations</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hoegaerden.be/2009/10/04/fun-with-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Struggling With Collation</title>
		<link>http://blog.hoegaerden.be/2009/09/20/struggling-with-collation/</link>
		<comments>http://blog.hoegaerden.be/2009/09/20/struggling-with-collation/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 09:46:51 +0000</pubDate>
		<dc:creator>Valentino Vranken</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[collation]]></category>
		<category><![CDATA[data]]></category>

		<guid isPermaLink="false">http://blog.hoegaerden.be/2009/09/20/struggling-with-collation/</guid>
		<description><![CDATA[Recently I was investigating an issue related to ordering data.&#160; As a test, I ran the following script:

&#8211; Sorting data from a temporary table&#160; declare&#160;@tbl&#160;table&#160;(&#160;ProductNumber&#160;varchar(25)&#160;);&#160; insert&#160;into&#160;@tbl&#160;select &#8216;BBBB&#8217;;&#160; insert&#160;into&#160;@tbl&#160;select &#8216;AAAA&#8217;;&#160; insert&#160;into&#160;@tbl&#160;select &#8216;A-B&#8217;;&#160; insert&#160;into&#160;@tbl&#160;select &#8216;A123&#8242;;&#160; select&#160;*&#160;from&#160;@tbl&#160;order&#160;by&#160;ProductNumber&#160;asc;      
&#8211; Sorting data from a table variable&#160; create&#160;table&#160;#tbl&#160;(&#160;ProductNumber&#160;varchar(25)&#160;);&#160; insert&#160;into&#160;#tbl&#160;select &#8216;BBBB&#8217;;&#160; insert&#160;into&#160;#tbl&#160;select &#8216;AAAA&#8217;;&#160; insert&#160;into&#160;#tbl&#160;select &#8216;A-B&#8217;;&#160; insert&#160;into&#160;#tbl&#160;select &#8216;A123&#8242;;&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was investigating an <strong>issue related to ordering data</strong>.&#160; As a test, I ran the following script:</p>
<p><span style="color: #0000ff"></span></p>
<div class="code"><font color="#006400">&#8211; Sorting data from a temporary table&#160; <br /></font><font color="#0000ff">declare</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#000000">(</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">varchar</font><font color="#000000">(</font><font color="#800000">25</font><font color="#000000">)</font><font color="#808080">&#160;</font><font color="#000000">)</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;BBBB&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;AAAA&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A-B&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A123&#8242;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">select</font><font color="#808080">&#160;</font><font color="#000000">*</font><font color="#808080">&#160;</font><font color="#0000ff">from</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">order</font><font color="#808080">&#160;</font><font color="#0000ff">by</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">asc;</font><font color="#808080">      </p>
<p></font><font color="#006400">&#8211; Sorting data from a table variable&#160; <br /></font><font color="#0000ff">create</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#000000">(</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">varchar</font><font color="#000000">(</font><font color="#800000">25</font><font color="#000000">)</font><font color="#808080">&#160;</font><font color="#000000">)</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;BBBB&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;AAAA&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A-B&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A123&#8242;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">select</font><font color="#808080">&#160;</font><font color="#000000">*</font><font color="#808080">&#160;</font><font color="#0000ff">from</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">order</font><font color="#808080">&#160;</font><font color="#0000ff">by</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">asc;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">drop</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#0000ff">;</font> </div>
<p>&#160;</p>
<p>And it gave me this result:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Results from script" border="0" alt="Results from script" src="http://blog.hoegaerden.be/wp-content/uploads/image72.png" width="133" height="235" /></p>
<p>As you can see, the order of the data coming from a <strong>temporary table</strong> is different than the data from a <strong>table variable</strong>, even though the same data type is used.&#160; At first I thought, how on earth is this possible?&#160; After spending some time pondering about this problem (and after a colleague reported that when he executed the above script, the result was as expected), it came to me.&#160; Don’t tell me it’s a <a title="BOL 2008 - Working with Collations" href="http://msdn.microsoft.com/en-us/library/ms187582.aspx" target="_blank">collation</a> problem?!&#160; Well, it is.&#160; (In case you don’t know collation, in short “Collations specify the rules for how strings of character data are sorted and compared”.&#160; More info through that previous link and <a title="BOL 2000 - SQL Server Collation Fundamentals" href="http://msdn.microsoft.com/en-us/library/aa174903%28SQL.80%29.aspx" target="_blank">here</a>.)</p>
<p>When I executed the script, my Management Studio session was connected to AdventureWorks2008, one of my test databases.&#160; When I switched to <a title="BOL 2008 - tempdb Database" href="http://msdn.microsoft.com/en-us/library/ms190768.aspx" target="_blank">tempdb</a>, the result was normal – both queries returned the same result.&#160; Then I had a look at the collations.&#160; My tempdb is using Latin1_General_CI_AS while the AdventureWorks2008 database is apparently using SQL_Latin1_General_CP1_CI_AS.</p>
<p>A <a title="BOL 2008 - Special Table Types" href="http://msdn.microsoft.com/en-us/library/ms186986.aspx" target="_blank">temporary table</a> is created in the tempdb and thus uses the collation of the tempdb for its string columns.&#160; And a <a title="BOL 2008 - DECLARE @local_variable (Transact-SQL)" href="http://msdn.microsoft.com/en-us/library/ms188927.aspx" target="_blank">table variable</a> uses the same collation as the database to which the session is connected.&#160; Which is quite logical because otherwise you would get collation conflicts when using the table variable in combination with a table from the active database in the same query.&#160; I will demonstrate that with the following script:</p>
<div class="code"><span style="color: #0000ff">declare</span><span style="color: #808080"> </span><span style="color: #000000">@tbl</span><span style="color: #808080"> </span><span style="color: #0000ff">table</span><span style="color: #808080"> </span><span style="color: #000000">(</span><span style="color: #808080"> </span><span style="color: #000000">ProductNumber</span><span style="color: #808080"> </span><span style="color: #0000ff">varchar</span><span style="color: #000000">(</span><span style="color: #800000">25</span><span style="color: #000000">)</span><span style="color: #808080"> </span><span style="color: #000000">)</span><span style="color: #0000ff">;</span><span style="color: #808080">      <br /></span><span style="color: #0000ff">insert</span><span style="color: #808080"> </span><span style="color: #0000ff">into</span><span style="color: #808080"> </span><span style="color: #000000">@tbl</span><span style="color: #808080"> </span><span style="color: #0000ff">select</span><span style="color: #808080"> &#8216;BBBB&#8217;</span><span style="color: #0000ff">;</span><span style="color: #808080">      <br /></span><span style="color: #0000ff">select</span><span style="color: #808080"> </span><span style="color: #000000">*</span><span style="color: #808080"> </span><span style="color: #0000ff">from</span><span style="color: #808080"> </span><span style="color: #000000">AdventureWorks.Production.Product</span><span style="color: #808080"> </span><span style="color: #000000">P</span><span style="color: #808080">      <br /></span><span style="color: #0000ff">where</span><span style="color: #808080"> </span><span style="color: #000000">P.ProductNumber</span><span style="color: #808080"> </span><span style="color: #0000ff">in</span><span style="color: #808080"> </span><span style="color: #000000">(</span><span style="color: #0000ff">select</span><span style="color: #808080"> </span><span style="color: #000000">ProductNumber</span><span style="color: #808080"> </span><span style="color: #0000ff">from</span><span style="color: #808080"> </span><span style="color: #000000">@tbl)</span><span style="color: #0000ff">;</span></div>
<p>&#160;</p>
<p>On my (SQL Server 2008) server I also have the old SQL2005 AdventureWorks database up and running, which is using the Latin1_General_CI_AS collation.&#160; Executing the above script (which references that AdventureWorks DB) while being connected to AdventureWorks2008 (or any other DB that uses a different collation from Latin1_General_CI_AS) results in the following error:</p>
<blockquote><p><span style="color: #ff0000">Msg 468, Level 16, State 9, Line 3        <br />Cannot resolve the collation conflict between &quot;SQL_Latin1_General_CP1_CI_AS&quot; and &quot;Latin1_General_CI_AS&quot; in the equal to operation.</span></p>
</blockquote>
<p>Changing the active database to any other DB that uses the Latin1_General_CI_AS collation and then executing the query results in a positive execution.</p>
<p>Now, to get back to the initial issue of sorting inconsistency, even though the collations are not exactly the same, they are both Latin1, Case Insensitive, Accent Sensitive and still they don’t sort the data in the same way??&#160; For an explanation on that I found the following page on the Microsoft Support site: <a title="Comparing SQL collations to Windows collations" href="http://support.microsoft.com/kb/322112" target="_blank">Comparing SQL collations to Windows collations</a>.&#160; In short: a Windows collation (such as Latin1_General_CI_AS) uses a different comparison algorithm than a SQL collation (SQL_Latin1_General_CP1_CI_AS).&#160; A Windows collation uses the same algorithm as for Unicode data, even when the data is non-Unicode.</p>
<p>All that means is that in our test example, in order to get the data in the right order all the time, we could switch to Unicode fields:</p>
<p class="code"><span style="color: #0000ff"></span></p>
<div class="code"><font color="#006400">&#8211; Sorting data from a temporary table&#160; <br /></font><font color="#0000ff">declare</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#000000">(</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">nvarchar</font><font color="#000000">(</font><font color="#800000">25</font><font color="#000000">)</font><font color="#808080">&#160;</font><font color="#000000">)</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;BBBB&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;AAAA&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A-B&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A123&#8242;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">select</font><font color="#808080">&#160;</font><font color="#000000">*</font><font color="#808080">&#160;</font><font color="#0000ff">from</font><font color="#808080">&#160;</font><font color="#000000">@tbl</font><font color="#808080">&#160;</font><font color="#0000ff">order</font><font color="#808080">&#160;</font><font color="#0000ff">by</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">asc;</font><font color="#808080">     </p>
<p></font><font color="#006400">&#8211; Sorting data from a table variable&#160; <br /></font><font color="#0000ff">create</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#000000">(</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">nvarchar</font><font color="#000000">(</font><font color="#800000">25</font><font color="#000000">)</font><font color="#808080">&#160;</font><font color="#000000">)</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;BBBB&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;AAAA&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A-B&#8217;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">insert</font><font color="#808080">&#160;</font><font color="#0000ff">into</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">select</font><font color="#808080"> &#8216;A123&#8242;</font><font color="#0000ff">;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">select</font><font color="#808080">&#160;</font><font color="#000000">*</font><font color="#808080">&#160;</font><font color="#0000ff">from</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#808080">&#160;</font><font color="#0000ff">order</font><font color="#808080">&#160;</font><font color="#0000ff">by</font><font color="#808080">&#160;</font><font color="#000000">ProductNumber</font><font color="#808080">&#160;</font><font color="#0000ff">asc;</font><font color="#808080">&#160; <br /></font><font color="#0000ff">drop</font><font color="#808080">&#160;</font><font color="#0000ff">table</font><font color="#808080">&#160;</font><font color="#0000ff">#</font><font color="#000000">tbl</font><font color="#0000ff">;</font> </div>
<p>&#160;</p>
<p>Executing that returns the following result, no matter what the active database is:</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Script result when using Unicode fields" border="0" alt="Script result when using Unicode fields" src="http://blog.hoegaerden.be/wp-content/uploads/image73.png" width="135" height="231" /></p>
<p>And it also means that you need to be careful when retrieving data from different sources (which is how I came across the issue in the first place).&#160; If you need to have your data sorted in a particular, consistent way and you’re doing that by using an ORDER BY in a SELECT statement on the source system, double-check if all sources are using the same collation!</p>
<p>In case the above left you wondering about <strong>what collation to use</strong>: the SQL collations are there for backward compatibility.&#160; For new developments use the Windows collations!</p>
<p>&#160;</p>
<p>See my <a title="Fun With Strings" href="http://blog.hoegaerden.be/2009/10/04/fun-with-strings/" target="_blank">Fun With Strings</a> article for another story related to collation.</p>
<p>&#160;</p>
<p><strong>Additional reference material:</strong></p>
<p><a title="Comparing Table Variables with Temporary Tables" href="http://www.sqlservercentral.com/articles/Temporary+Tables/66720/" target="_blank">SQL Server Central &#8211; Comparing Table Variables with Temporary Tables</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hoegaerden.be/2009/09/20/struggling-with-collation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
