[Database] SqlServer: SQL Server 2005 中使用正则表达式匹配
2009-03-13 16:07
387 查看
SQL Server 2005 中使用正则表达式匹配
CLR 用户定义函数只是在 .NET 程序集中定义的静态方法。CREATE FUNCTION 语句已扩展为支持创建 CLR
用户定义函数。
1、创建数据库项目
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
=============================================
CLR 用户定义函数只是在 .NET 程序集中定义的静态方法。CREATE FUNCTION 语句已扩展为支持创建 CLR
用户定义函数。
1、创建数据库项目
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
=============================================
相关文章推荐
- SQL Server 2005 中使用正则表达式匹配
- SQL Server 2005正则表达式使模式匹配和数据提取变得更容易~
- SQL Server 2005 里使用正则表达式的方法
- SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易
- 在SQL Server 2005中使用正则表达式
- SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易
- python使用正则表达式替换匹配成功的组
- php正则表达式中的非贪婪模式匹配的使用
- 不使用正则表达式 匹配字符串(字母)
- 使用正则表达式匹配嵌套Html标签
- python里使用正则表达式来替换匹配成功的组并限定替换的次数
- 实例分析js和C#中使用正则表达式匹配a标签
- python里使用正则表达式的转义字符的匹配
- Python中使用中文正则表达式匹配指定的中文字符串
- android开发(22)使用正则表达式 。从一个字符串中找出数字,多次匹配。
- 使用正则表达式匹配模式(验证电子邮件地址)
- 使用正则表达式匹配任意字符包括空…
- 使用正则表达式匹配IP地址
- 使用正则表达式匹配嵌套Html标签
- sublimetext 使用正则表达式匹配中文