<?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>.NET Tips and Tricks &#187; xml path</title>
	<atom:link href="http://kossovsky.net/index.php/tag/xml-path/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>SQL 2005 &#8211; Return query rows as one delimited column</title>
		<link>http://kossovsky.net/index.php/2009/06/sql-2005-return-rows-as-delimited-column/</link>
		<comments>http://kossovsky.net/index.php/2009/06/sql-2005-return-rows-as-delimited-column/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 09:34:28 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[concatenating]]></category>
		<category><![CDATA[customer id]]></category>
		<category><![CDATA[delimited]]></category>
		<category><![CDATA[for xml path]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[microsoft sql]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[root root]]></category>
		<category><![CDATA[several ways]]></category>
		<category><![CDATA[sql query]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sql server 2005]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[xml path]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=272</guid>
		<description><![CDATA[Once in a while we need to return data from SQL as delimited string of values and not as rows. There are several ways to do that, but personally i like the next one. SELECT CustomerID AS 'data()' FROM Customer FOR XML PATH('') -- This will result one column with a value like this: 1 [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Once in a while we need to return data from SQL as delimited string of values and not as rows. There are several ways to do that, but personally i like the next one.</p>
<p><span id="more-272"></span></p>
<pre class="sql:nogutter" name="code">
SELECT CustomerID AS 'data()'
FROM Customer
FOR XML PATH('')
-- This will result one column with a value like this: 1 2 3 4 5 6 7 8

-- And to get the column as part of some other query:
SELECT CustomerID,
       (SELECT OrderID AS 'data()'
       FROM Order O WHERE O.CustomerID = C.Customer ID
       FOR XML PATH('')) AS OrderList
FROM Customer C

-- The result of the above query will be something like this :
-- CustomerID	| OrderList
-- --------------------------------
-- 1		    | 1 2 5 9 90 200
-- 2		    | 2 7 10 22
-- 3		    | 12 17 246
-- 4		    | 83 92 1002

-- And of course we could return it as XML
SELECT CustomerID AS '@id',
       (SELECT OrderID AS 'data()'
       FROM Order O WHERE O.CustomerID = C.Customer ID
       FOR XML PATH('')) AS '@ordelist'
FROM Customer C
FOR XML PATH('customer'), ROOT('root')

-- The result of the above query will be something like this :
&lt;root&gt;
  &lt;customer id="1" orderlist="1 2 5 9 90 200"/&gt;
  &lt;customer id="2" orderlist="2 7 10 22"/&gt;
  &lt;customer id="3" orderlist="12 17 246"/&gt;
  &lt;customer id="4" orderlist="83 92 1002"/&gt;
&lt;/root&gt;
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fsql-2005-return-rows-as-delimited-column%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/sql-2005-return-rows-as-delimited-column/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

