Search code examples
sqlt-sql

How to concatenate all the records in a column returned by a query into one varchar string in T-SQL?


A query (SELECT/FUNCTION/VIEW/PROCEDURE) returns a column of varchar records. I need to concatenate them all into one single varchar line. How do I best do it in T-SQL?


Solution

  • declare @s varchar(8000)
    select @s = coalesce(@s + ', ' + col, col) from tbl