Solved

Split function

Posted on 2013-02-01
4
195 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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:jonnidip
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
Because it worked.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now