Sample data:
id email_address email_new
-------------------------------------------------------------------------------
1 [email protected] [email protected]
2 [email protected]; [email protected] [email protected]
2 [email protected]; [email protected] [email protected]
3 [email protected]; [email protected]; [email protected] [email protected]
3 [email protected]; [email protected]; [email protected] [email protected]
3 [email protected]; [email protected]; [email protected] [email protected]
I used the following query to split a '; ' separated string into rows:
SELECT
id, email_address, email_new
FROM
(SELECT
id, email_address,
Split.a.value('.', 'NVARCHAR(max)') AS email_new
FROM
(SELECT
id, email_address, CAST ('<M>' + REPLACE(email_address, '; ', '</M><M>') + '</M>' AS XML) email_new
FROM
table) AS A
CROSS APPLY
email_new.nodes ('/M') AS Split(a)) x
GROUP BY
id, email_address, email_new
Problem with my query is that I don't want to create a new row for every split email address- I would like to add a new column for it.. Ideally, something along the lines of this:
id email_1 email_2 email_3
------------------------------------------------------------------------------
1 [email protected] null null
2 [email protected] [email protected] null
3 [email protected] [email protected] [email protected]
There are as many as 3 separated email addresses in email_address column. Any suggestions? In case it grows beyond 3- it is best if answer could account for n columns.
substring(), also split it with the ID and you can just pivot it back into columns'; 'email_address? 3 maximum