Solved

Split function

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: Subtracting Amounts from "Among Rows" 3 43
VS2015 Redefinition errors 4 29
Add '#' to end of file 2 30
t-sql split string into multiple rows 7 35
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

910 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

20 Experts available now in Live!

Get 1:1 Help Now