0

您好,我需要将此查询从 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

4

1 回答 1

0

selfServe:...:completed就我个人而言,我会使用一个字符串拆分函数来解决这个问题,该函数在一个逗号分隔的字符串中寻找合适的。如果您使用的是相当现代的 SQL Server 版本,则可以使用内置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数据类型的基于拆分器。

当您在我假设不允许您在数据库中创建新的表值函数的 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 |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
于 2019-06-25T11:42:55.973 回答