How to concatenate many rows into a single text string?
It’s easier than you think!
Let’s say that my table might look like this:
Now, I want to combine the values of ‘Name’ into a single string (separated by a comma: ‘, ‘)
“James, Daniel, Cindy, Edward”
You might want to use a query similar to this one:
DECLARE @Names VARCHAR(MAX)
SELECT @Names = COALESCE(@Names + ', ', '') + Name FROM People
Want to split it up into separate values again? 🙂
Use this table-valued function (TVF) to do that:
Multi-value parameter function