Home All Groups Group Topic Archive Search About

Changing resultset to 1 row and 1 column

Author
21 Jul 2006 6:50 PM
MittyKom
Hi All

How can i change the resultset of a query from being a column and several
rows to just 1 column and 1 row? eg

select * from tb1
The resultset is as below:

Col1
------
ab
ac
ad

I would like it to come as below (that is as 1 column and 1 row):
Col1
------------
ab, ac, ad

Col1 is varchar(5) in tb1. Thank you in advance.

Author
21 Jul 2006 7:19 PM
Omnibuzz
check this link...
http://omnibuzz-sql.blogspot.com/2006/06/concatenate-values-in-column-in-sql.html

what you need seems to be very similar

Hope this helps.
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/
Author
21 Jul 2006 8:51 PM
Anith Sen
Author
22 Jul 2006 9:52 AM
Omnibuzz
Thanks for the links Anith. I knew there was a problem with the group by
clause, thats why I added a disclaimer that it all depends on the query
execution plan. Anyways.. I was trying to reply to this... then it became so
big that I thought I will put it in my blog for reference :)
Check this out and let me know if I make sense.
http://omnibuzz-sql.blogspot.com/2006/07/resolution-for-concatenate-column.html

-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/
Author
24 Jul 2006 2:57 PM
Anith Sen
That is flattering, but a good job with the explanations :-)

--
Anith
Author
21 Jul 2006 7:21 PM
Alejandro Mesa
See if this helps.
http://groups.google.com/group/microsoft.public.sqlserver.programming/msg/2d85bf366dd9e73e


AMB


Show quote
"MittyKom" wrote:

> Hi All
>
> How can i change the resultset of a query from being a column and several
> rows to just 1 column and 1 row? eg
>
> select * from tb1
> The resultset is as below:
>
> Col1
> ------
> ab
> ac
> ad
>
> I would like it to come as below (that is as 1 column and 1 row):
> Col1
> ------------
> ab, ac, ad
>
> Col1 is varchar(5) in tb1. Thank you in advance.
>
Author
21 Jul 2006 7:24 PM
Arnie Rowland
There are a couple of different approaches. I'll provide you a couple of
references and you can select the one that works best for you.

How to rotate a table in SQL Server
http://support.microsoft.com/default.aspx?scid=kb;en-us;175574
Concatenate One Field to Itself for string
SQL 2000
http://omnibuzz-sql.blogspot.com/2006/06/concatenate-values-in-column-in-sql.html
SQL 2005
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/07/06/871.aspx

--
Arnie Rowland
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"MittyKom" <Mitty***@discussions.microsoft.com> wrote in message
news:F455E6C5-255E-4F51-90CD-452C484DC50B@microsoft.com...
> Hi All
>
> How can i change the resultset of a query from being a column and several
> rows to just 1 column and 1 row? eg
>
> select * from tb1
> The resultset is as below:
>
> Col1
> ------
> ab
> ac
> ad
>
> I would like it to come as below (that is as 1 column and 1 row):
> Col1
> ------------
> ab, ac, ad
>
> Col1 is varchar(5) in tb1. Thank you in advance.
>
Author
21 Jul 2006 7:26 PM
Sylvain Devidal
Hello MittyKom,

I think there is no other solution than using a cursor.
-> load the query result in a cursor
-> the concatenate each line together in a temporary variable
-> then return the temporary variable

Show quote
"MittyKom" <Mitty***@discussions.microsoft.com> wrote in message
news:F455E6C5-255E-4F51-90CD-452C484DC50B@microsoft.com...
> Hi All
>
> How can i change the resultset of a query from being a column and several
> rows to just 1 column and 1 row? eg
>
> select * from tb1
> The resultset is as below:
>
> Col1
> ------
> ab
> ac
> ad
>
> I would like it to come as below (that is as 1 column and 1 row):
> Col1
> ------------
> ab, ac, ad
>
> Col1 is varchar(5) in tb1. Thank you in advance.
>

AddThis Social Bookmark Button