-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
  • 303
  • 1
  • 5
  • 22

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,198,228
  • 53
  • 572
  • 709