0

The link below will be helpful to understand my question. As pooja mentioned about her output like a concatenated string, is it possible to get a new line after each concatenation like

1  Editor
   Reviewer
7  EIC
   Editor
   Reviewer

the roles should be in a single cell though.

I have tried using char(13) and char(10), but there is always something mismatch with SQL Server 2012 I am using: How to concatenate values with same id in sql

Can anyone please help?

Thanks!

Community
  • 1
  • 1
krits
  • 68
  • 1
  • 9

1 Answers1

0
DECLARE @T TABLE (id int,
                  name NVARCHAR(50))

INSERT INTO @T 
VALUES(1,'JAMES'),
(1,'HARRY'),
(2,'CO'),
(2,'VT'),
(2,'SC')
;

SELECT id,STUFF((SELECT DISTINCT(','+B.name) 
              FROM @T B
              WHERE B.id = A.id
              FOR XML PATH('')),1,1,'' ) AS name
FROM @T A
GROUP BY id
;
Krish
  • 39
  • 8