Solved

Split function

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 30
VBScript Write Column Headers 3 37
insert wont work in SQL 14 21
SQL Query assistance 16 23
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

770 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