相当于MySQL REGEXP的Microsoft SQL Server [英] Microsoft SQL Server equivalent of MySQL REGEXP

查看:86
本文介绍了相当于MySQL REGEXP的Microsoft SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重建为Microsoft SQL Server中的MySQL创建的数据库查询.我正在寻找一种类似于REGEXP的运算符或函数SQL Server.

I am trying to reconstruct a database query I created for MySQL in Microsoft SQL Server. I am looking for an operator or function SQL Server which acts like REGEXP.

以下是我如何使用运算符的示例:

Here is an example of how I am using the operator:

select *
from   musicdetails
WHERE  artistname REGEXP '^".mysql_escape_string($_GET['search'])."$'

推荐答案

在这里(编译为SQL CLR程序集):

Here you go (compile as SQL CLR assembly):

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;
  }
}

这篇关于相当于MySQL REGEXP的Microsoft SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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