Get Column values as comma seperated string





5.00/5 (4 votes)
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