<?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>DevExpertise &#187; T-SQL</title>
	<atom:link href="http://www.devexpertise.com/category/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexpertise.com</link>
	<description>Practical tips and tricks for all things .NET, SharePoint, Silverlight, InfoPath, and general application development.</description>
	<lastBuildDate>Wed, 12 May 2010 14:32:33 +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>SQL Tip/Trick: Retrieving Records as a Comma-Delimited Value Using FOR XML and STUFF</title>
		<link>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/</link>
		<comments>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 12:50:49 +0000</pubDate>
		<dc:creator>DevExpert</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/</guid>
		<description><![CDATA[Once in awhile I run across a need to create a delimited list from a set of rows in a SQL table.&#160; The old-school method is to use a cursor to iterate over these rows and build a dynamic string.&#160; As we all know, cursors are expensive and should be avoided whenever possible.&#160; I recently [...]]]></description>
			<content:encoded><![CDATA[<p>Once in awhile I run across a need to create a delimited list from a set of rows in a SQL table.&#160; The old-school method is to use a cursor to iterate over these rows and build a dynamic string.&#160; As we all know, cursors are expensive and should be avoided whenever possible.&#160; I recently found a better way to accomplish this and wanted to share it in case you’ve run into this before.</p>
<p>For this example, I’m going to be working with a simple Cities table that contains a City field and a State field.&#160; A simple SELECT statement yields the following result:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York'</span></pre>
</div>
<p><a href="http://www.devexpertise.com/wp-content/uploads/2009/04/image5.png"  rel="lightbox"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.devexpertise.com/wp-content/uploads/2009/04/image-thumb5.png" width="186" height="139" /></a> </p>
</p>
<p>
  <br />The first step is to transform this result into a chunk of XML, and for that the <a href="http://msdn.microsoft.com/en-us/library/ms345137.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">FOR XML</a> statement is perfect.&#160; Consider the following SQL:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">declare </span>@xml <span style="color: blue">varchar</span><span style="color: gray">(</span>1000<span style="color: gray">)
</span><span style="color: blue">set </span>@xml <span style="color: gray">= (</span><span style="color: blue">select </span>City <span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York' </span><span style="color: blue">for xml path</span><span style="color: gray">(</span><span style="color: red">''</span><span style="color: gray">))</span></pre>
</div>
<p>
  <br />This returns the following (I trimmed it down a little to fit on the page, but it does return all rows):</p>
<blockquote>
<p><strong>&lt;City&gt;New York City&lt;/City&gt;&lt;City&gt;Buffalo&lt;/City&gt;&lt;City&gt;Rochester&lt;/City&gt;</strong></p>
</blockquote>
<p>Now that we have an XML string, we can just use the built-in REPLACE functions to remove the XML nodes:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">set </span>@xml <span style="color: gray">= </span><span style="color: magenta">replace</span><span style="color: gray">(</span>@xml<span style="color: gray">, </span><span style="color: red">'&lt;City&gt;'</span><span style="color: gray">, </span><span style="color: red">','</span><span style="color: gray">)
</span><span style="color: blue">set </span>@xml <span style="color: gray">= </span><span style="color: magenta">replace</span><span style="color: gray">(</span>@xml<span style="color: gray">, </span><span style="color: red">'&lt;/City&gt;'</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>
  <br />This returns the following:</p>
<blockquote>
<p><strong>,New York City,Buffalo,Rochester,Yonkers,Syracuse</strong></p>
</blockquote>
<p>Now, the only thing left is to remove the beginning space and comma from this for which the new SQL 2008 <a href="http://msdn.microsoft.com/en-us/library/ms188043.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">STUFF</a> function is perfect:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: magenta">stuff</span><span style="color: gray">(</span>@xml<span style="color: gray">,</span>1<span style="color: gray">,</span>1<span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>
  <br />This returns the following, which is exactly what we need:</p>
<blockquote>
<p><strong>New York City,Buffalo,Rochester,Yonkers,Syracuse</strong></p>
</blockquote>
<p>Now, chances are you don’t want to have to declare variables and do all this in multiple operations.&#160; This can all be wrapped into a single select statement, which makes it easy to implement:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: magenta">stuff</span><span style="color: gray">((</span><span style="color: magenta">replace</span><span style="color: gray">(</span><span style="color: magenta">replace</span><span style="color: gray">(
       (</span><span style="color: blue">select </span>City <span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York' </span><span style="color: blue">for xml path</span><span style="color: gray">(</span><span style="color: red">''</span><span style="color: gray">)), </span><span style="color: red">'&lt;City&gt;'</span><span style="color: gray">, </span><span style="color: red">','
       </span><span style="color: gray">), </span><span style="color: red">'&lt;/City&gt;'</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)),</span>1<span style="color: gray">,</span>1<span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>&#160;</p>
<p>That’s it!&#160; Pretty slick, huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
