您好,我需要将此查询从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
。
答案 0 :(得分:0)
我个人将使用字符串拆分功能来解决此问题,该功能在逗号分隔的字符串之一中寻找适当的selfServe:...:completed
。如果您使用的是SQL Server的较新版本,则可以使用内置的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 |
+----+-----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+