将 Oracle 正则表达式查询转换为 MSSQL

Translate Oracle Regular Expression Query to MSSQL

您好,我需要将此查询从 Oracle 数据库转换为 MSSQL 并获得完全相同的结果:

WHEN REGEXP_LIKE(E.EVENTS, 'selfServe:[^:]*:completed[:]').

我的以下尝试都失败了:

WHEN EVENTS LIKE '%[:]selfServe[:][^:]%[:]completed[:]%'

EVENTS LIKE '%[:]selfServe[:]%[^:][:]completed[:]%'

WHERE PATINDEX('selfServe:[^:]*:completed[:]', EVENTS) != 0

WHERE PATINDEX('selfServe:[^:]%:completed[:]', EVENTS) != 0 .

示例:

这不应该匹配:

OpenQ,
Payment,
Payment:selfServe:Payment-Cancel_Scheduled:initiated::,
Payment:authentication:Authentication:initiated::,
Payment:authentication:Authentication:completed::,
HUP

虽然这应该匹配:

OpenQ2,
Payment,
Payment:selfServe:Payment:initiated::,
Payment:authentication:Authentication:initiated::,
Payment:authentication:Authentication:initiated::,
Payment:authentication:Authentication:completed::,
Payment:selfServe:Payment:completed::,
HUP

在第一种情况下,我有 authentication:completed 但没有 selfServe:Payment:completed

就我个人而言,我会使用一个字符串拆分函数来解决这个问题,该函数在一个逗号分隔的字符串中寻找合适的 selfServe:...:completed。如果您使用的是相当现代的 SQL 服务器版本,您可以使用内置的 string_split 函数来完成此操作:

string_split

declare @t table(id int,t varchar(1000));
insert into @t values
 (1,'OpenQ,Payment,Payment:selfServe:Payment-Cancel_Scheduled:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,HUP')
,(2,'OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP');


select t.id
      ,s.value
      ,t.t
from @t as t
    cross apply string_split(t.t,',') as s
where case when patindex('%:selfServe:%',s.value) > 0
                and patindex('%:completed:%',s.value) > 0
            then 1
            else 0
            end = 1;

输出

+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id |                 value                 |                                                                                                                          t                                                                                                                           |
+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  2 | Payment:selfServe:Payment:completed:: | OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP |
+----+---------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

如果没有,您将需要推出自己的字符串拆分器。我将假设您这里可能有一些相当长的字符串(超过 4000 个字符),因此我使用基于 XML 的拆分器,它适用于 max 数据类型。

当您在我假设不允许您在数据库中创建新的 Table Value Function 的 BI 工具中执行此操作时,您将需要一个相当复杂的语句来处理您的数据,即内联字符串拆分器:

自己动手

declare @t table(id int,t varchar(1000));
insert into @t values
 (1,'OpenQ,Payment,Payment:selfServe:Payment-Cancel_Scheduled:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,HUP')
,(2,'OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP');

with s as
(       -- Convert the string to an XML value, replacing the delimiter with XML tags
    select id
          ,t
          ,convert(xml,'<x>' + replace((select '' + t for xml path('')),',','</x><x>') + '</x>').query('.') as s
    from @t
)
select id
        ,item
        ,t     -- Select the values from the generated XML value by CROSS APPLYing to the XML nodes
from(select id
            ,t
            ,n.x.value('.','nvarchar(max)') as item
    from s
            cross apply s.nodes('x') as n(x)
    ) a
where case when patindex('%:selfServe:%',a.item) > 0
                and patindex('%:completed:%',a.item) > 0
            then 1
            else 0
            end = 1;

输出

+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id |                  item                   |                                                                                                                          t                                                                                                                           |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  2 |   Payment:selfServe:Payment:completed:: | OpenQ2,Payment,Payment:selfServe:Payment:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:initiated::,Payment:authentication:Authentication:completed::,Payment:selfServe:Payment:completed::,HUP |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+