0

I have a table that looks like this:

Emails                                                   | Data
---------------------------------------------------------------------------------
userA@email.com;userB@email.com;userC@email.com          | Foo
userB@email.com                                          | Bar

I want to parse out the delimited emails into their own rows such that it looks something like this:

   Emails                                                   | Data
    ---------------------------------------------------------------------------------
    userA@email.com                                          | Foo
    userB@email.com                                          | Foo
    userC@email.com                                          | Foo
    userB@email.com                                          | Bar

I know there is a string_split function, but it would only work on the first column. I need some kind of join for this.

EDIT: Yes I know it breaks normal form, but bigquery for instance has an "unnest" function and has arrays as a datatype.

Agneum
  • 727
  • 7
  • 23

1 Answers1

3

In the more recent versions of SQL Server, you can use string_split():

select s.value as email, t.data
from t cross apply
     string_split(t.emails, ';') s;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786