0

I need to turn

Key     Location
123456  UK,France,Italy

into

Key    Location
123456 UK
123456 France
123456 Italy

Any ideas? Thanks

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Paul
  • 9
  • 2

1 Answers1

2

Typically, assuming SQL Server 2016 or better, this is:

SELECT t.[Key], Location = s.value
  FROM dbo.TableName AS t
  CROSS APPLY STRING_SPLIT(t.Location, N',') AS s;

If you're on < 2016 (or >= 2016 but < 130 compatibility level), you'll need to create a separate inline TVF to handle this (example forthcoming if necessary).

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490