[英]Microsoft SQL Server equivalent of MySQL REGEXP
I am trying to reconstruct a database query I created for MySQL in Microsoft SQL Server. 我正在尝试重建为Microsoft SQL Server中的MySQL创建的数据库查询。 I am looking for an operator or function SQL Server which acts like
REGEXP
. 我正在寻找一种类似于
REGEXP
的运算符或函数SQL Server。
Here is an example of how I am using the operator: 这是我如何使用运算符的示例:
select *
from musicdetails
WHERE artistname REGEXP '^".mysql_escape_string($_GET['search'])."$'
Here you go (compile as SQL CLR assembly): 在这里(编译为SQL CLR程序集):
using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[SqlFunction]
public static bool RegexMatch(string expr, string regex)
{
return Regex.IsMatch(expr, regex);
}
[SqlFunction]
public static string RegexReplace(string expr, string regex, string replace)
{
return Regex.Replace(expr, regex, replace);
}
[SqlFunction(FillRowMethodName="GetToken",
TableDefinition="Value nvarchar(max)")]
public static IEnumerable RegexSplit(string expr, string regex)
{
return Regex.Split(expr, regex);
}
public static void GetToken(object row, out string str)
{
str = (string) row;
}
}
The only way you can do this in SQL Server (2005 and up only) is to use CLR functions; 在SQL Server(仅限2005年及更高版本)中执行此操作的唯一方法是使用CLR函数。 regular expressions as part of native SQL queries isn't standard.
作为本机SQL查询一部分的正则表达式不是标准的。
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
While the code in leppie's answer will compile and execute, I would not recommend it for production use. 虽然leppie答案中的代码可以编译和执行,但我不建议将其用于生产。 If you want RegEx functions that:
如果要使用RegEx函数,请执行以下操作:
NULL
s appropriately NULL
@StartAt
, @RegExOptions
for IgnoreCase , MultiLine , etc) @StartAt
, @RegExOptions
, MultiLine等) and are also free, then check out the SQL# SQLCLR library (that I wrote). 并且也是免费的,然后签出SQL# SQLCLR库(我写的)。 There are 13 RegEx functions in the Free version (and 2 more in the Full / paid version, plus the ability to increase the expression cache size, which can help if you frequently use a variety of expressions).
免费版中有13个RegEx函数(完全版/付费版中还有2个RegEx函数,以及增加表达式缓存大小的功能,如果您经常使用各种表达式,则可以提供帮助)。
I believe the function RegEx_IsMatch (or RegEx_IsMatch4k ) is what you are looking for (yes, it is in the Free version). 我相信您正在寻找功能RegEx_IsMatch (或RegEx_IsMatch4k )(是的,它在免费版本中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.