tsql函数拆分字符串

时间:2009-10-20 13:36:23

标签: sql sql-server tsql

我想知道是否有人可以帮助我。

我需要一个tsql函数来分割给定的值,例如:

1)    00 Not specified
3)    01-05 Global WM&BB | Operations
2)    02-05-01 Global WM&BB | Operations | Operations n/a

我需要得到这样的结果:

cat1  cat1descr       cat2    cat2descr     cat3   cat3descr
----------------------------------------------------------------
00    Not especified  null    null          null   null
01    Global WM&BB    05      Operations    null   null
01    Global WM&BB    05      Operations    01     Operations n/a

结果总是有6列

选择funcX('00未指定');

cat1  cat1descr      cat2  cat2descr      cat3  cat3descr
----------------------------------------------------------------
00    Not especified  null  null          null   null

3 个答案:

答案 0 :(得分:6)

这将适用于SQL Server 2005和SQL Server 2008.我假设您的第一个数字序列固定为2位数的1,2或3组。您可以使用更少的级联CTE执行此操作,但我发现SUBSTRING / CHARINDEX / LEN语法很快就会变得非常难以阅读和调试。

DECLARE @foo TABLE
(
    bar VARCHAR(4000)
);

INSERT @foo(bar) SELECT '00 Not specified'
UNION ALL SELECT '01-05 Global WM&BB | Operations'
UNION ALL SELECT '02-05-01 Global WM&BB | Operations | Operations n/a';


WITH split1 AS
(
    SELECT 
        n = SUBSTRING(bar, 1, CHARINDEX(' ', bar)-1),
        w = SUBSTRING(bar, CHARINDEX(' ', bar)+1, LEN(bar)),
        rn = ROW_NUMBER() OVER (ORDER BY bar)
    FROM
        @foo
),
split2 AS
(
    SELECT
        rn,
        cat1 = LEFT(n, 2),
        wl = RTRIM(SUBSTRING(w, 1, 
             COALESCE(NULLIF(CHARINDEX('|', w), 0)-1, LEN(w)))),
        wr = LTRIM(SUBSTRING(w, NULLIF(CHARINDEX('|', w),0) + 1, LEN(w))),
        cat2 = NULLIF(SUBSTRING(n, 4, 2), ''),
        cat3 = NULLIF(SUBSTRING(n, 7, 2), '')
    FROM
        split1
),
split3 AS
(
    SELECT
        rn,
        cat1descr = wl,
        cat2descr = RTRIM(SUBSTRING(wr, 1, 
              COALESCE(NULLIF(CHARINDEX('|', wr), 0)-1, LEN(wr)))),
        cat3descr = LTRIM(SUBSTRING(wr, 
              NULLIF(CHARINDEX('|', wr),0) + 1, LEN(wr)))
    FROM 
        split2
)
SELECT
    s2.cat1, s3.cat1descr,
    s2.cat2, s3.cat2descr,
    s2.cat3, s3.cat3descr
 FROM split2 AS s2
INNER JOIN split3 AS s3
ON s2.rn = s3.rn;

答案 1 :(得分:0)

您可以使用PatIndex和SubString

执行此操作

答案 2 :(得分:0)

如果@In是您要解析的字符串的值,请尝试以下操作:

   Select 
    Case When firstDash = 0 Then zz.sKey 
         Else left(zz.sKey, FirstDash-1) End cat1,
      Ltrim(RTrim(Case When firstPipe = 0 Then zz.Vals
         Else Left(zz.Vals, firstPipe -1) End)) ca1Desc,
    Case When firstDash = 0 Then Null
         When secondDash = 0 
            Then SubString(zz.sKey, FirstDash+1, Len(zz.skey))
       Else SubString(zz.sKey, FirstDash+1, secondDash-firstDash-1) End cat2,
      Ltrim(RTrim(Case When firstPipe = 0 Then Null
         When secondPipe = 0 
            Then SubString(zz.Vals, firstPipe+1, Len(zz.Vals))
       Else SubString(zz.Vals, firstPipe+1, 
                      secondPipe-firstPipe-1) End)) cat2Desc,
    Case When secondDash > 0 
         Then Substring(zz.sKey, secondDash+1, len(sKey)-seconddash) End cat3,
            Ltrim(RTrim(Case When secondPipe > 0 
                Then Substring(zz.Vals, secondPipe+1, 
                     len(Vals)-secondPipe) End)) cat3Desc
   From (Select Z.sKey, Z.Vals,
           charIndex('-', Z.skey) firstDash,
           charIndex('-', Z.skey, 1 + charIndex('-', Z.skey)) secondDash,
           charIndex('|', Z.Vals) firstPipe,
        charIndex('|', Z.Vals, 1 + charIndex('|', Z.Vals)) secondPipe
      From  (Select Left(@In, CharIndex(' ', @In)-1) skey,
              substring(@In, CharIndex(' ', @In)+ 1, Len(@In)) vals) Z) ZZ