I need to combine rows where Recid is the same, but using a carriage return/linefeed between row values, rather than a comma.
See example data below:
create table #Temp
(
Companyid nvarchar(2),
Recid nvarchar(1),
Notes nVarchar(max)
)
insert into #Temp
Values (
'A','1','NotesA1'),
('A','1','NotesA11'),
('A','1','NotesA111'),
('A','2','NotesA2'),
('A','2','NotesA22'),
('B','1','NotesB1')
select Recid, Notes from #Temp where Companyid='A'
drop table #Temp
How do I end up with the below:
Recid Notes
1 NotesA1
NotesA11
NotesA111
2 NotesA2
NotesA22
I have tried using the following but it returns semi-colons between values:
create table #Temp
(
Companyid nvarchar(2),
Recid nvarchar(1),
Notes nVarchar(max)
)
insert into #Temp
Values (
'A','1','NotesA1'),
('A','1','NotesA11'),
('A','1','NotesA111'),
('A','2','NotesA2'),
('A','2','NotesA22'),
('B','1','NotesB1')
SELECT dr.Recid,
STUFF((SELECT '; ' + cast(US.notes as nvarchar(max))
FROM #Temp US
WHERE US.Recid = DR.Recid and us.COMPANYID='A'
FOR XML PATH('')), 1, 1, '') [Notes]
FROM #Temp DR
where (dr.Companyid='A')
GROUP BY DR.Recid
ORDER BY dr.Recid
drop table #Temp
Returns:
Recid Notes
1 NotesA1;NotesA11;NotesA111
2 NotesA2;NotesA22
Cheers for all help!