T-SQL中使用正則表達(dá)式函數(shù)
更新時(shí)間:2010年06月29日 15:46:35 作者:
有想過(guò)在T-Sql使用正則表達(dá)式嗎?是的,完全可以的,我們可以用SQL SERVER CLR sql function來(lái)實(shí)現(xiàn)這一功能。
首先,我們?cè)赩STS中創(chuàng)建一Database Project,增一個(gè)class, 實(shí)現(xiàn)下面的一個(gè)方法:
/// <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會(huì)自動(dòng)注冊(cè)這個(gè)程序集的。如果,你想手動(dòng)注冊(cè)程序集,可執(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的后,我們來(lái)測(cè)試下:
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的正則表達(dá)式。
完了,希望這篇POST對(duì)您有幫助。
您可能對(duì)以下POST感興趣:
SQLSERVER2008中CTE的Split與CLR的性能比較
SQLSERVER使用CLR Stored Procedure導(dǎo)出數(shù)據(jù)到Excel
復(fù)制代碼 代碼如下:
/// <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會(huì)自動(dòng)注冊(cè)這個(gè)程序集的。如果,你想手動(dòng)注冊(cè)程序集,可執(zhí)行以下的T-SQL:
復(fù)制代碼 代碼如下:
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的后,我們來(lái)測(cè)試下:
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的正則表達(dá)式。
完了,希望這篇POST對(duì)您有幫助。
您可能對(duì)以下POST感興趣:
SQLSERVER2008中CTE的Split與CLR的性能比較
SQLSERVER使用CLR Stored Procedure導(dǎo)出數(shù)據(jù)到Excel
您可能感興趣的文章:
- sqlserver2005 TSql新功能學(xué)習(xí)總結(jié)(數(shù)據(jù)類型篇)
- 如何在SQL Server 2008下輕松調(diào)試T-SQL語(yǔ)句和存儲(chǔ)過(guò)程
- SQLServer 2008 新增T-SQL 簡(jiǎn)寫語(yǔ)法
- SQL Server 數(shù)據(jù)庫(kù)管理常用的SQL和T-SQL語(yǔ)句
- 通過(guò)T-SQL語(yǔ)句實(shí)現(xiàn)數(shù)據(jù)庫(kù)備份與還原的代碼
- SQL Server 數(shù)據(jù)庫(kù)管理常用的SQL和T-SQL語(yǔ)句
- T-SQL篇如何防止SQL注入的解決方法
- T-SQL 查詢語(yǔ)句的執(zhí)行順序解析
- 一些 T-SQL 技巧
- SQL Server中T-SQL 數(shù)據(jù)類型轉(zhuǎn)換詳解
相關(guān)文章
SQL判斷是否"存在",還在用 count 操作?很耗時(shí)的!
這篇文章主要介紹了SQL判斷是否"存在",還在用 count 操作?很耗時(shí)的!本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12SQL Server數(shù)字開頭的數(shù)據(jù)庫(kù)表名的解決方法
這篇文章主要介紹了SQL Server數(shù)字開頭的數(shù)據(jù)庫(kù)表名的解決方法,需要的朋友可以參考下2015-11-11SQL Server 2012使用Offset/Fetch Next實(shí)現(xiàn)分頁(yè)數(shù)據(jù)查詢
在Sql Server 2012之前,實(shí)現(xiàn)分頁(yè)主要是使用ROW_NUMBER(),在SQL Server2012,可以使用Offset ...Rows Fetch Next ... Rows only的方式去實(shí)現(xiàn)分頁(yè)數(shù)據(jù)查詢,具體代碼詳解大家參考下本文2017-07-07判斷觸發(fā)器正在處理的是插入,刪除還是更新觸發(fā)
平常時(shí)寫觸發(fā)器(TRIGGER),一般會(huì)分別寫插入(INSERT),刪除(DELETE)和更新(UPDATE)單獨(dú)的觸發(fā)器2012-01-01世界杯猜想活動(dòng)的各類榜單的SQL語(yǔ)句小結(jié)
自己網(wǎng)站的世界杯猜想活動(dòng),整理了幾個(gè)排行榜。寫了半個(gè)小時(shí)的SQL,丟了多可惜,放在這里,反正是別人的地盤,不心疼。2010-07-07SQL Server 2005數(shù)據(jù)庫(kù)還原錯(cuò)誤的經(jīng)典解決方案
本文主要介紹了一個(gè)SQL Server 2005數(shù)據(jù)庫(kù)還原過(guò)程中的錯(cuò)誤的解決方案,需要的朋友可以參考下2015-08-08SQL Server中函數(shù)、存儲(chǔ)過(guò)程與觸發(fā)器的用法
這篇文章介紹了SQL Server中函數(shù)、存儲(chǔ)過(guò)程與觸發(fā)器的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04