在SQL Server 2005中使用正则表达式

运维 数据库运维 SQL Server
本文将介绍在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;     } }

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

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

4、Sql 查询分析器

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

EXEC sp_configure 'clr enabled', 1

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

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

【编辑推荐】

  1. 深入浅出MySQL双向复制技术
  2. 并行查询让SQL Server加速运行
  3. SQL Server 2005常见问题浅析
责任编辑:彭凡 来源: 博客园
相关推荐

2023-10-07 08:25:09

Java处理工具正则表达式

2012-05-08 13:29:01

VB

2010-03-25 18:25:36

Python正则表达式

2024-12-25 15:09:38

Python字符串函数

2009-04-23 11:24:44

SQL正则表达式查询

2009-06-15 17:24:59

Groovy正则表达式

2023-04-17 19:53:37

编程正则表达式

2024-10-17 17:03:43

SQL正则表达式MySQL

2018-09-27 15:25:08

正则表达式前端

2009-09-16 17:29:30

正则表达式使用详解

2020-09-04 09:16:04

Python正则表达式虚拟机

2024-10-09 16:35:20

Python正则表达式

2024-09-14 09:18:14

Python正则表达式

2010-07-14 09:47:04

Perl正则表达式

2009-09-16 13:14:10

Ereg正则表达式

2020-11-04 09:23:57

Python

2011-08-23 17:46:09

LUA正则表达式模式

2009-09-16 18:19:34

正则表达式组

2021-01-27 11:34:19

Python正则表达式字符串

2011-06-02 12:34:16

正则表达式
点赞
收藏

51CTO技术栈公众号