-3

How to separate values by new line and split to new row in SQL Server. I have the following data

Ticket Agent
98765 Agent1 Agent2 Agent3
12345 Agent4 Agent5 Agent6

Or as below

enter image description here

Now I have to convert it into the following -

Ticket Agent
98765 Agent1
98765 Agent2
98765 Agent3
12345 Agent4
12345 Agent5
12345 Agent6
noswear
  • 311
  • 1
  • 6
  • 27

1 Answers1

1

Use string_split():

select t.ticket, s.value as agent
from t cross apply
     string_split(t.agent, '
') s
order by s.value;

Here is a db<>fiddle.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786