I m using the below query to merge column Message
based on column 'Customer_Name' from table Customers
SELECT
[Customer_Name],
STUFF((SELECT
', ' + LTRIM(RTRIM([Message]))
FROM [dbo].[Customers] t2
WHERE t2.[Customer_Name] = t1.[Customer_Name]
FOR XML PATH ('')), 1, 1, '')
FROM [dbo].[Customers] t1
GROUP BY [Customer_Name]
Using the above code, the Message
are separated by ,
but i want a new line. i try to use CHAR(13)+CHAR(10)
but i getting #x0D;
and the merge column seems to be wrong.
Any idea on how to fix it will greatly appreciate.
Answer using @Larnu help and posts on comments
SELECT
[Customer_Name],
STUFF((SELECT
(CHAR(13) + CHAR(10)) + LTRIM(RTRIM([Message]))
FROM [Customers] t2
WHERE t2.[Customer_Name] = t1.[Customer_Name]
FOR XML PATH (''),TYPE
).value('(./text())[1]','varchar(MAX)'),1,2,'')
FROM [Customers] t1
GROUP BY [Customer_Name]