<?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>Ray Jezek's Blog &#187; SQL Server</title>
	<atom:link href="http://ray.jez.net/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://ray.jez.net</link>
	<description>AllThingsR</description>
	<lastBuildDate>Sun, 10 Jan 2010 01:51:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Concatenation with COALESCE</title>
		<link>http://ray.jez.net/concatenation-with-coalesce/</link>
		<comments>http://ray.jez.net/concatenation-with-coalesce/#comments</comments>
		<pubDate>Fri, 22 Aug 2003 07:31:00 +0000</pubDate>
		<dc:creator>ray</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">/jez/archive/2003/08/22/146.aspx</guid>
		<description><![CDATA[I will forever be grateful to Ryan for telling me about how to create comma delimited strings out of many-to-many relationships in SQL Server.  It&#8217;s an absolute gem! 
So if were trying to bring back Employees and their territories from Northwind you could do the following:
CREATE FUNCTION dbo.GetCommaList(@EmployeeID int)  RETURNS varchar(500) AS  [...]]]></description>
			<content:encoded><![CDATA[<p><font face="Tahoma" size="2">I will forever be grateful to <a href="http://blogs.geekdojo.net/ryan">Ryan</a> for telling me about how to create comma delimited strings out of many-to-many relationships in SQL Server.  It&#8217;s an absolute gem! </font><br />
<font face="Tahoma" size="2">So if were trying to bring back Employees and their territories from Northwind you could do the following:</font><br />
<font face="Courier New" size="2"><font color="#0000ff">CREATE FUNCTION</font> dbo.GetCommaList(@EmployeeID <font color="#0000ff">int</font>)  <br /><font color="#0000ff">RETURNS varchar</font>(500) <font color="#0000ff">AS</font>  <br /><font color="#0000ff">BEGIN </font></font><br />
<font face="Courier New" size="2"><font color="#0000ff">            DECLARE</font> @StringList <font color="#0000ff">varchar</font>(500) </font><br />
<font face="Courier New" size="2">            <font color="#0000ff">SELECT</font> @StringList = <font color="#ff1493">COALESCE</font>(@StringList + &#8216;, &#8216;, &#8221;) + <font color="#ff1493">RTRIM</font>(t.TerritoryDescription) <br />            <font color="#0000ff">FROM</font>   EmployeeTerritories et <br />                   <font color="#0000ff">INNER</font> <font color="#808080">JOIN</font> Employees e <font color="#0000ff">ON</font> et.EmployeeID = e.EmployeeID <br />                   <font color="#0000ff">INNER</font> <font color="#808080">JOIN</font> Territories t <font color="#0000ff">ON</font> et.TerritoryID = t.TerritoryID <br />            <font color="#0000ff">WHERE</font>  et.EmployeeID = @EmployeeID </font></p>
<p><font face="Courier New" size="2">            <br />            <font color="#0000ff">IF</font> @StringList <font color="#0000ff">IS</font> <font color="#808080">NULL</font> <br />                <font color="#0000ff">SET</font> @StringList = &#8216;No Territories&#8217; </font><br />
<font face="Courier New" size="2">            <font color="#0000ff">RETURN</font> @StringList <br /><font color="#0000ff">END</font> </font><br />
<font face="Garamond"><font face="Tahoma" size="2">And then:</font> </font><br />
<font face="Courier New" size="2"><font color="#0000ff">SELECT</font> FirstName, LastName, dbo.GetCommaList(EmployeeID) <font color="#0000ff">AS</font> Territories<br /><font color="#0000ff">FROM</font> Employees</font> <br />
<font face="Garamond"><font face="Tahoma" size="2">This would return the list of employees with all of their corresponding territories in a comma delimited string.  Of course there are a number of variations of this same idea that you could so but the prinicple is useful in so many situations.</font><br /></font></p>
]]></content:encoded>
			<wfw:commentRss>http://ray.jez.net/concatenation-with-coalesce/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

