• [Database] SqlServer: SQL Server 2005 中使用正则表达式匹配


    SQL Server 2005 中使用正则表达式匹配

    CLR 用户定义函数只是在 .NET 程序集中定义的静态方法。CREATE FUNCTION 语句已扩展为支持创建 CLR 用户定义函数。

    1、创建数据库项目

      

    2、添加用户定义函数

      

    以下是演示代码:

    Code
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    using System.Text.RegularExpressions;

    // 示意代码
    public partial class UserDefinedFunctions
    {
        
    public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;

        [Microsoft.SqlServer.Server.SqlFunction]
        
    public static string RegexValue(SqlChars input, SqlString pattern)
        {
            Regex regex 
    = new Regex(pattern.Value, Options);

            
    return  regex.Match(new string(input.Value)).Value;
        }
    }

    3、将自定义函数关联到数据库

      

    4、Sql 查询分析器

        

    为了确保SQL可以执行托管代码,执行下面的语句:

    EXEC sp_configure 'clr enabled', 1

    sql 如下:
    select dbo.RegexValue('2008-09-02',N'\d{4}') from Table

    =============================================

  • 相关阅读:
    CH4302 Interval GCD
    20201017 模拟赛总结
    LOJ#2610. 「NOIP2013」货车运输
    POJ3696 The Luckiest number
    20200925 小练习总结
    更新一波状态
    AFO
    【扩展lucas定理】
    【LCT维护子树信息】uoj207 共价大爷游长沙
    [bzoj4589]Hard Nim(FWT快速沃尔什变化+快速幂)
  • 原文地址:https://www.cnblogs.com/abeen/p/1410853.html
Copyright © 2020-2023  润新知