Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Get Column values as comma seperated string

5.00/5 (4 votes)
13 Apr 2012CPOL 18.1K  
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:
 
SQL
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)