日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

求第一個(gè)字符串中第二個(gè)串的個(gè)數(shù),綜合模糊查詢

 zww_blog 2012-03-15
求第一個(gè)字符串中第二個(gè)串的個(gè)數(shù)
分類: SQL函數(shù)分享系列


--創(chuàng)建函數(shù)

create function [dbo].[m_count]

(

    @str_one nvarchar(200),  --第一個(gè)字符串

    @str_two nvarchar(200)   --第二個(gè)字符串

)

returns int as

begin

    declare @sqlcount int

    select @sqlcount=(len(@str_one)-len(replace(@str_one,@str_two,'')))/len(@str_two)

return @sqlcount

end

 

--測(cè)試示例

select dbo.m_count('sqlserver','e') as [count]

 

--運(yùn)行結(jié)果

/*

count

-----------

2

*/


綜合模糊查詢


--創(chuàng)建函數(shù)

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;

       end

    set @hh='%'+@nn

    return @hh

end

 

--測(cè)試數(shù)據(jù)

declare @table table (connect varchar(30))

insert into @table

select '我愛程序' union all

select '我的程序生活' union all

select '絕對(duì)無聊的生活' union all

select '活得好累' union all

select '程序員的生活' union all

select '序論' union all

select '生機(jī)' union all

select '生活雜志' union all

select '我只是隨便寫寫' union all

select '真的是要來搜索的' union all

select '程序員一生的活路'

 

--普通的模糊查詢

select * from @table where connect like '%程序生活%'

--運(yùn)行結(jié)果

/*

connect

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

我的程序生活

*/

 

--應(yīng)用函數(shù)查詢

select * from @table where connect like ( select dbo.[m_fuzzyquery_v1]('程序生活'))

--運(yùn)行結(jié)果

/*

connect

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

我的程序生活

程序員的生活

程序員一生的活路

*/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多