字符串匹配度 函数

ALTER FUNCTION [dbo].[F_GetsimilarRate]
(
@strlist nvarchar(max),
@comparelist nvarchar(max)
)
RETURNS decimal(9,2)
AS
BEGIN

if(@strlist is null)
begin
 return 0;
end
declare @count int; --计算几个相同的字
declare @strlen int;  --字符串的长度
declare @location int; --对比到那个位置
declare @strindex int;   --对比字符在对方的那个位置
declare @strone nvarchar(1); -- 对比字符
declare @otherstr nvarchar(max);
set @count=0;
set @strlen=len(@strlist);
set @location=1;
set @otherstr=@comparelist;

if(@strlen=0)
begin
 return 0;
end

while(@strlen>0)
begin
 set @strone=substring(@strlist,@location,1); --取每个字符
 set @location=@location+1;
 set @strlen=@strlen-1;
 set @strindex=charindex(@strone,@otherstr,1); --获取相同字符的位置
 if(@strindex>0)
 begin
     set @otherstr=substring(@otherstr,0,@strindex)+substring(@otherstr,@strindex+1,len(@otherstr)-@strindex); --去掉对比字符串里相同的字符
  set @count=@count+2;
 end
end

return convert(decimal(9,2),(convert(decimal(9,2),@count)/convert(decimal(9,2),len(@strlist)+len(@comparelist)))*100);

posted @ 2012-10-31 17:14  洗耳恭听兼烂笔头  阅读(335)  评论(0编辑  收藏  举报