T-SQL中使用正則表達式函數(shù)
更新時間:2010年06月29日 15:46:35 作者:
有想過在T-Sql使用正則表達式嗎?是的,完全可以的,我們可以用SQL SERVER CLR sql function來實現(xiàn)這一功能。
首先,我們在VSTS中創(chuàng)建一Database Project,增一個class, 實現(xiàn)下面的一個方法:
/// <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會自動注冊這個程序集的。如果,你想手動注冊程序集,可執(zhí)行以下的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的記錄數(shù)。 等于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導出數(shù)據(jù)到Excel
復制代碼 代碼如下:
/// <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會自動注冊這個程序集的。如果,你想手動注冊程序集,可執(zhí)行以下的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的記錄數(shù)。 等于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導出數(shù)據(jù)到Excel
您可能感興趣的文章:
- sqlserver2005 TSql新功能學習總結(jié)(數(shù)據(jù)類型篇)
- 如何在SQL Server 2008下輕松調(diào)試T-SQL語句和存儲過程
- SQLServer 2008 新增T-SQL 簡寫語法
- SQL Server 數(shù)據(jù)庫管理常用的SQL和T-SQL語句
- 通過T-SQL語句實現(xiàn)數(shù)據(jù)庫備份與還原的代碼
- SQL Server 數(shù)據(jù)庫管理常用的SQL和T-SQL語句
- T-SQL篇如何防止SQL注入的解決方法
- T-SQL 查詢語句的執(zhí)行順序解析
- 一些 T-SQL 技巧
- SQL Server中T-SQL 數(shù)據(jù)類型轉(zhuǎn)換詳解
相關(guān)文章
SQL Server查詢數(shù)據(jù)庫中表使用空間信息實現(xiàn)腳本
這篇文章主要介紹了SQL Server查詢數(shù)據(jù)庫中表使用空間信息實現(xiàn)腳本,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-07-07
CMD命令操作MSSQL2005數(shù)據(jù)庫(命令整理)
創(chuàng)建數(shù)據(jù)庫、創(chuàng)建用戶、修改數(shù)據(jù)的所有者、設(shè)置READ_COMMITTED_SNAPSHOT以及備份、日志扥等等,感興趣的朋友可以參考下2013-05-05
SQL Server2019數(shù)據(jù)庫之簡單子查詢的具有方法
這篇文章主要介紹了SQL Server2019數(shù)據(jù)庫之簡單子查詢的具有方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
SQL Server 作業(yè)的備份(備份作業(yè)非備份數(shù)據(jù)庫)
我的方法是把作業(yè)導出成文件備份起來,因為當你服務(wù)器維護的多了的時候很多你的作業(yè) 就很成問題,很麻煩2012-06-06
將string數(shù)組轉(zhuǎn)化為sql的in條件用sql查詢
將string數(shù)組轉(zhuǎn)化為sql的in條件就可以用sql查詢了,下面是具體是的示例,大家可以參考下2014-05-05
SQLServer中使用擴展事件獲取Session級別的等待信息及SQLServer 2016中Session級別等待信
這篇文章主要介紹了SQLServer中使用擴展事件獲取Session級別的等待信息及SQLServer 2016中Session級別等待信息的增強,需要的朋友可以參考下2017-05-05

