“=”/like 子句中存储过程中的 REGEX
REGEX in stored procedure in "="/like clause
我创建了一个包含 3 个内部联接的存储过程(请不要评判我 :D):
SELECT
T.Name, A.Value
FROM
Table AS T
INNER JOIN
TableAnotherTable AS TA ON TA.ID_1 = T.ID_1
INNER JOIN
AnotherTable AS A ON A.ID_2 = TA.ID_2
INNER JOIN
EndTable AS ET ON ET.Value = A.Value
TableAnotherTable
是 Table
和 AnotherTable
之间的关联 table。因此,此存储过程在某些情况下有效(如果 ET.Value
等于 等于 到 A.Value
)。
但是如果:
ET.Value = 'SOME\THING\RIGHT\<ABC>\THERE'
A.Value= 'SOME\THING\RIGHT\{DEFGHILM}\THERE'
存储过程也必须有效。
如何将内部连接子句(带有“=”)的最后一个 ON
更改为:
Regex("pattern1", E.Value) like Regex("pattern", A.Value)
其中 pattern
和 pattern1
是 ({.*?})
或 (<.*?>)
?
谢谢
示例:
ET.Value = 'HI'
A.Value = 'HI'
true
ET.Value = 'SOME\THING\RIGHT\<ABC>\THERE'
A.Value = 'SOME\THING\RIGHT\{DEFGHILMNOP}\THERE'
true
ET.Value = 'HOME\SWEET\HOME\<12345>\'
A.Value = 'HOME\SWEET\HOME\{4875928346}\'
true
ET.Value = 'EXAMPLE\<1234>'
A.Value = 'EG\{1234}'
false
ET.Value = 'dog'
A.Value = 'cat'
false
您可以比较没有 <> 和 {} 之间部分的文本:
这里是连接条件:
LEFT(ET.VALUE,CHARINDEX('<', ET.VALUE)-1) + RIGHT(ET.VALUE,LEN(ET.VALUE)- CHARINDEX('>', ET.VALUE))
=
LEFT(A.Value,CHARINDEX('{', A.Value)-1) + RIGHT(A.Value,LEN(A.Value)- CHARINDEX('}', A.Value))
但它在性能方面可能很糟糕,因为您转换了用于加入 table 的密钥。
很遗憾,SQL 服务器中没有正则表达式替换功能。您可以获得的最接近的方法是使用 PATINDEX
找到 <{
和 }>' characters and
STUFF` 之间的所有内容:
SELECT t.*, CASE WHEN val1_new = val2_new THEN 'match' END
FROM (VALUES
('SOMETHING\<1>\SOMETHING', 'SOMETHING\{2}\SOMETHING'),
('SOMETHING\BLABLA\<1>\BLA', 'SOMETHING\BLABLA\{2}\BLA'),
('SOME\<1>\THING\BLA', 'SOMETHING\{2}\BLABLA'),
('dog', 'dog')
) AS t(val1, val2)
CROSS APPLY (
SELECT
PATINDEX('%[<{]%', val1) AS val1_pos1,
PATINDEX('%[>}]%', val1) AS val1_pos2,
PATINDEX('%[<{]%', val2) AS val2_pos1,
PATINDEX('%[>}]%', val2) AS val2_pos2
) AS ca1
CROSS APPLY (
SELECT
CASE WHEN val1_pos1 > 0 AND val1_pos2 > 0 THEN STUFF(val1, val1_pos1, val1_pos2 - val1_pos1 + 1, '') ELSE val1 END,
CASE WHEN val2_pos1 > 0 AND val2_pos2 > 0 THEN STUFF(val2, val2_pos1, val2_pos2 - val2_pos1 + 1, '') ELSE val2 END
) AS ca3(val1_new, val2_new)
我创建了一个包含 3 个内部联接的存储过程(请不要评判我 :D):
SELECT
T.Name, A.Value
FROM
Table AS T
INNER JOIN
TableAnotherTable AS TA ON TA.ID_1 = T.ID_1
INNER JOIN
AnotherTable AS A ON A.ID_2 = TA.ID_2
INNER JOIN
EndTable AS ET ON ET.Value = A.Value
TableAnotherTable
是 Table
和 AnotherTable
之间的关联 table。因此,此存储过程在某些情况下有效(如果 ET.Value
等于 等于 到 A.Value
)。
但是如果:
ET.Value = 'SOME\THING\RIGHT\<ABC>\THERE'
A.Value= 'SOME\THING\RIGHT\{DEFGHILM}\THERE'
存储过程也必须有效。
如何将内部连接子句(带有“=”)的最后一个 ON
更改为:
Regex("pattern1", E.Value) like Regex("pattern", A.Value)
其中 pattern
和 pattern1
是 ({.*?})
或 (<.*?>)
?
谢谢
示例:
ET.Value = 'HI'
A.Value = 'HI'
true
ET.Value = 'SOME\THING\RIGHT\<ABC>\THERE'
A.Value = 'SOME\THING\RIGHT\{DEFGHILMNOP}\THERE'
true
ET.Value = 'HOME\SWEET\HOME\<12345>\'
A.Value = 'HOME\SWEET\HOME\{4875928346}\'
true
ET.Value = 'EXAMPLE\<1234>'
A.Value = 'EG\{1234}'
false
ET.Value = 'dog'
A.Value = 'cat'
false
您可以比较没有 <> 和 {} 之间部分的文本:
这里是连接条件:
LEFT(ET.VALUE,CHARINDEX('<', ET.VALUE)-1) + RIGHT(ET.VALUE,LEN(ET.VALUE)- CHARINDEX('>', ET.VALUE))
=
LEFT(A.Value,CHARINDEX('{', A.Value)-1) + RIGHT(A.Value,LEN(A.Value)- CHARINDEX('}', A.Value))
但它在性能方面可能很糟糕,因为您转换了用于加入 table 的密钥。
很遗憾,SQL 服务器中没有正则表达式替换功能。您可以获得的最接近的方法是使用 PATINDEX
找到 <{
和 }>' characters and
STUFF` 之间的所有内容:
SELECT t.*, CASE WHEN val1_new = val2_new THEN 'match' END
FROM (VALUES
('SOMETHING\<1>\SOMETHING', 'SOMETHING\{2}\SOMETHING'),
('SOMETHING\BLABLA\<1>\BLA', 'SOMETHING\BLABLA\{2}\BLA'),
('SOME\<1>\THING\BLA', 'SOMETHING\{2}\BLABLA'),
('dog', 'dog')
) AS t(val1, val2)
CROSS APPLY (
SELECT
PATINDEX('%[<{]%', val1) AS val1_pos1,
PATINDEX('%[>}]%', val1) AS val1_pos2,
PATINDEX('%[<{]%', val2) AS val2_pos1,
PATINDEX('%[>}]%', val2) AS val2_pos2
) AS ca1
CROSS APPLY (
SELECT
CASE WHEN val1_pos1 > 0 AND val1_pos2 > 0 THEN STUFF(val1, val1_pos1, val1_pos2 - val1_pos1 + 1, '') ELSE val1 END,
CASE WHEN val2_pos1 > 0 AND val2_pos2 > 0 THEN STUFF(val2, val2_pos1, val2_pos2 - val2_pos1 + 1, '') ELSE val2 END
) AS ca3(val1_new, val2_new)