T-SQL中使用正則表達式函數
更新時間:2010年06月29日 15:46:35 作者:
有想過在T-Sql使用正則表達式嗎?是的,完全可以的,我們可以用SQL SERVER CLR sql function來實現這一功能。
首先,我們在VSTS中創(chuàng)建一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會自動注冊這個程序集的。如果,你想手動注冊程序集,可執(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的記錄數。 等于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
復制代碼 代碼如下:
/// <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的記錄數。 等于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
相關文章
SQL判斷是否"存在",還在用 count 操作?很耗時的!
這篇文章主要介紹了SQL判斷是否"存在",還在用 count 操作?很耗時的!本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12SQL Server 2012使用Offset/Fetch Next實現分頁數據查詢
在Sql Server 2012之前,實現分頁主要是使用ROW_NUMBER(),在SQL Server2012,可以使用Offset ...Rows Fetch Next ... Rows only的方式去實現分頁數據查詢,具體代碼詳解大家參考下本文2017-07-07判斷觸發(fā)器正在處理的是插入,刪除還是更新觸發(fā)
平常時寫觸發(fā)器(TRIGGER),一般會分別寫插入(INSERT),刪除(DELETE)和更新(UPDATE)單獨的觸發(fā)器2012-01-01