2

我尝试过使用 patindex 和 charindex,但似乎它们都不会轻易地做我想做的事。Charindex 一次只想搜索一个符号,而 patindex 不允许“搜索自”索引,不允许我循环查找符号的所有索引。我有哪些选择?

4

1 回答 1

2

由于没有关于您的“特殊符号”是什么、如何存储以及您期望的输出的具体细节,这涉及到一些猜测工作,但我认为无论如何都可以应用我的回答的基本原则。获得所有事件的关键是使用递归 CTE 和OUTER APPLY. 每次 CTE 循环时,它都会用空格替换一个特殊字符,直到没有特殊字符为止,同时存储字符的位置。

样本数据:

DECLARE @SpecialSymbols TABLE (Symbol CHAR(1) NOT NULL PRIMARY KEY)
INSERT @SpecialSymbols VALUES ('@'), ('.'), ('['), (']')

DECLARE @TestData TABLE (StringToTest VARCHAR(100))
INSERT @TestData VALUES 
    ('test 1 [Using Square Brackets]'), 
    ('[Test2@EmailAddress.com]'), 
    ('No Special Symbols')

实际查询

;WITH CTE AS
(   SELECT  *, STUFF(StringToTest, Position, 1, ' ') [ReworkedString]
    FROM    @TestData
            OUTER APPLY
            (   SELECT  CHARINDEX(Symbol, StringToTest) [Position], Symbol
                FROM    @SpecialSymbols
            ) Symbols
    WHERE   Position > 0
    UNION ALL
    SELECT  StringToTest, Symbols.Position, Symbols.Symbol, STUFF(ReworkedString, Symbols.Position, 1, ' ') [ReworkedString]
    FROM    CTE
            OUTER APPLY
            (   SELECT  CHARINDEX(Symbol, ReworkedString) [Position], Symbol
                FROM    @SpecialSymbols
                WHERE   Symbol = CTE.Symbol
            ) Symbols
    WHERE   Symbols.Position > 0
)

-- CTE NOW LOOKS LIKE:
--  | test 1 [[Using Square Brackets]   |   8   |   [   | test 1  [Using Square Brackets]
--  | test 1 [[Using Square Brackets]   |   30  |   ]   | test 1 [[Using Square Brackets 
--  | [Test2@EmailAddress.com]          |   20  |   .   | [Test2@EmailAddress com]
--  | [Test2@EmailAddress.com]          |   7   |   @   | [Test2 EmailAddress.com]
--  | [Test2@EmailAddress.com]          |   1   |   [   |  Test2@EmailAddress.com]
--  | [Test2@EmailAddress.com]          |   24  |   ]   | [Test2@EmailAddress.com 
--  | test 1 [[Using Square Brackets]   |   9   |   [   | test 1   Using Square Brackets]

SELECT  a.StringToTest, COALESCE(Location, '') [SpecialSymbolLocations]
FROM    @TestData a
        LEFT JOIN
        (   SELECT  DISTINCT
                    StringToTest,
                    -- THIS MERELY CONCATENATES ROWS INTO COLUMNS TO GET COMMA SEPARATED LIST
                    STUFF(( SELECT  ', ' + CONVERT(VARCHAR, Position)
                            FROM    CTE b
                            WHERE   a.StringToTest = b.StringToTest
                            ORDER BY Position
                            FOR XML PATH('')
                    ), 1, 2, '') [Location] 
            FROM    CTE a
        ) b
            ON a.StringToTest = b.StringToTest

CTE 可以随心所欲地进行操作,但是为了完整起见,我添加了一个使用SQL 服务器 XML 扩展的最终查询,将特殊符号的位置连接到一个逗号分隔的列表中,并将它们放在每个原始的旁边字符串。所以最终输出如下:

| StringToTest                      | SpecialSymbolLocations    |
|-----------------------------------|---------------------------|
| test 1 [[Using Square Brackets]   | 8, 9, 31                  |
| [Test2@EmailAddress.com]          | 1, 7, 20, 24              |
| No Special Symbols                |                           |
于 2012-04-18T10:21:23.347 回答