葉子函式分享四 綜合模糊查詢

2021-08-25 18:18:52 字數 1663 閱讀 4451

--建立函式

create

function [dbo].[m_fuzzyquery_v1] (

@str nvarchar

(2000))

returns

nvarchar

(2000)as

begin

declare @count int

,@i int

;declare @newchar nvarchar

(200),@nn nvarchar

(300),@hh nvarchar

(200)

set @count=

len(@str);

set @i=1;

set @nn='';

while @i<@count+1

begin

set @newchar=

substring

(@str,@i,1)+

'%'set @nn=@nn+@newchar;

set @i=@i+1;

endset @hh=

'%'+@nn

return @hh

end

--測試資料

declare

@table table

(connect varchar

(30))

insert

into @table

select

'我愛程式'

union

allselect

'我的程式生活'

union

allselect

'絕對無聊的生活'

union

allselect

'活得好累'

union

allselect

'程式設計師的生活'

union

allselect

'序論'

union

allselect

'生機'

union

allselect

'生活雜誌'

union

allselect

'我只是隨便寫寫'

union

allselect

'真的是要來搜尋的'

union

allselect

'程式設計師一生的活路'

--普通的模糊查詢

select

*from @table where connect like

'%程式生活%'

--執行結果 /*

connect

------------------------------

我的程式生活 */

--應用函式查詢

select

*from @table where connect like

(select dbo.[m_fuzzyquery_v1](

'程式生活'))

--執行結果 /*

connect

------------------------------

我的程式生活

程式設計師的生活

程式設計師一生的活路 */

特別說明:

如果資料量比較大,盡量避免使用自定義函式,以免嚴重影響效能。

葉子函式分享五十 無序字元比較函式

go 建立函式 第一版 create function get orderstr str varchar 8000 returns varchar 8000 as begin set str rtrim str declare tb table s varchar 1 a int while len...

葉子函式分享四十四 全形半形轉換函式

此函式部分思路參考了 csdn 上大力的轉換函式 鄒建 2005.01 引用請保留此資訊 go 建立函式 create function sbc2dbc str nvarchar 4000 要轉換的字串 flag bit 轉換標誌 0轉換成半形 1轉換成全角 returns nvarchar 400...

葉子函式分享四十四 全形半形轉換函式

此函式部分思路參考了csdn上大力的轉換函式 鄒建2005.01 引用請保留此資訊 go 建立函式 create function sbc2dbc str nvarchar 4000 要轉換的字串 flag bit 轉換標誌,0轉換成半形,1轉換成全角 returns nvarchar 4000 a...