Split data that is between Commas

Hi I would like to do this:

I have a column that has data stored in two columns Id, topic

It could look like this

ID.    Topic
1.      ABC, def, this

What I want returned is

1 abc
1 def
1 this

I've tried parsebame but it doesn't do exactly this

Thanks for help !
LVL 1
Auerelio VasquezETL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
first defined a Table-valued function such as:

Create function [dbo].[SplitString] 
(
    @str nvarchar(max), 
    @separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
    select 
        cast(1 as bigint), 
        cast(1 as bigint), 
        charindex(@separator, @str)
    union all
    select
        p + 1, 
        b + 1, 
        charindex(@separator, @str, b + 1)
    from tokens
    where b > 0
)
select
    p-1 ItemIndex,
    substring(
        @str, 
        a, 
        case when b > 0 then b-a ELSE LEN(@str) end) 
    AS Item
from tokens
);

Open in new window

and then call it like:

declare @yourTable table
(
	ID int,
	Topic Varchar(100)
)
insert into @yourTable values
(1, 'ABC, def, this'),
(2, '123, 456, 789, hello, world');

select a.ID, ltrim(rtrim(b.Item)) Topic
from @yourTable a
outer apply dbo.SplitString(a.Topic,',') b

Open in new window

0

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
Nitin SontakkeDeveloperCommented:
And just FYI, if you have SQL Server 2016, there is built-in function string_split.
0
SharathData EngineerCommented:
try this.
create table table1 (id int, topic varchar(100));
insert into table1 values (1, 'abc,def,this'),(2, 'abc,this');

SELECT id, ltrim(td.value('.', 'NVARCHAR(255)')) AS topic
  FROM table1
CROSS APPLY (SELECT  CAST(('<td><![CDATA[' + REPLACE(topic, ',',']]></td><td><![CDATA[') + ']]></td>') AS XML) AS XmlData) ToXml
CROSS APPLY XmlData.nodes('td') A ( td );

/*
  	id	topic
1	1	abc
2	1	def
3	1	this
4	2	abc
5	2	this

*/

Open in new window

0
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

From novice to tech pro — start learning today.