我正在将使用正则表达式的CLR函数转换为SQL函数。我知道SQL Server不完全支持正则表达式,但是我只需要一种情况就可以搜索单词。
搜索字段值: {"Id":1234, "Title": "The quick brown"}
.NET中的正则表达式模式: '\b' + '"Id":1234' '\b'
我希望能够匹配时,搜索字段包含“身份证”:1234,但没有“身份证”:12345
我该如何实现?我已经尝试过了,但是当数字1234以空格或b而不是逗号结尾时,显然不起作用。
DECLARE @match bit = 0
DECLARE @input nvarchar(max) = '{"Id":1234, "Title": "The quick brown"}'
DECLARE @pattern nvarchar(max) ='"Id":1234'
SELECT @match = 1 FROM (VALUES ('')) table1 (column1)
WHERE @Input LIKE '%' + @pattern + ',%'
我认为模式'%"Id":1234[^a-zA-Z0-9]%'
可以。
它使用否定的字符类[^a-zA-Z0-9]
,其作用与常规正则表达式:)
样品:
declare @tbl table (col varchar(100));
insert into @tbl values
('{"Id":1234, "Title": "The quick brown"}'),
('{"Id":1234, "Title": "The quick brown"}'),
('Id":1234, "Title": "The quick brown"}'),
('{"Id":12345, "Title": "The quick brown"}');
select *
from @tbl
where col like '%"Id":1234[^a-zA-Z0-9]%'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句