65.9K
CodeProject is changing. Read more.
Home

Get Column values as comma seperated string

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Apr 13, 2012

CPOL
viewsIcon

18112

Another option:declare @commaStringList varchar(max)set @commaStringList = ''select @commaStringList = @commaStringList + ', ' + CAST(ColumnName as VARCHAR) from MyTable -- remove leading commentif len(@commaStringList) > 3 set @commaStringList =...

Another option:
 
declare @commaStringList varchar(max)
set @commaStringList = ''
select @commaStringList = @commaStringList + ', ' +
       CAST(ColumnName as VARCHAR) from MyTable 
 

-- remove leading comment
if len(@commaStringList) > 3
  set @commaStringList = substring(@commaStringList, 3, len(@commaStringList) - 2)
 
-- show the result
select @commaStringList