Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Get Column values as comma seperated string

, 13 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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


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


About the Author

Elina Blank
Software Developer (Senior) The Code Project
United States United States
Elina joined the Code Project team as a software developer in order to make the site run even smoother than it is now. She will also take care of making wishes and promises on the site improvements come to the light sooner than later. When not at work, she enjoys being with her family and wishes there will be at least 30 hours in a day Smile | :)

Comments and Discussions

GeneralYou are right, typo fixed. (and improved a bit) PinadminElina Blank11-Jun-10 6:49 
GeneralReason for my vote of 1 Does not work. PinmemberManas Bhardwaj11-Jun-10 6:47 
GeneralDoen't work buddy. Btw, I tested in SQL Server 2005. PinmemberManas Bhardwaj11-Jun-10 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150302.1 | Last Updated 13 Apr 2012
Article Copyright 2012 by Elina Blank
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid