<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Coalesce is not the answer to string concatentation in T-SQL	</title>
	<atom:link href="https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/</link>
	<description></description>
	<lastBuildDate>Wed, 27 Jan 2010 05:24:25 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>
	<item>
		<title>
		By: Rob Farley		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-318</link>

		<dc:creator><![CDATA[Rob Farley]]></dc:creator>
		<pubDate>Wed, 27 Jan 2010 05:24:25 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-318</guid>

					<description><![CDATA[Hi Ben,

The thing is that the concatenation isn’t happening because of COALESCE, because COALESCE is just a generalised form of ISNULL.

I should update the post to include a useful way of decoding the other characters.

Rob]]></description>
			<content:encoded><![CDATA[<p>Hi Ben,</p>
<p>The thing is that the concatenation isn’t happening because of COALESCE, because COALESCE is just a generalised form of ISNULL.</p>
<p>I should update the post to include a useful way of decoding the other characters.</p>
<p>Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ben		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-317</link>

		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Wed, 27 Jan 2010 00:51:32 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-317</guid>

					<description><![CDATA[I’m used to using coalesce and I’ve run into stuff in a database. I thought I’d do some research and I’ve landed here. It seems like the primary argument against coalesce is null checking?

Anyway, stuff looks ok, except it only deals in varchar. I’ve got nvarchars, this seems like a deal breaker. Also, the manual manipulation to restore the XML escaped characters seems as though it would torpedo any performance benefit over coalesce…

Is the null check optimization a YAGNI pre-optimization, or am I missing something here? Also, is there an NVARCHAR variation to stuff?

Thanks,
Ben]]></description>
			<content:encoded><![CDATA[<p>I’m used to using coalesce and I’ve run into stuff in a database. I thought I’d do some research and I’ve landed here. It seems like the primary argument against coalesce is null checking?</p>
<p>Anyway, stuff looks ok, except it only deals in varchar. I’ve got nvarchars, this seems like a deal breaker. Also, the manual manipulation to restore the XML escaped characters seems as though it would torpedo any performance benefit over coalesce…</p>
<p>Is the null check optimization a YAGNI pre-optimization, or am I missing something here? Also, is there an NVARCHAR variation to stuff?</p>
<p>Thanks,<br />
Ben</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: robfarley		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-316</link>

		<dc:creator><![CDATA[robfarley]]></dc:creator>
		<pubDate>Wed, 04 Mar 2009 02:27:59 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-316</guid>

					<description><![CDATA[Oh, and GPS... there&#039;s still no absolute guarantee that the data will be returned in the correct order out of the table variable.

Rob]]></description>
			<content:encoded><![CDATA[<p>Oh, and GPS&#8230; there&#8217;s still no absolute guarantee that the data will be returned in the correct order out of the table variable.</p>
<p>Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: robfarley		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-315</link>

		<dc:creator><![CDATA[robfarley]]></dc:creator>
		<pubDate>Wed, 04 Mar 2009 02:27:05 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-315</guid>

					<description><![CDATA[GPS,

Better to:
select @ItemLst = &#039;&#039;

...and then not use COALESCE, or maybe use it on the new ItemName.

Like:

SELECT @ItemLst = @ItemLst + ISNULL(&#039;,&#039; + ItemName,&#039;&#039;)

And then use STUFF to remove the leading comma like SELECT @ItemLst = STUFF(@ItemLst,1,1,&#039;&#039;)

Otherwise you&#039;re testing for NULL way too often.

But this is still a lot of work compared to using FOR XML PATH(&#039;&#039;)

Rob
]]></description>
			<content:encoded><![CDATA[<p>GPS,</p>
<p>Better to:<br />
select @ItemLst = &#8221;</p>
<p>&#8230;and then not use COALESCE, or maybe use it on the new ItemName.</p>
<p>Like:</p>
<p>SELECT @ItemLst = @ItemLst + ISNULL(&#8216;,&#8217; + ItemName,&#8221;)</p>
<p>And then use STUFF to remove the leading comma like SELECT @ItemLst = STUFF(@ItemLst,1,1,&#8221;)</p>
<p>Otherwise you&#8217;re testing for NULL way too often.</p>
<p>But this is still a lot of work compared to using FOR XML PATH(&#8221;)</p>
<p>Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: GPS		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-314</link>

		<dc:creator><![CDATA[GPS]]></dc:creator>
		<pubDate>Tue, 03 Mar 2009 22:40:57 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-314</guid>

					<description><![CDATA[For those of use with DBAs that will not allow the use of XMLDATA or any other XML option within the database (I&#039;m one of them obviously), if you need to have an order for the list, use a table variable to store the ordered list, then make list from the table variable.  Example:

declare @stuff table
(
     RowId int identity(1,1), 
     ItemName varchar(30)
)
DECLARE @ItemLst varchar(2000)
select @ItemLst = NULL  -- hate using set

insert into @stuff(ItemName)
select ItemName
from Item
order by ItemName

SELECT @ItemLst = COALESCE(@ItemLst + &#039;,&#039;,&#039;&#039;) + ItemName
FROM @stuff

As I said, I&#039;m not allowed to use any XML options in the database, otherwise I&#039;d have used those options.  

Have fun.  

Bad wine is for driving away bad relations - French Proverb, &quot;I drank what?&quot;

GPS 
(yes these are my real initials...)]]></description>
			<content:encoded><![CDATA[<p>For those of use with DBAs that will not allow the use of XMLDATA or any other XML option within the database (I&#8217;m one of them obviously), if you need to have an order for the list, use a table variable to store the ordered list, then make list from the table variable.  Example:</p>
<p>declare @stuff table<br />
(<br />
     RowId int identity(1,1),<br />
     ItemName varchar(30)<br />
)<br />
DECLARE @ItemLst varchar(2000)<br />
select @ItemLst = NULL  &#8212; hate using set</p>
<p>insert into @stuff(ItemName)<br />
select ItemName<br />
from Item<br />
order by ItemName</p>
<p>SELECT @ItemLst = COALESCE(@ItemLst + &#8216;,&#8217;,&#8221;) + ItemName<br />
FROM @stuff</p>
<p>As I said, I&#8217;m not allowed to use any XML options in the database, otherwise I&#8217;d have used those options.  </p>
<p>Have fun.  </p>
<p>Bad wine is for driving away bad relations &#8211; French Proverb, &#8220;I drank what?&#8221;</p>
<p>GPS<br />
(yes these are my real initials&#8230;)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: robfarley		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-313</link>

		<dc:creator><![CDATA[robfarley]]></dc:creator>
		<pubDate>Wed, 27 Aug 2008 18:44:43 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-313</guid>

					<description><![CDATA[I&#039;m sure it&#039;s a typo. The only mention of FOR XML being deprecated is at http://msdn.microsoft.com/en-us/library/ms143729.aspx, where it&#039;s clearly just the XMLDATA option. It reads very much as if it&#039;s just been edited poorly for the FOR XML page.

Hopefully I will have some more information for you about this later.

Rob]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure it&#8217;s a typo. The only mention of FOR XML being deprecated is at <a href="http://msdn.microsoft.com/en-us/library/ms143729.aspx" rel="nofollow ugc">http://msdn.microsoft.com/en-us/library/ms143729.aspx</a>, where it&#8217;s clearly just the XMLDATA option. It reads very much as if it&#8217;s just been edited poorly for the FOR XML page.</p>
<p>Hopefully I will have some more information for you about this later.</p>
<p>Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: robfarley		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-312</link>

		<dc:creator><![CDATA[robfarley]]></dc:creator>
		<pubDate>Wed, 27 Aug 2008 18:25:48 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-312</guid>

					<description><![CDATA[Evan,

Thanks for that - I&#039;ll look into it and get back to you when I can.

Rob]]></description>
			<content:encoded><![CDATA[<p>Evan,</p>
<p>Thanks for that &#8211; I&#8217;ll look into it and get back to you when I can.</p>
<p>Rob</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Evan		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-311</link>

		<dc:creator><![CDATA[Evan]]></dc:creator>
		<pubDate>Wed, 27 Aug 2008 13:35:05 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-311</guid>

					<description><![CDATA[So, what do we make of &lt;http://msdn.microsoft.com/en-us/library/ms190922.aspx&gt; saying:

&quot;The FOR XML option is deprecated. Use XSD generation in the case of RAW and AUTO modes. There is no replacement for the explicit mode. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.&quot;

Does this mean &quot;FOR XML&quot; isn&#039;t the answer either?
]]></description>
			<content:encoded><![CDATA[<p>So, what do we make of <<a href="http://msdn.microsoft.com/en-us/library/ms190922.aspx" rel="nofollow ugc">http://msdn.microsoft.com/en-us/library/ms190922.aspx</a>> saying:</p>
<p>&#8220;The FOR XML option is deprecated. Use XSD generation in the case of RAW and AUTO modes. There is no replacement for the explicit mode. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.&#8221;</p>
<p>Does this mean &#8220;FOR XML&#8221; isn&#8217;t the answer either?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bibble		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-310</link>

		<dc:creator><![CDATA[Bibble]]></dc:creator>
		<pubDate>Fri, 22 Aug 2008 17:13:33 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-310</guid>

					<description><![CDATA[Great solution!]]></description>
			<content:encoded><![CDATA[<p>Great solution!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jorge		</title>
		<link>https://lobsterpot.com.au/blog/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql/#comment-309</link>

		<dc:creator><![CDATA[Jorge]]></dc:creator>
		<pubDate>Thu, 26 Jun 2008 09:36:07 +0000</pubDate>
		<guid isPermaLink="false">/blogs/robfarley/archive/2007/04/08/coalesce-is-not-the-answer-to-string-concatentation-in-t-sql.aspx#comment-309</guid>

					<description><![CDATA[You don&#039;t want to use replace since you don&#039;t know exactly what was on the original value and you may also miss some characters that need bve replaced.

Here is what I found to fix it. The solution is to get the value out of the XML that it is produced, using XPATH query. I just cast the value to VARCHAR...and you are done. 

...and here is the sample
Thanks

Jorge

--DROP TABLE t1;
CREATE TABLE t1 (id INT, name VARCHAR(MAX));

INSERT t1 values (1,&#039;Jamie&#039;);
INSERT t1 values (1,&#039;Joe&#039;);
INSERT t1 values (1,&#039;John&#039;);
INSERT t1 values (2,&#039;Sai&#039;);
INSERT t1 values (2,&#039;Sam&#039;);
INSERT t1 values (2,&#039;Can you handle &lt; Check it out&#039;);

INSERT t1 values (2,&#039;What about &gt; Check it out&#039;);

GO

select
   id,
   stuff((
       select &#039;,&#039; + t.[name] as name
       from t1 t
       where t.id = t1.id
       order by t.[name]
       for xml path(&#039;&#039;), TYPE).value(&#039;name[1]&#039;, &#039;VARCHAR(MAX)&#039;) 
   ,1,1,&#039;&#039;) as name_csv
from t1
group by id; 

]]></description>
			<content:encoded><![CDATA[<p>You don&#8217;t want to use replace since you don&#8217;t know exactly what was on the original value and you may also miss some characters that need bve replaced.</p>
<p>Here is what I found to fix it. The solution is to get the value out of the XML that it is produced, using XPATH query. I just cast the value to VARCHAR&#8230;and you are done. </p>
<p>&#8230;and here is the sample<br />
Thanks</p>
<p>Jorge</p>
<p>&#8211;DROP TABLE t1;<br />
CREATE TABLE t1 (id INT, name VARCHAR(MAX));</p>
<p>INSERT t1 values (1,&#8217;Jamie&#8217;);<br />
INSERT t1 values (1,&#8217;Joe&#8217;);<br />
INSERT t1 values (1,&#8217;John&#8217;);<br />
INSERT t1 values (2,&#8217;Sai&#8217;);<br />
INSERT t1 values (2,&#8217;Sam&#8217;);<br />
INSERT t1 values (2,&#8217;Can you handle < Check it out');

INSERT t1 values (2,'What about > Check it out&#8217;);</p>
<p>GO</p>
<p>select<br />
   id,<br />
   stuff((<br />
       select &#8216;,&#8217; + t.[name] as name<br />
       from t1 t<br />
       where t.id = t1.id<br />
       order by t.[name]<br />
       for xml path(&#8221;), TYPE).value(&#8216;name[1]&#8217;, &#8216;VARCHAR(MAX)&#8217;)<br />
   ,1,1,&#8221;) as name_csv<br />
from t1<br />
group by id; </p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
