SQL Server 中的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1964124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:51:49  来源:igfitidea点击:

Regular expressions inside SQL Server

sqlsql-serverregexexpression

提问by Hyman

I have stored values in my database that look like 5XXXXXX, where Xcan be any digit. In other words, I need to match incoming SQL query strings like 5349878.

我在我的数据库中存储了看起来像 的值5XXXXXX,其中X可以是任何数字。换句话说,我需要匹配传入的 SQL 查询字符串,如5349878.

Does anyone have an idea how to do it?

有谁知道该怎么做?

I have different cases like XXXX7XXfor example, so it has to be generic. I don't care about representing the pattern in a different way inside the SQL Server.

我有不同的情况XXXX7XX,例如,所以它必须是通用的。我不关心在 SQL Server 内部以不同的方式表示模式。

I'm working with c# in .NET.

我在 .NET 中使用 c#。

采纳答案by OMG Ponies

stored value in DB is: 5XXXXXX [where x can be any digit]

DB 中的存储值为:5XXXXXX [其中 x 可以是任何数字]

You don't mention data types - if numeric, you'll likely have to use CAST/CONVERT to change the data type to [n]varchar.

您没有提到数据类型 - 如果是数字,您可能必须使用 CAST/CONVERT 将数据类型更改为 [n]varchar。

Use:

用:

WHERE CHARINDEX(column, '5') = 1
  AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
  AND ISNUMERIC(column) = 1

References:

参考:

i have also different cases like XXXX7XX for example, so it has to be generic.

我也有不同的情况,例如 XXXX7XX,所以它必须是通用的。

Use:

用:

WHERE PATINDEX('%7%', column) = 5
  AND CHARINDEX(column, '.') = 0 --to stop decimals if needed
  AND ISNUMERIC(column) = 1

References:

参考:

Regex Support

正则表达式支持

SQL Server 2000+ supports regex, but the catch is you have to create the UDF function in CLR before you have the ability. There are numerous articles providing example code if you google them. Once you have that in place, you can use:

SQL Server 2000+ 支持正则表达式,但问题是您必须先在 CLR 中创建 UDF 函数,然后才能拥有该功能。如果你用谷歌搜索,有很多文章提供了示例代码。一旦你有了它,你可以使用:

  • 5\d{6}for your first example
  • \d{4}7\d{2}for your second example
  • 5\d{6}对于你的第一个例子
  • \d{4}7\d{2}对于你的第二个例子

For more info on regular expressions, I highly recommend this website.

有关正则表达式的更多信息,我强烈推荐这个网站

回答by dan

You can write queries like this in SQL Server:

您可以在 SQL Server 中编写这样的查询:

--each [0-9] matches a single digit, this would match 5xx
SELECT * FROM YourTable WHERE SomeField LIKE '5[0-9][0-9]'

回答by Sparky

Try this

尝试这个

select * from mytable
where p1 not like '%[^0-9]%' and substring(p1,1,1)='5'

Of course, you'll need to adjust the substring value, but the rest should work...

当然,您需要调整子字符串值,但其余的应该可以工作...

回答by Robert

In order to match a digit, you can use [0-9].

为了匹配数字,您可以使用[0-9].

So you could use 5[0-9][0-9][0-9][0-9][0-9][0-9]and [0-9][0-9][0-9][0-9]7[0-9][0-9][0-9]. I do this a lot for zip codes.

所以你可以使用5[0-9][0-9][0-9][0-9][0-9][0-9]and [0-9][0-9][0-9][0-9]7[0-9][0-9][0-9]。对于邮政编码,我经常这样做。

回答by JCasso

SQL Wildcards are enough for this purpose. Follow this link: http://www.w3schools.com/SQL/sql_wildcards.asp

SQL 通配符足以达到此目的。按照此链接:http: //www.w3schools.com/SQL/sql_wildcards.asp

you need to use a query like this:

你需要使用这样的查询:

select * from mytable where msisdn like '%7%'

or

或者

select * from mytable where msisdn like '56655%'