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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Output like comma separated values

Author  Topic 

Ciupaz
Posting Yak Master

232 Posts

Posted - 2012-11-30 : 09:18:47
Hello all,
I have a simple query that returns me one column of integer values.
Something like this:


SELECT TableA.ID
FROM TableA INNER JOIN TableB
ON TableA.FieldFK = TableB.FieldFK

.....(query output)

12
34
32
36
3
6

Now I have to populate a VARCHAR variable with these values separated by commas, like:


DECLARE @MyList VARCHAR(1000)

@MyList = '12,34,32,36,3,6,'


How can I obtain this?

THanks in advance.

Luigi

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-30 : 09:29:46
[code]SELECT
STUFF(c,1,1,'') AS CommaSeparated
FROM
(
SELECT
',' AS [text()],
a.ID AS [text()]
FROM
TableA a
INNER JOIN TableB b
ON a.FieldFK = b.FieldFK
FOR XML PATH('')
)T(c)[/code]
Go to Top of Page

Ciupaz
Posting Yak Master

232 Posts

Posted - 2012-11-30 : 10:21:36
Perfect, thank you very much Sunitabeck.

Luigi
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-30 : 11:08:02
You are very welcome, pleasure to be of help.

Sunita
Go to Top of Page
   

- Advertisement -