We help IT Professionals succeed at work.

Get data between commas

77 Views
Last Modified: 2017-04-09
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
Comment
Watch Question

Commented:
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 Developer

Author

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 Developer

Author

Commented:
Sorry I'd like the outcome to look like this

1 product1
1 product2
2 product3
2 product4
Auerelio VasquezETL Developer

Author

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 SontakkeDeveloper
CERTIFIED EXPERT

Commented:
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

Data Engineer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions