Problem solve Get help with specific problems with your technologies, process and projects.

Return all values in a comma-delimited string

This script will return one big string where the rows are separated by a comma.

This script will return one big string where the rows are seperated by commas. When getting a set of rows of a field (any string type field), this script could be used to find the field names seperated by commas. It works on SQL server 2000 and 7.0.

To get the list of fields in the Job table (Pubs database), replace <Column_Name> with Column_Name, <Table_Name> with Information_Schema.Columns, <ConditionList> with Table_Name = 'jobs' and <OrderList> with Ordinal_Position.

Declare @Str varchar(4000)
 SET @Str =''
 Select @Str =@Str + ', '+ <Column_Name> From <TableName>
 Where <ConditionList>
 Order By <OrderList>
 IF LEN(@Str)>2
  Select Substring(@Str, 3, LEN(@Str)-2)

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

Dig Deeper on XML in SQL Server

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close