随笔 - 651  文章 - 0  评论 - 385  阅读 - 60万

sql2005判断字符(串)出现次数

   http://blog.csdn.net/eduxh/archive/2008/03/06/2154439.aspx

方法:Create function angel
(@variable varchar(max), --要寻找的字符
@char varchar(max)) --目标字符串
returns int
begin
    declare @index int
    set @index=(len(@variable)-len(replace(@variable, @char, '')))/len(@char)

    return @index
end
但是ljmiis的也有不全面的地方:
1、datalength表示的是字符的长度,向中文的长度就是两个。这样结果就是错的;
2、可能我标题没取好,原函数还可以判断字符串出现次数。

 原始方法

Create function angel
(@variable varchar(max), --要寻找的字符
@char varchar(max)) --目标字符串
returns int
begin
    declare @index int
    set @index=0

    while (select charindex(@variable,@char))>0
    begin
        set @index=@index+1
        set @char=stuff(@char,(charindex(@variable,@char)),1,'0')
    end

    return @index

end

posted on   巍巍边疆  阅读(370)  评论(0编辑  收藏  举报
< 2008年3月 >
24 25 26 27 28 29 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示