0

I am trying to write a query on SQL Server to merge data items in 1 record

Input table:

sales_ref_no    Description
001 Hello
001 Hi
002 Dear
002 All
002 Please
003 Thanks

Output table:

sales_ref_no    Description
001 Hello | Hi
002 Dear | All | Please
003 Thanks

The description under the same sales_ref_no is accumulated under the same record using a | delimiter

Can anyone help?

Thanks!

Roman Pekar
  • 107,110
  • 28
  • 195
  • 197

1 Answers1

0
Select 
  sales_ref_no,
  STUFF((
  SELECT ' | ' + B.Description
   FROM YOUR_TABLE B
   WHERE (B.sales_ref_no = A.sales_ref_no) 
   FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS Description
From YOUR_TABLE A
Group By sales_ref_no
Palanikumar
  • 6,940
  • 4
  • 40
  • 51