求两个字符串中相同的汉字及字母的个数

--创建函数

create function [dbo].[funcomparestring]

(

       @stra nvarchar(200),

       @strb nvarchar(200)

)

returns int as

begin

    declare @strbase nvarchar(200)

    declare @rank int

    select @rank=0

       if len(@stra)>len(@strb)

          select @rank=count(*)

          from funsplitchar(@strb)

          where item in(select item from funsplitchar(@stra))

       else

          select @rank=count(*)

          from funsplitchar(@stra)

          where item in(select item from funsplitchar(@strb))

    return @rank

end

Go

--创建第二种函数

create function [dbo].[funcomparestring_new]

(

       @stra nvarchar(200),

       @strb nvarchar(200)

)

returns int as

begin

       declare @strbase nvarchar(200)

       declare @rank int

       select @rank=0

       if len(@stra)>len(@strb)

       select @rank=count(*)

           from funsplitchar(@strb)

           where item in(select distinct  item from funsplitchar(@stra))

   else

       select @rank=count(*)

       from

       (

         select distinct * from funsplitchar(@stra)

           where item in(select distinct item from funsplitchar(@strb))

       ) bb

       return @rank

end

 

--以上两个函数有什么不同呢?下面我用个例子来给大家说明一下:

 

--测试示例

select [dbo].[funCompareString]('中国Chinese之家','中国人是Chinese')

 

--结果为:9

 

select [dbo].[funCompareString_new]('中国Chinese之家','中国人是Chinese')

 

--结果为:8

 

--在这两个字符串中,'ese'与'ese'的重复在第一个函数算个字符重复,

--而在第二个函数中算个字符重复。

--也就是说在第二个函数中,多次相同的重复不累积计算例如ese中的e。

原文地址:https://www.cnblogs.com/accumulater/p/6244456.html