Solved

Split function

Posted on 2013-02-01
4
199 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

733 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