SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

jimoomba
Yak Posting Veteran

India
90 Posts

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

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,

rams

bandi
Flowing Fount of Yak Knowledge

India
2217 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')

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

--Alternate is:

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


--
Chandu

Edited by - bandi on 11/23/2012 06:53:12
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.03 seconds. Powered By: Snitz Forums 2000