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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Output like comma separated values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Ciupaz
Posting Yak Master

Italy
232 Posts

Posted - 11/30/2012 :  09:18:47  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/30/2012 :  09:29:46  Show Profile  Reply with Quote
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)
Go to Top of Page

Ciupaz
Posting Yak Master

Italy
232 Posts

Posted - 11/30/2012 :  10:21:36  Show Profile  Reply with Quote
Perfect, thank you very much Sunitabeck.

Luigi
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/30/2012 :  11:08:02  Show Profile  Reply with Quote
You are very welcome, pleasure to be of help.

Sunita
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