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

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

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

1、创建数据库项目

  2009031315384276.gif

2、添加用户定义函数

  2009031315410629.gif

以下是演示代码:

ContractedBlock.gif ExpandedBlockStart.gif 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、将自定义函数关联到数据库

  2009031315415387.gif

4、Sql 查询分析器

    2009031315435727.gif

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

EXEC sp_configure 'clr enabled', 1

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

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

转载于:https://www.cnblogs.com/abeen/archive/2009/03/13/1410853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值