user3666224 user3666224 - 6 months ago 32
SQL Question

Substring and Charindex

I have field in a table that contains food items, the food items can be separated by a ; or , and field size can be of varying lengths.

"Milk - 2%; Peanut Butter and Jelly Sandwich; Fruit Cocktail"
"oatmeal cookies ; Milk - Whole"
"Peanut Butter and Jelly Sandwich"


This what I've done with the first row but is there an easier way?

DECLARE @Food VARCHAR(100)
SET @Food = 'Cottage Cheese (1/2 cup); Fruit Cocktail, apple juice'
SELECT SUBSTRING(@FOOD,1,CHARINDEX(';', @FOOD)-1)
SELECT SUBSTRING(@FOOD,27,CHARINDEX(',', @FOOD)-27)
SELECT SUBSTRING(@FOOD,42,CHARINDEX(',', @FOOD)-1)

Answer

You might want to use a string splitter for this. Here is one from Aaron Bertrand's article:

CREATE FUNCTION dbo.SplitStrings_XML
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );
GO

Your final query will be:

SELECT
    t.Id,
    LTRIM(RTRIM(c.Item))
FROM tbl t
CROSS APPLY dbo.SplitStrings_XML(t.food, ';') sc
CROSS APPLY dbo.SplitStrings_XML(sc.Item, ',') c

ONLINE DEMO


Another way is to use a Pattern-based Splitter. Here is one from Dwain Camp's article

-- PatternSplitCM will split a string based on a pattern of the form 
-- supported by LIKE and PATINDEX 
-- 
-- Created by: Chris Morris 12-Oct-2012 
CREATE FUNCTION [dbo].[PatternSplitCM]
(
       @List                VARCHAR(8000) = NULL
       ,@Pattern            VARCHAR(50)
) RETURNS TABLE WITH SCHEMABINDING 
AS 

RETURN
    WITH numbers AS (
        SELECT TOP(ISNULL(DATALENGTH(@List), 0))
            n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
        FROM
        (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
        (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
        (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
        (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n)
    )

    SELECT
        ItemNumber = ROW_NUMBER() OVER(ORDER BY MIN(n)),
        Item = SUBSTRING(@List,MIN(n),1+MAX(n)-MIN(n)),
        [Matched]
    FROM (
        SELECT n, y.[Matched], Grouper = n - ROW_NUMBER() OVER(ORDER BY y.[Matched],n)
        FROM numbers
        CROSS APPLY (
            SELECT [Matched] = CASE WHEN SUBSTRING(@List,n,1) LIKE @Pattern THEN 1 ELSE 0 END
        ) y
    ) d
    GROUP BY [Matched], Grouper

Your final query will be:

SELECT
    t.Id,
    LTRIM(RTRIM(s.Item))
FROM tbl t
CROSS APPLY dbo.PatternSplitCM(t.food, '%[;,]%') s
WHERE s.Matched = 0

ONLINE DEMO