SQL Server中的正则表达式 [英] Regular expressions inside SQL Server

查看:166
本文介绍了SQL Server中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在数据库中存储了一些类似于 5XXXXXX 的值,其中 X 可以是任何数字。换句话说,我需要匹配传入的SQL查询字符串,如 5349878



有没有人有一个想法如何做?



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



我正在使用.NET中的c#。

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


blockquote>

你不提到数据类型 - 如果是数字,你可能不得不使用CAST / CONVERT来将数据类型更改为[n] varchar。



使用:

  WHERE CHARINDEX(column,'5')= 1 
AND CHARINDEX(列,'。')= 0 - 如果需要,停止小数
和ISNUMERIC(列)= 1

参考文献:





$ b $我也有不同之处例如XXXX7XX的例子,所以它必须是通用的。


使用:

  WHERE PATINDEX('%7%',column)= 5 
AND CHARINDEX(column,'。')= 0 - 如果需要,停止小数
和ISNUMERIC(列)= 1

参考文献:





正则表达式支持



SQL Server 2000+支持正则表达式,但是catch在您有能力之前,必须在CLR中创建UDF函数。有很多文章提供示例代码,如果你谷歌他们。一旦你有了,你可以使用:




  • 5\d {6} 为您的第一个例子

  • \d {4} 7\d {2} li>


有关正则表达式的更多信息,我强烈建议本网站


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

Does anyone have an idea how to do it?

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

I'm working with c# in .NET.

解决方案

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

You don't mention data types - if numeric, you'll likely have to use CAST/CONVERT to change the data type to [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.

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:

  • 5\d{6} for your first example
  • \d{4}7\d{2} for your second example

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

这篇关于SQL Server中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆