Return all values in a comma-delimited string
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
Premium Access
Register now for unlimited access to our premium content across our network of over 70 information Technology web sites.
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy
Dig Deeper
-
People who read this also read...
This was first published in February 2003
Disclaimer:
Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.
Join the conversationComment
Share
Comments
Results
Contribute to the conversation