SQL 创建自定义方法,匹配正则

C#代码#

using System.Text.RegularExpressions;

public class FunctionRegex
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static string Match(string input, string regex)
    {
        return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Value;
    }

    [Microsoft.SqlServer.Server.SqlFunction]
    public static string MatchGroup(string input, string regex)
    {
        return string.IsNullOrEmpty(input) ? "" : new Regex(regex, RegexOptions.IgnoreCase).Match(input).Groups[1].Value;
    }

}
//drop function  func_RegexMatchGroup
//drop assembly SQLCLR_RegEx

// create assembly SQLCLR_RegEx
//from 'F:SSISPetroChina.PPS2.DB.SQLCLR.dll'
// WITH PERMISSION_SET = UNSAFE


//-- =============================================
//-- Author:		wangzhanbo
//-- Create date: 2018-11-19
//-- Description:	regex_function
//-- =============================================
//CREATE FUNCTION func_RegexMatchGroup
//(
//    @input nvarchar(1000)
//	,@regex nvarchar(1000)
//)
//RETURNS nvarchar(1000)

//WITH EXECUTE AS CALLER
//AS
//EXTERNAL NAME SQLCLR_RegEx.FunctionRegex.Match

原文地址:https://www.cnblogs.com/wangzhanbo/p/9987043.html