子串和Charindex

时间:2016-05-12 04:28:30

标签: sql sql-server

我在一个包含食品的餐桌上,食品可以分开;或者,字段大小可以是不同的长度。

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

这是我对第一行所做的,但是有更简单的方法吗?

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)

1 个答案:

答案 0 :(得分:1)

您可能希望为此使用字符串拆分器。这是来自Aaron Bertrand的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

您的最终查询将是:

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

另一种方法是使用基于模式的分割器。这是来自Dwain Camp的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

您的最终查询将是:

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

ONLINE DEMO