Query : How to fetch comma separted column values

Comma separated column values without using any function.

To get comma separated all column values of any table

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

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


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: