<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.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>Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx</link><description>UPDATED 11:11p 7 September 2009: Based on my latest exchange with Adam (see the comments for this post), compound operators should be avoided for string concatenation due to the ORDER BY bug discused in KB 287515 . The XML variant below is the preferred</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3338517</link><pubDate>Wed, 16 Jun 2010 10:53:13 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3338517</guid><dc:creator>Claudio Cauchi</dc:creator><description>&lt;p&gt;Answering my own question: [Promise I had googled this a zillion times]&lt;/p&gt;
&lt;p&gt;from: &lt;a rel="nofollow" target="_new" href="http://sqlblog.com/blogs/rob_farley/archive/2010/04/15/handling-special-characters-with-for-xml-path.aspx"&gt;sqlblog.com/.../handling-special-characters-with-for-xml-path.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;select&lt;/p&gt;
&lt;p&gt; stuff(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;(select &amp;#39;, &amp;lt;&amp;#39; + name + &amp;#39;&amp;gt;&amp;#39;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;from sys.databases&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;where database_id &amp;gt; 4&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;order by name&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;for xml path(&amp;#39;&amp;#39;), type&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;).value(&amp;#39;(./text())[1]&amp;#39;,&amp;#39;varchar(max)&amp;#39;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;, 1, 2, &amp;#39;&amp;#39;) as namelist;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3338517" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3338513</link><pubDate>Wed, 16 Jun 2010 10:27:35 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3338513</guid><dc:creator>Claudio Cauchi</dc:creator><description>&lt;p&gt;I know I might be resuscitating an old post however I would like to pose a problem I find to you guys. I prefer the FOR XML approach when concatenating strings. However my data sometimes contains non-xml friendly characters like &amp;amp;, &amp;gt; and &amp;lt; which are translated to &amp;amp; etc. Is there a neat way to avoid this from happening? &amp;nbsp;I would like to avoid the REPLACE (result,&amp;#39;&amp;amp;&amp;#39;,&amp;#39;&amp;amp;&amp;#39;) as there are multiple &amp;quot;special&amp;quot; characters.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3338513" width="1" height="1"&gt;</description></item><item><title>http://blog.sqlauthority.com/2009/09/20/sql-server-execution-plan-and-results-of-aggregate-concatenation-queries-depend-upon-expression-location/</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3283648</link><pubDate>Tue, 29 Sep 2009 03:32:44 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3283648</guid><dc:creator>TrackBack</dc:creator><description>&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3283648" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279669</link><pubDate>Tue, 08 Sep 2009 06:09:39 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279669</guid><dc:creator>WardPond</dc:creator><description>&lt;p&gt;@Adam: you've obviously got way more context around the history of this issue that I do. &amp;nbsp;Under the circimstances, I agrree that the XML variant is unambiguously preferable for ordered concatenation.&lt;/p&gt;
&lt;p&gt;Now.. &amp;nbsp;how 'bout them row constructors? :)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; -wp&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279669" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279662</link><pubDate>Tue, 08 Sep 2009 04:27:05 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279662</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Ward,&lt;/p&gt;
&lt;p&gt;I have no issue w/ compound operators--I think they're a useful addition to T-SQL and see no reason to avoid them (unless you have to write code that has any chance of being back-ported to 2005, etc). The aggregate concatenation trick is, in my opinion, not something that should be used unless the QP team decides to modify the query engine to fully and deterministically support it. The issue, as far as I know, is not limited to cases where there is a function used in the ORDER BY--that's just an easy way to show the problem. There is simply no guarantee that it will work. &lt;/p&gt;
&lt;p&gt;And will the issue indeed be fixed eventually? I don't know, but I certainly don't see that on the horizon; this &amp;quot;feature&amp;quot; has been heavily discussed since the SQL Server 7.0 time frame. I've been an MVP since 2004 and have seen numerous discussions on the topic since that time in the private MVP newsgroup--many of which included members of the QO and QP teams--and never has there been any indication that anyone was planning to fix the issue. I'm sure there were many discussions before I showed up. So this is something they've known about for 10+ years and four major versions. I believe that the primary argument against a fix is that it's not a &amp;quot;fix&amp;quot; at all since this behavior is undocumented. So there's no bug; a &amp;quot;fix&amp;quot; would actually be an enhancement. And with the supported and much more flexible FOR XML PATH workaround, plus the ability to do aggregate concatenation using SQLCLR, there is no need for such an enhancement.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279662" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279660</link><pubDate>Tue, 08 Sep 2009 04:13:24 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279660</guid><dc:creator>WardPond</dc:creator><description>&lt;p&gt;@Adam: As always, thanks for your comments, no matter how you format them. &amp;nbsp;I've updated the top of the post with what I hope is an accurate synopsis of your concerns.&lt;/p&gt;
&lt;p&gt;If I'm reading the KB article correctly, the bug only manifests itself when functions are used on columns in the scope of the ORDER BY statement. &amp;nbsp;This approach wouldn't be a best practice (we couldn't use the order of an index to satisfy an ORDER BY; even an index on the necessary column would need to be scanned due to the presence of the functions), and there are several workaround (in addition to the workaround discussed in the article, an index on a calculated, persisted column matching the expression would also solve the problem if that column were used in the ORDER BY).&lt;/p&gt;
&lt;p&gt;In the context of the original post, all of this likely mitigates in favor of use of the XML variant for string concatenation, at least until the ORDER BY issue is resolved.&lt;/p&gt;
&lt;p&gt;It seems to me that compound operators are still useful for scenarios where order is immaterial (aggregates, etc., although alternate syntax certainly also exists in these cases).&lt;/p&gt;
&lt;p&gt;Do you agree, Adam, or do you stay away from compound operators altogether?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; -wp&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279660" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279658</link><pubDate>Tue, 08 Sep 2009 03:00:51 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279658</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;... and sorry for the three posts, but I just tested and this is certainly not fixed in SQL Server 2008, even with the new syntax... try the following with and without the ORDER BY (and with different ORDER BYs):&lt;/p&gt;
&lt;p&gt;DECLARE @temp table([id] int);&lt;/p&gt;
&lt;p&gt;INSERT INTO @temp VALUES(3);&lt;/p&gt;
&lt;p&gt;INSERT INTO @temp VALUES(2);&lt;/p&gt;
&lt;p&gt;INSERT INTO @temp VALUES(1);&lt;/p&gt;
&lt;p&gt;DECLARE @i varchar(50);&lt;/p&gt;
&lt;p&gt;SET @i = '';&lt;/p&gt;
&lt;p&gt;SELECT&lt;/p&gt;
&lt;p&gt;@i += convert(varchar, [id])&lt;/p&gt;
&lt;p&gt;FROM @temp&lt;/p&gt;
&lt;p&gt;ORDER BY &lt;/p&gt;
&lt;p&gt;	LTRIM(RTRIM([id]));&lt;/p&gt;
&lt;p&gt;PRINT @i;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279658" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279657</link><pubDate>Tue, 08 Sep 2009 02:56:01 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279657</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;I did the Googling for you. Found a KB article on the topic, describing the ORDER BY issue:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://support.microsoft.com/default.aspx?scid=287515"&gt;http://support.microsoft.com/default.aspx?scid=287515&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279657" width="1" height="1"&gt;</description></item><item><title>re: Database Programming: A New String Concatenation Contender for SQL Server 2008</title><link>http://blogs.technet.com/b/wardpond/archive/2009/09/07/database-programming-a-new-string-concatenation-contender-for-sql-server-2008.aspx#3279656</link><pubDate>Tue, 08 Sep 2009 02:53:43 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3279656</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Ward,&lt;/p&gt;
&lt;p&gt;Nothing really &amp;quot;new&amp;quot; here except the syntax. We call that the &amp;quot;aggregate concatenation&amp;quot; trick. Google and you'll see lots of stuff. Look especially for posts by Steve Kass. What you'll find is that the behavior is undefined and your results are not guaranteed, by the query processor, to comply to what you specified in the ORDER BY clause. &lt;/p&gt;
&lt;p&gt;The order IS, in fact, guaranteed when using FOR XML, the behavior of that operator is documented and well-defined, and in my tests they're equally fast. So, no contest if you ask me.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3279656" width="1" height="1"&gt;</description></item></channel></rss>