SQL Merge Multiple Rows Into Single String (2)

Method 1:

DECLARE @str varchar(4000)SET @str = (SELECT CONTACT_EMAIL + ';' FROM table FOR XML PATH(''))SET @str = SUBSTRING(@str,1,LEN(@str)-1)SELECT @str

Method 2:

DECLARE @str varchar(4000)SELECT @str = COALESCE(@str + ';', '') + CONTACT_EMAIL FROM tableSELECT @str

Method 3:

DECLARE @str varchar(4000)SELECT DISTINCT STUFF( (SELECT CONTACT_EMAIL + ';'   from table FOR XML PATH('')),1,1,'')SELECT @str

Resource: Nagendra Prasad

Leave a Reply

Your email address will not be published. Required fields are marked *