Solved

Split function

Posted on 2013-02-01
4
200 Views
Last Modified: 2013-06-13
I need help with row of data in the format below.

Before
Product        Ingredients                
xxxxxxxxxxxx   Each capsule contains:  Flaxseed Oil  1000 mg, providing Omega 3 Alpha Linolenic Acid 500 mg. Other Ingredients: Carob, Gelatin, Glycerin, Lecithin, Soybean Oil, Titanium Dioxide, Vegetable Shortening, Yellow Beeswax.
yyyyyyyyyyyy   Six tablets contain:  Calcium  500 mg •  Magnesium  250 mg •  Potassium  99 mg •  Manganese  10 mg •  Iron  18 mg •  Silicon  2 mg •  Zinc  15 mg •  Iodine  (Kelp) 150 mcg •  Chromium  200 mcg •  Selenium  25 mcg •  Molybdenum  150 mcg &#8226

After split
Product          Ingredients  
xxxxxxxxxxxx Flaxseed Oil
xxxxxxxxxxxx Omega 3 Alpha Linolenic Acid
xxxxxxxxxxxx Carob
xxxxxxxxxxxx Gelatin
xxxxxxxxxxxx Glycerin
xxxxxxxxxxxx Lecithin
xxxxxxxxxxxx Soybean Oil
xxxxxxxxxxxx Titanium Dioxide
xxxxxxxxxxxx Vegetable Shortening
yyyyyyyyyyyy Yellow Beeswax
yyyyyyyyyyyy  Calcium
yyyyyyyyyyyy  Magnesium
yyyyyyyyyyyy  Potassium
yyyyyyyyyyyy  Manganese
yyyyyyyyyyyy  Iron
yyyyyyyyyyyy  Silicon
yyyyyyyyyyyy  Zinc
yyyyyyyyyyyy  Iodine
yyyyyyyyyyyy  Chromium
yyyyyyyyyyyy  Selenium
yyyyyyyyyyyy  Molybdenum
0
Comment
Question by:olongus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:jonnidip
ID: 38843872
This is the function I use.
I deployed it to the master db.

CREATE FUNCTION [dbo].[Split](
    @sInputList VARCHAR(MAX) -- List of delimited items
  , @sDelimiter CHAR(1) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(MAX))

BEGIN
DECLARE @sItem VARCHAR(MAX)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
 
 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END

Open in new window


(to be used in your example you will also need to put this function into a more complex query).
This will, at least, give you the ability to simply "split" delimited values (in your case the deliminter is a ",").
Then you may want to take into account only what comes afther the ":".

Regards.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38843899
with the function dbo.ParmsTolist (shown in this article: http://www.experts-exchange.com/Database/Miscellaneous/A_1536-delimited-list-as-parameter-what-are-the-options.html). you can implement like this:
select p.Product      , f.value
 from yourtable p
 cross apply dbo.ParmsTolist( replace(p.Ingredients      , '.', ',' )   , ',' ) f
 

Open in new window

at least, that is the starter.
to remove those part that are like: Each capsule contains:   or the "mg" specs, you can apply more "filterning2 ...
0
 

Accepted Solution

by:
olongus earned 0 total points
ID: 38844993
Thank you angelIII, the function was very much helful, but how do I remove the additional info which are attached to the ingredients?
0
 

Author Closing Comment

by:olongus
ID: 39243881
Because it worked.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question