Query : How to fetch comma separted column values


Comma separated column values without using any function.

Problem
To get comma separated all column values of any table
Solution


declare @idString varchar(max)
set @idString = ''
select @idString = @idString + ',' + cast(< column > as varchar) from < table Name >
print @idString

Usage
It's very handy if need to return all related id's in a string.Typically these comma separated Ids goes into 'In' command. i.e. select < column list > from tablename where id in(< comma separated ids >)


Related Articles

How to get rows into comma separated values column in sql

How to get rows into comma separated values column in sql. Fetching rows values into comma separated column value is required lots of time in our projects. So I tried to achieve this and lastly found one solution. Use this to get all row values into one single column value with comma separation.

Split Comma separated values into Columns

In this article, I explain about how to split a string with delimiter into list of rows. De limiter can be comma or plus our requirement is to convert a single row of a table into multiple rows based on number of delimiter.

More articles: Comma separated value SQL query

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: