温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-28 14:32:09 来源:亿速云 阅读:167 作者:Leah 栏目:数据库

今天就跟大家聊聊有关T-SQL中如何使用正则表达式函数,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

首先,我们在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中如何使用正则表达式函数有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI