我正在寻找一个RegExp来查找SQL Server和正则表达式(RegExp)中整个单词中的重复字符。例如:
"AAUGUST" match (AA)
"ANDREA" don't match (are 2 vowels "A", buit are separated)
"ELEEPHANT" match (EE)
我正在尝试:
SELECT field1
FROM exampleTable
WHERE field1 like '%([A-Z]){2}%'
但它不起作用。
我赞成你的帮助。
谢谢!
答案 0 :(得分:0)
您不能使用T-SQL LIKE
执行您的问题。
你最好的选择是使用Common Language Runtime (CLR),但也可以使用例如标量值函数来实现(尽管痛苦缓慢):
create function dbo.ContainsRepeatingAlphaChars(@str nvarchar(max)) returns bit
as begin
declare @p int, -- the position we're looking at
@c char(1) -- the previous char
if @str is null or len(@str) < 2 return 0;
select @c = substring(@str, 1, 1), @p = 1;
while (1=1) begin
set @p = @p + 1; -- move position pointer ahead
if @p > len(@str) return 0; -- if we're at the end of the string and haven't already exited, we haven't found a match
if @c like '[A-Z]' and @c = substring(@str, @p, 1) return 1; -- if last char is A-Z and matches the current char then return "found!"
set @c = substring(@str, @p, 1); -- Get next char
end
return 0; -- this will never be hit but stops SQL Server complaining that not all paths return a value
end
GO
-- Example usage:
SELECT field1
FROM exampleTable
WHERE dbo.ContainsRepeatingAlphaChars(field1) = 1
我提到它会慢吗?不要在大桌子上使用它。去CLR。