Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get comma separated value from multiple row in sql server 2005

i have this table

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

want out put in select query like this

cream

CHOCALATE,GREEN,TEST

like image 855
Yagnesh84 Avatar asked Dec 14 '22 02:12

Yagnesh84


2 Answers

With sysobjects this worked:

DECLARE @List varchar(2000)

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

SELECT @List As 'List'
like image 122
Arthur Avatar answered Jun 11 '23 16:06

Arthur


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
like image 26
Jane Avatar answered Jun 11 '23 15:06

Jane