<?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: T-SQL &#8211; How to select multiple rows into a single row</title>
	<atom:link href="http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/feed/" rel="self" type="application/rss+xml" />
	<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/</link>
	<description>The blog for developers</description>
	<lastBuildDate>Sun, 05 Feb 2012 21:35:36 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: mrinal</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-196006</link>
		<dc:creator>mrinal</dc:creator>
		<pubDate>Thu, 01 Dec 2011 07:10:26 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-196006</guid>
		<description>thanks</description>
		<content:encoded><![CDATA[<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dobert</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-154492</link>
		<dc:creator>Dobert</dc:creator>
		<pubDate>Wed, 17 Aug 2011 09:19:56 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-154492</guid>
		<description>Thanks a lot Jarosław, your page saved me a lot of time! 

Didn&#039;t need as sophisticated solution as you and what Chris suggested was perfect for my needs :)</description>
		<content:encoded><![CDATA[<p>Thanks a lot Jarosław, your page saved me a lot of time! </p>
<p>Didn&#8217;t need as sophisticated solution as you and what Chris suggested was perfect for my needs <img src='http://dobrzanski.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rais Hussain</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-133389</link>
		<dc:creator>Rais Hussain</dc:creator>
		<pubDate>Mon, 04 Jul 2011 11:48:08 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-133389</guid>
		<description>I have one table named Fixture which contains a column FixtureID. For instance, I want to get top 5 FixtureID and pass it to another query like comma separated value, but the comparison would be with BIGINT field like;

CREATE TABLE Fixture (FixtureID BIGINT)


INSERT INTO Fixture(FixtureID) VALUES(11111111)
INSERT INTO Fixture(FixtureID) VALUES(22222222)
INSERT INTO Fixture(FixtureID) VALUES(33333333)
INSERT INTO Fixture(FixtureID) VALUES(44444444)
INSERT INTO Fixture(FixtureID) VALUES(55555555)

DECLARE @res NVARCHAR(max)  
SET @res = &#039;&#039;  
  
 
SELECT TOP 5 @res = @res + FixtureID + &#039;, &#039; from Fixture  
SELECT @res = substring(@res,1,len(@res)-1) 


SELECT * FROM Match
WHERE  FixtureID IN (@res)

FixtureID is BIGINT, how should I compare comma separated values with BIGINT column?</description>
		<content:encoded><![CDATA[<p>I have one table named Fixture which contains a column FixtureID. For instance, I want to get top 5 FixtureID and pass it to another query like comma separated value, but the comparison would be with BIGINT field like;</p>
<p>CREATE TABLE Fixture (FixtureID BIGINT)</p>
<p>INSERT INTO Fixture(FixtureID) VALUES(11111111)<br />
INSERT INTO Fixture(FixtureID) VALUES(22222222)<br />
INSERT INTO Fixture(FixtureID) VALUES(33333333)<br />
INSERT INTO Fixture(FixtureID) VALUES(44444444)<br />
INSERT INTO Fixture(FixtureID) VALUES(55555555)</p>
<p>DECLARE @res NVARCHAR(max)<br />
SET @res = &#8221;  </p>
<p>SELECT TOP 5 @res = @res + FixtureID + &#8216;, &#8216; from Fixture<br />
SELECT @res = substring(@res,1,len(@res)-1) </p>
<p>SELECT * FROM Match<br />
WHERE  FixtureID IN (@res)</p>
<p>FixtureID is BIGINT, how should I compare comma separated values with BIGINT column?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TC Tham</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-37967</link>
		<dc:creator>TC Tham</dc:creator>
		<pubDate>Tue, 22 Jun 2010 03:01:53 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-37967</guid>
		<description>Can try use my code if you are interested.

Sorry if it is abit messy, I&#039;m new to SQL and I&#039;m not particularly good at coding.

--------------------------------------------------------------------
declare @Counter int
declare @RowCount int
declare @RESULT NVARCHAR(max)
declare @TempRESULT NVARCHAR(max)
declare @Table Table (
internalID int identity(1,1),
id int,
Name varchar(max)
)

insert @table (id, Name)
select id, Name from People
order by id asc

set @Counter = 0
set @RESULT = &#039;&#039;
select @RowCount = COUNT(internalID) from @Table
while @Counter &lt; @RowCount
begin
	set @Counter = @Counter + 1
	Select @TempRESULT = cast(id as varchar(max)) from @Table where internalID = @Counter 
	select @TempRESULT = @TempRESULT + &#039;:&#039; + Name from @Table where internalID = @Counter
	if (@Counter = 1)
	begin
		set @RESULT = @TempRESULT
	end
	else
	begin
		set @RESULT = @RESULT + &#039;, &#039; + @TempRESULT
	end
end

------------------------------------------------------------------

It should work. I modified it abit from my codings to this.</description>
		<content:encoded><![CDATA[<p>Can try use my code if you are interested.</p>
<p>Sorry if it is abit messy, I&#8217;m new to SQL and I&#8217;m not particularly good at coding.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
declare @Counter int<br />
declare @RowCount int<br />
declare @RESULT NVARCHAR(max)<br />
declare @TempRESULT NVARCHAR(max)<br />
declare @Table Table (<br />
internalID int identity(1,1),<br />
id int,<br />
Name varchar(max)<br />
)</p>
<p>insert @table (id, Name)<br />
select id, Name from People<br />
order by id asc</p>
<p>set @Counter = 0<br />
set @RESULT = &#8221;<br />
select @RowCount = COUNT(internalID) from @Table<br />
while @Counter &lt; @RowCount<br />
begin<br />
	set @Counter = @Counter + 1<br />
	Select @TempRESULT = cast(id as varchar(max)) from @Table where internalID = @Counter<br />
	select @TempRESULT = @TempRESULT + &#039;:&#039; + Name from @Table where internalID = @Counter<br />
	if (@Counter = 1)<br />
	begin<br />
		set @RESULT = @TempRESULT<br />
	end<br />
	else<br />
	begin<br />
		set @RESULT = @RESULT + &#039;, &#039; + @TempRESULT<br />
	end<br />
end</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>It should work. I modified it abit from my codings to this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-37148</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Thu, 10 Jun 2010 13:48:19 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-37148</guid>
		<description>Simple code for a simple problem.  This is an oldie....

declare @RESULT NVARCHAR(max)
set @RESULT = &#039;&#039;

SELECT @RESULT = @RESULT + CASE @RESULT WHEN &#039;&#039; THEN &#039;&#039; ELSE &#039;, &#039; END + cast(id as NVARCHAR(MAX)) + &#039;: &#039; + ISNULL(NAME,&#039; &#039;)
FROM people
ORDER BY id</description>
		<content:encoded><![CDATA[<p>Simple code for a simple problem.  This is an oldie&#8230;.</p>
<p>declare @RESULT NVARCHAR(max)<br />
set @RESULT = &#8221;</p>
<p>SELECT @RESULT = @RESULT + CASE @RESULT WHEN &#8221; THEN &#8221; ELSE &#8216;, &#8216; END + cast(id as NVARCHAR(MAX)) + &#8216;: &#8216; + ISNULL(NAME,&#8217; &#8216;)<br />
FROM people<br />
ORDER BY id</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarosław Dobrzański</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-21720</link>
		<dc:creator>Jarosław Dobrzański</dc:creator>
		<pubDate>Wed, 26 Aug 2009 12:22:09 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-21720</guid>
		<description>Cursors are an option here but I didn&#039;t want to use them. Not only is it (much) longer but also not that efficient (especially if a table holds lots of data).</description>
		<content:encoded><![CDATA[<p>Cursors are an option here but I didn&#8217;t want to use them. Not only is it (much) longer but also not that efficient (especially if a table holds lots of data).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: witecat</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-21719</link>
		<dc:creator>witecat</dc:creator>
		<pubDate>Wed, 26 Aug 2009 12:09:13 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-21719</guid>
		<description>--- HOW ABOUT USING CURSORS??:


--- SELECT ONE COLUMN ---
DECLARE @colvalu varchar (150),
	@cString1 varchar (MAX),
	@cString2 varchar (MAX)
SET @cString2 = &#039;&#039;

DECLARE my_cursor CURSOR FOR
	select distinct COLNAME from TEBLENAME
OPEN my_cursor

FETCH NEXT FROM my_cursor
INTO @colvalu

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @cString1 = @colvalu + &#039;,&#039;
    FETCH NEXT FROM my_cursor
    INTO @colvalu
SET @cString2 = @cString2 + @cString1
END
SET @cString2 = Left(@cString2, Len(@cString2)-1)
print @cString2
CLOSE my_cursor
DEALLOCATE my_cursor

--- SELECT 2(+) COLUMNS ---
DECLARE @colvalu_1 varchar (150),
	@colvalu_2 varchar (150),
	@cString1 varchar (MAX),
	@cString2 varchar (MAX)
SET @cString2 = &#039;&#039;

DECLARE my_cursor CURSOR FOR
	SELECT COLNAME_1, COLNAME_2 FROM DBNAME
OPEN my_cursor

FETCH NEXT FROM my_cursor
INTO @colvalu_1, @colvalu_2

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @cString1 = @colvalu_1 + @colvalu_2 + &#039;,&#039;
    FETCH NEXT FROM my_cursor
    INTO @colvalu_1, @colvalu_2
SET @cString2 = @cString2 + @cString1
END
SET @cString2 = Left(@cString2, Len(@cString2)-1)
print @cString2
CLOSE my_cursor
DEALLOCATE my_cursor</description>
		<content:encoded><![CDATA[<p>&#8212; HOW ABOUT USING CURSORS??:</p>
<p>&#8212; SELECT ONE COLUMN &#8212;<br />
DECLARE @colvalu varchar (150),<br />
	@cString1 varchar (MAX),<br />
	@cString2 varchar (MAX)<br />
SET @cString2 = &#8221;</p>
<p>DECLARE my_cursor CURSOR FOR<br />
	select distinct COLNAME from TEBLENAME<br />
OPEN my_cursor</p>
<p>FETCH NEXT FROM my_cursor<br />
INTO @colvalu</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
	SET @cString1 = @colvalu + &#8216;,&#8217;<br />
    FETCH NEXT FROM my_cursor<br />
    INTO @colvalu<br />
SET @cString2 = @cString2 + @cString1<br />
END<br />
SET @cString2 = Left(@cString2, Len(@cString2)-1)<br />
print @cString2<br />
CLOSE my_cursor<br />
DEALLOCATE my_cursor</p>
<p>&#8212; SELECT 2(+) COLUMNS &#8212;<br />
DECLARE @colvalu_1 varchar (150),<br />
	@colvalu_2 varchar (150),<br />
	@cString1 varchar (MAX),<br />
	@cString2 varchar (MAX)<br />
SET @cString2 = &#8221;</p>
<p>DECLARE my_cursor CURSOR FOR<br />
	SELECT COLNAME_1, COLNAME_2 FROM DBNAME<br />
OPEN my_cursor</p>
<p>FETCH NEXT FROM my_cursor<br />
INTO @colvalu_1, @colvalu_2</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
	SET @cString1 = @colvalu_1 + @colvalu_2 + &#8216;,&#8217;<br />
    FETCH NEXT FROM my_cursor<br />
    INTO @colvalu_1, @colvalu_2<br />
SET @cString2 = @cString2 + @cString1<br />
END<br />
SET @cString2 = Left(@cString2, Len(@cString2)-1)<br />
print @cString2<br />
CLOSE my_cursor<br />
DEALLOCATE my_cursor</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: simon</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-20611</link>
		<dc:creator>simon</dc:creator>
		<pubDate>Sun, 02 Aug 2009 05:57:25 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-20611</guid>
		<description>Cool, thanks for this guys, I got stuck with something like this a while back. I could not do it with SQL, I had to move the formatting to my application.</description>
		<content:encoded><![CDATA[<p>Cool, thanks for this guys, I got stuck with something like this a while back. I could not do it with SQL, I had to move the formatting to my application.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarosław Dobrzański</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-20477</link>
		<dc:creator>Jarosław Dobrzański</dc:creator>
		<pubDate>Wed, 29 Jul 2009 19:59:09 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-20477</guid>
		<description>So true, you can always replace parts of the string... Sometimes easy solutions are the hardest to find :)</description>
		<content:encoded><![CDATA[<p>So true, you can always replace parts of the string&#8230; Sometimes easy solutions are the hardest to find <img src='http://dobrzanski.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lashek</title>
		<link>http://dobrzanski.net/2009/07/16/tsql-select-multiple-rows-single-row/comment-page-1/#comment-20474</link>
		<dc:creator>Lashek</dc:creator>
		<pubDate>Wed, 29 Jul 2009 15:04:54 +0000</pubDate>
		<guid isPermaLink="false">http://dobrzanski.net/?p=449#comment-20474</guid>
		<description>You could always stream it as XML Data and convert it into one column that way, similar to what you started with:

ISNULL(LTRIM(RTRIM(REPLACE( 
 ( 
  SELECT CAST(ID AS NVARCHAR(MAX)) + &#039;: &#039; + NAME AS [data()] 
  FROM People
  ORDER BY ID
  FOR XML PATH (&#039;&#039;) 
 ), &#039;&#039;, &#039;,&#039;))), &#039;&#039;)


For instances where it might &quot;replace&quot; strings with things like &gt;... you could always encapsulate in a REPLACE string to change it back?

I use this trick a lot, but all the data I pass to it is alphanumeric with no symbols or unicode characters.</description>
		<content:encoded><![CDATA[<p>You could always stream it as XML Data and convert it into one column that way, similar to what you started with:</p>
<p>ISNULL(LTRIM(RTRIM(REPLACE(<br />
 (<br />
  SELECT CAST(ID AS NVARCHAR(MAX)) + &#8216;: &#8216; + NAME AS [data()]<br />
  FROM People<br />
  ORDER BY ID<br />
  FOR XML PATH (&#8221;)<br />
 ), &#8221;, &#8216;,&#8217;))), &#8221;)</p>
<p>For instances where it might &#8220;replace&#8221; strings with things like &gt;&#8230; you could always encapsulate in a REPLACE string to change it back?</p>
<p>I use this trick a lot, but all the data I pass to it is alphanumeric with no symbols or unicode characters.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

