简体   繁体   English

SQL Server 类似模式

[英]SQL Server Like Pattern

I want to match custom pattern in one of the column in a SQL Server database.我想在 SQL Server 数据库的列之一中匹配自定义模式。 The problem is I don't know the exact pattern length.问题是我不知道确切的图案长度。

I want only those rows which has 'function' and 'alphanumeric pattern' which has min 5 and max 8 characters.我只想要那些具有最少 5 个和最多 8 个字符的“功能”和“字母数字模式”的行。 Starting and ending characters are not fixed, not case sensitive.开始和结束字符不固定,不区分大小写。

Column value looks like this:列值如下所示:

Row    Value
--------------------------------------------------------------------
 1     I have a single own function and its namely 123BA689,BAS54256
 2     Everyone has base function AFD12,CHD12234
 3     Nicole has its own ASS1256902,25ADFG2

Desired output:期望的输出:

Row    Value
--------------------------------------------------------------------
 1     I have a single own function and its namely 123BA689,BAS54256a
 2     Everyone has base function AFD12,CHD1223465AS

I have tried Like and regex to match pattern but failed.我尝试过Like和 regex 来匹配模式但失败了。

Does anybody know how to fix it?有人知道如何解决吗?

select * 
from ab 
where lower(ab.a) like '%function' and '%[a-z0-9]{6}%'

Thanks.谢谢。

SQL Server doesn't support regular expressions. SQL Server 不支持正则表达式。 You could conceptually do what you want with:你可以在概念上做你想做的事情:

where lower(ab.a) like '%function%' and
      lower(ab.a) like '%[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]%' and
      lower(ab.a) not like '%[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]%' 

However, this will return any string that has "function", because that is an alphanumeric patter with 5-8 characters.但是,这将返回任何具有“功能”的字符串,因为这是一个包含 5-8 个字符的字母数字模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM