【叶子函数分享三】求第一个字符串中第二个串的个数

来源:互联网 发布:python 卷积函数 编辑:程序博客网 时间:2024/05/27 21:48

--创建函数

create function [dbo].[m_count]

(

    @str_one nvarchar(200),  --第一个字符串

    @str_two nvarchar(200)   --第二个字符串

)

returns int as

begin

    declare @sqlcount int

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

return @sqlcount

end

 

--测试示例

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

 

--运行结果

/*

count

-----------

2

*/

 

 

特别说明:

如果数据量比较大,尽量避免使用自定义函数,以免严重影响性能。


原创粉丝点击