SQL Update table & Split one column into multiple rows

Please help!, I know how to spilt data from one column (seperated by ',')  and create new multiple rows, but how can I update the table at the same time?


I have a table with a few columns and in one of the columns (Col3) it has Emails seperated by a ',' like:

Col1   Col2              Col3  
----    -----               -----
1       ABC Ltd          pjones@ZZZ.com, wsmith@ABC.com
2       Compx Ltd      gbrown@xxx.com, Wills@xxx.com

I am using the following SQL code to split the emails and create new rows (with other columns retaining the same data):

Select *  FROM dbo.Table1  cross apply dbo.split(Col3,',' )

The SQL output is the following with new cols ID and Data that SQL inserts:

Col1             Col2              Col3                                                   Id              Data
----               -----               -----                                                     -----            -----
London       ABC Ltd          pjones@ZZZ.com, wsmith@ABC.com          1                pjones@ZZZ.com
London       Compx Ltd      gbrown@xxx.com, Wills@xxx.com              2               gbrown@xxx.com
London       ABC Ltd          pjones@ZZZ.com, wsmith@ABC.com          1                wsmith@ABC.com          
London       Compx Ltd      gbrown@xxx.com, Wills@xxx.com              2                Wills@xxx.com

But how do I Update Col3 with the new values from the Data column?, I have tried:

 update dbo.Table1
 set Col3 = F.Data, Col2 = Col2, Col1 = Col1
 Select *
 FROM dbo.Table1  cross apply dbo.split(Col,',' ) as F
 where f.id = f.id

Any help would be much appreciated!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
create a temp table first

select * into #tmp
(Select *  FROM dbo.Table1  cross apply dbo.split(Col3,',' )) x

then delete all

delete from myTable

insert into myTable select * from #tmp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HainKurtSr. System AnalystCommented:
(fix for above code) actually this one:

insert into myTable select * from #tmp
insert into myTable
select Col1,Col2, Data from #tmp
HainKurtSr. System AnalystCommented:
you cannot use update since you are duplicating the number of records...
say you have 3 records, and after these operations you will have 5 records
so, you cannot achieve it just by an update statement...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.