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 >)


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: