Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 combining multiple rows
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

90 Posts

Posted - 11/23/2012 :  05:16:40  Show Profile  Reply with Quote

I have a delima, and im not really sure if this possible. But i have a table like lets say

id | data1
1 this
2 that
3 stuff

i want to be able to return this as one row with the data from data1 in one column inside colons each seperated by commas.

so the result would be

1 Column
'this', 'that', 'stuff'

can anyone help me with this.

Thank you,


Flowing Fount of Yak Knowledge

2242 Posts

Posted - 11/23/2012 :  06:43:58  Show Profile  Reply with Quote
DECLARE @tab1 TABLE(id int, data1 varchar(10))
INSERT INTO @tab1 VALUES(1, 'this'),(2, 'that'),(3, 'stuff')

DECLARE @a varchar(1000)
SELECT @a = COALESCE(@a + ', ', '') + data1
FROM @tab1

--Alternate is:

SELECT id, STUFF((SELECT ',' + data1 FROM @tab1 FOR XML PATH('')),1,1,'') AS CSV
FROM @tab1


Edited by - bandi on 11/23/2012 06:53:12
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000