T-SQL中使用正则表达式函数

时间:2024-01-27 13:36:58 

首先,我们在VSTS中创建一Database Project,增一个class, 实现下面的一个方法:


/// <summary>
/// Regs the ex match.
/// </summary>
/// <param name="inputValue">The input value.</param>
/// <param name="regexPattern">The regex pattern.</param>
/// <remarks>Author: Petter Liu http://wintersun.cnblogs.com </remarks>
/// <returns>1 match,0 not match</returns>
[SqlFunction]
public static bool RegExMatch(string inputValue, string regexPattern)
{
// Any nulls - we can't match, return false
if (string.IsNullOrEmpty(inputValue) || string.IsNullOrEmpty(regexPattern))
return false;

Regex r1 = new Regex(regexPattern.TrimEnd(null));
return r1.Match(inputValue.TrimEnd(null)).Success;
}


好了,Build后Deploy到你的Target database就OK了,VisualStudio会自动注册这个程序集的。如果,你想手动注册程序集,可执行以下的T-SQL:


CREATE ASSEMBLY [RegExCLR] FROM 'RegExCLR.dll';

-- Add the REGEX function. We want a friendly name
-- RegExMatch rather than the full namespace name.
-- Note the way we have to specify the Assembly.Namespace.Class.Function
-- NOTE the RegExCLR.RegExCLR
-- (one is the assembly the other is the namespace)
CREATE FUNCTION RegExMatch ( @inputCalue NVARCHAR(4000),
@regexPattern NVARCHAR(4000) ) RETURNS BIT
AS EXTERNAL NAME RegExCLR.RegExCLR.ClrClass.RegExMatch;


OK, 一切OK的后,我们来测试下:

select COUNT(1) from Threads where dbo.RegExMatch(ThreadId,'^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$')=1
上面的T-SQL是找出Threads表ThreadId是GUID的记录数。 等于1是匹配,^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$ 匹配GUID的正则表达式。

完了,希望这篇POST对您有帮助。

您可能对以下POST感兴趣:

SQLSERVER2008中CTE的Split与CLR的性能比较

SQLSERVER使用CLR Stored Procedure导出数据到Excel

标签:T-SQL,正则表达
0
投稿

猜你喜欢

  • PHP查询快递信息的方法

    2023-11-14 10:25:48
  • Keras 使用 Lambda层详解

    2021-08-11 20:12:40
  • asp中常用的文件处理函数

    2009-01-08 18:09:00
  • 解析PyCharm集成GitLab代码仓的问题

    2022-06-08 07:36:15
  • python GUI库图形界面开发之PyQt5布局控件QVBoxLayout详细使用方法与实例

    2022-10-12 11:37:27
  • python 列表输出重复值以及对应的角标方法

    2021-06-19 13:29:57
  • Yii2框架整合Xunsearch搜索引擎的方法

    2024-06-05 09:37:48
  • Pandas Shift函数的基础入门学习笔记

    2023-02-16 20:46:12
  • JS关于 replace 取值、替换第几个匹配项问题小结

    2024-04-25 13:11:05
  • Python Socket实现简单TCP Server/client功能示例

    2021-05-21 19:19:46
  • 基于pycharm 项目和项目文件命名规则的介绍

    2021-02-05 19:30:51
  • python连接PostgreSQL数据库的过程详解

    2023-08-24 03:42:31
  • Python3安装Pillow与PIL的方法

    2022-09-27 10:21:28
  • 修改asp代码防止被杀毒软件误删

    2007-10-07 12:32:00
  • Python快速排序算法实例分析

    2021-10-23 09:14:37
  • javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)

    2024-04-22 12:49:59
  • Javascript技术栈中的四种依赖注入小结

    2024-04-18 10:54:08
  • Numpy中的repeat函数使用

    2023-03-06 19:16:38
  • 浅谈tensorflow 中tf.concat()的使用

    2023-07-21 20:24:08
  • golang 实现tcp server端和client端,并计算RTT时间操作

    2023-08-30 14:12:49
  • asp之家 网络编程 m.aspxhome.com