SQL Server – Select column data from multiple rows, and string together in one row and in one column

Let’s say you have 5 rows in a table. Column1 is populated for 4 out of the 5 rows with the following data:

599
abc
xyz
456

The following SQL will provide the results as such:

599, abc, xyz, 456

declare @YourString varchar(4000)

select @YourString = isnull(@YourString + ‘, ‘, ”) + cast(column1 as varchar(20))
from some_table
where guid = @SomeGUID
group by column1

select @YourString

Please follow and like us: