views:

195

answers:

4

i have this table

Cream
----------
CHOCALATE
GREEN
TEST

want out put in select query like this

cream

CHOCALATE,GREEN,TEST

+2  A: 

You can use coalesce

astander
+3  A: 

With sysobjects this worked:

DECLARE @List varchar(2000)

SELECT @List = COALESCE(@List + ',', '') + Cast(name As varchar(50))
FROM sys.sysobjects

SELECT @List As 'List'
Arthur
brilliant, simply brilliant. I have always went down the aggregate UDF path before but this solution relies on sequential evaluation of the variable expressions!
zvolkov
A: 

The best way to do this, is to create an aggregate user defined function and register it on the database.

Try here for an example.

http://msdn.microsoft.com/en-us/library/ms165055.aspx

Your query would look like "SELECT dbo.Concatenate(Field) FROM Cream"

You will be returned what you expect "a,b,c,d..." ect.

mrwayne
A: 

I found a useful resource here when I needed to do this, but as the others said, use COALESCE...

DECLARE @List VARCHAR(1000)

SELECT @List = COALESCE(@List + ', ', '') + Name
FROM Cream

SELECT @List
Jane