在Sql語句中使用正規表示式來查詢你所要的字元

2021-09-07 21:04:34 字數 4941 閱讀 3865

這幾天有個專案需要從表的某個字段判斷是否存在某些規則的字串,大概如下:

有個表叫taskdeploy(任務部署的),其中欄位participants以格式 使用者名稱,使用者名稱 具體如下:

admin,小明,samlin

小明,samlin,test

samlin,test,小明

samlin,admin

samlin,test

samlin,test

好了,現在我要查詢有指派給 'samlin'使用者的任務記錄怎麼辦?

我首先想到的是正規表示式,但sql提供的功能真是少啊就是乙個 like 加幾個萬用字元,顯然是滿足不了我的要求的

於是從網上蒐集了一些資料,整理了一下。

下面這個是乙個自定義函式,使用者可以呼叫這個函式判斷指定的字串是否符合正規表示式的規則.

create

function

dbo.find_regular_expression

(@source

varchar

(5000

),   

--需要匹配的源字串

@regexp

varchar

(1000

),  

--正規表示式

@ignorecase

bit=0--

是否區分大小寫,預設為false

)returns

bit--

返回結果0-false,1-true

asbegin

--0(成功)或非零數字(失敗),是由 ole 自動化物件返回的 hresult 的整數值。

declare

@hrinteger

--用於儲存返回的物件令牌,以便之後對該物件進行操作

declare

@objregexp

integer

declare

@objmatches

integer

--儲存結果

declare

@results

bit/*

建立 ole 物件例項,只有 sysadmin 固定伺服器角色的成員才能執行 sp_oacreate,並確定機器中有vbscript.regexp類庫

*/exec

@hr=

sp_oacreate 

'vbscript.regexp', 

@objregexp

output

if@hr

<>

0begin

set@results=0

return

@results

end/*

以下三個分別是設定新建物件的三個屬性。下面是'vbscript.regexp'中常用的屬性舉例:

dim regex,match,matches         '建立變數。

set regex = new regexp         '建立一般表示式。

regex.pattern= patrn         '設定模式。

regex.ignorecase = true         '設定是否區分大小寫。

regex.global=true                             '設定全域性可用性。

set matches=regex.execute(string)             '重複匹配集合

regexptest = regex.execute(strng)      '執行搜尋。

for each match in matches                    '重複匹配集合

retstr=retstr &"match found at position "

retstr=retstr&match.firstindex&".match value is '"

retstr=retstr&match.value&"'."&vbcrlf next

regexptest=retstr

*/exec

@hr=

sp_oasetproperty 

@objregexp, '

pattern', 

@regexp

if@hr

<>

0begin

set@results=0

return

@results

endexec

@hr=

sp_oasetproperty 

@objregexp, '

global

', false

if@hr

<>

0begin

set@results=0

return

@results

endexec

@hr=

sp_oasetproperty 

@objregexp, '

ignorecase', 

@ignorecase

if@hr

<>

0begin

set@results=0

return

@results

end--

呼叫物件方法

exec

@hr=

sp_oamethod 

@objregexp, '

test', 

@results

output, 

@source

if@hr

<>

0begin

set@results=0

return

@results

end--

釋放已建立的 ole 物件

exec

@hr=

sp_oadestroy 

@objregexp

if@hr

<>

0begin

set@results=0

return

@results

endreturn

@results

end

下面是乙個簡單的測試sql語句,可以直接在查詢分析器中執行。

declare

@intlength

asinteger

declare

@vchregularexpression

asvarchar(50

)declare

@vchsourcestring

asvarchar(50

)declare

@vchsourcestring2

asvarchar(50

)declare

@bithasnospecialcharacters

asbit

--初始化變數

set@vchsourcestring='

test one this is a test!!

'set

@vchsourcestring2='

test two this is a test'--

我們的正規表示式應該類似於

--[a-za-z ]{}

--如: [a-za-z ]

--獲得字串長度

set@intlength

=len

(@vchsourcestring)--

設定完整的正規表示式

set@vchregularexpression='

[a-za-z ]'--

是否有任何特殊字元

set@bithasnospecialcharacters

=dbo.find_regular_expression(

@vchsourcestring

, @vchregularexpression,0

)print

@vchsourcestring

if@bithasnospecialcharacters=1

begin

print

'no special characters.

'end

else

begin

print

'special characters found.

'end

print

'**************'--

獲得字串長度

set@intlength

=len

(@vchsourcestring2)--

設定完整的正規表示式

set@vchregularexpression='

[a-za-z ]'--

是否有任何特殊字元

set@bithasnospecialcharacters

=dbo.find_regular_expression(

@vchsourcestring2

, @vchregularexpression,0

)print

@vchsourcestring2

if@bithasnospecialcharacters=1

begin

print

'no special characters.

'end

else

begin

print

'special characters found.

'endgo

最後得出的查詢語句如下:

select

* from

taskdeploy  

where1=

1and

dbo.find_regular_expression(participants,

'\bsamlin\b',

0) =1

查詢出包含 samlin 使用者名稱記錄的任務

但用函式的可能效率不知怎麼樣,下次測試一下

不知大家有沒更好的方法?

使用正規表示式將sql語句中的分離

方法1 string regexstring select from where order by string yourstring select id,name from test where id 3 and name ttt order by id desc match m regex.ma...

在SQL使用正規表示式

if exists select from dbo.sysobjects where id object id n dbo getvaluebyregep and xtype in n fn n if n tf drop function dbo getvaluebyregep goset quot...

在UltraEdit中使用正規表示式

在ultraedit中使用正規表示式 刪除空行 替換 t p 為 空串 刪除行尾空格 替換 t 為 空串 刪除行首空格 替換 t 為 空串 每行設定為固定的4個空格開頭 替換 t t p 為 1 每段設定為固定的4個空格開頭 替換 t 為 如果一行是以空格開始的,則視之為一段的開始行 將一段合併為一...