Can somebody explain how is FOR XML used in SQL Server? Is this the only way to concatenate strings by rows without using an user-defined function? I got it from this post, did what I need to do, but I don't understand it.
The person who posted this did not explain anything. After an exhaustive search I couldn't find anything understandable. And no, MSDN was not helpful given my limited SQL skills.
Here's the my SQL, with only the names changed from the one in that post.
SELECT
ID,
STUFF((
SELECT ' -' + Code
FROM #Z
WHERE (ID = Results.ID)
FOR XML PATH(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)'),1,2,''
) AS ConcatCode
FROM #Z Results
GROUP BY ID