Get data between commas

Hi

I have data like this

Id        Products
1         Product1;product2

Separate by semi colon

I'd like the data like this

1 product1
1 product2

Etc
LVL 1
Auerelio VasquezETL DeveloperAsked:
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.

Shalu MCommented:
There are many ways you can do this depending on the version of SQL Server you're using.

SQL Server 2008 -

DECLARE @xml xml, @str varchar(100), @delimiter varchar(10)
SET @str = 'product1;product2'
SET @delimiter = ';'
SET @xml = cast(('<X>'+replace(@str, @delimiter, '</X><X>')+'</X>') as xml);

with data as (
SELECT '1' AS PRODUCT_ID, C.value('.', 'varchar(10)') as PRODUCT_NAME FROM @xml.nodes('X') as X(C)
)
select * from data;

Open in new window


Here's a reference - http://www.sqlservercentral.com/articles/XML/66932/

In SQL server 2016 there are inbuilt functions for splitting a string so you could try this code -

WITH DATA AS(
	SELECT '1' AS PRODUCT_ID, TRY_CAST(value AS VARCHAR) AS PRODUCT_NAME
	FROM   STRING_SPLIT ('product1;product2', ';') 
)
SELECT * FROM DATA;

Open in new window

Auerelio VasquezETL DeveloperAuthor Commented:
This is a bit difficult to follow. Can you do it from the perspective that product is the column name and there may be more than one product in the column. Also Id is not always 1

It may be like this

ID        Products
1          Prod1, prod2, product3, prodn
2./
Auerelio VasquezETL DeveloperAuthor Commented:
Sorry I'd like the outcome to look like this

1 product1
1 product2
2 product3
2 product4
CEOs need to know what they should worry about

Nearly every week during the past few years has featured a headline about the latest data breach, malware attack, ransomware demand, or unrecoverable corporate data loss. Those stories are frequently followed by news that the CEOs at those companies were forced to resign.

Auerelio VasquezETL DeveloperAuthor Commented:
By the way products is a column in a table called events.

So I want to solution to use the table and column name
Nitin SontakkeDeveloperCommented:
Hopefully you already have a script for a split function.

You can query it as follows:

select Id, s.data
from events e
outer apply dbo,split(e.Products, ',') s

Open in new window

SharathData EngineerCommented:
/*
create table test1(id int, Products varchar(100))
insert into test1 values (1, 'Prod1, prod2, product3, prod4')
*/
SELECT id, ltrim(td.value('.', 'NVARCHAR(255)')) AS coverage
  FROM test1
CROSS APPLY (SELECT  CAST(('<td><![CDATA[' + REPLACE(Products, ',',']]></td><td><![CDATA[') + ']]></td>') AS XML) AS XmlData) ToXml
CROSS APPLY XmlData.nodes('td') A ( td );
/*
  	id	coverage
1	1	Prod1
2	1	prod2
3	1	product3
4	1	prod4
*/

Open in new window

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