sql server 相似度对比

转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx

 

 

函數一.產生 Like 比對用字串

create function fn_get_fuzzy_str( @instr nvarchar(256) )
returns nvarchar(513)
as begin
    /*依據傳入字串補上%符號*/
    /*
    declare @instr nvarchar(256);
    set @instr = N'樹林國民小學'; */

    declare @outstr nvarchar(513)
    
    if isnull(@instr,'') = '' begin
  set @outstr = '';
    end else begin
  declare @i int;
  set @i = 1;    
  set @outstr = '%';
  while @i <= len(@instr) begin
      set @outstr = @outstr + substring(@instr,@i,1) + '%';
      set @i = @i + 1;
  end
    end
    return @outstr;
end

 

函數二.查詢函數

create function fn_str_fuzzy_qry( @src_str nvarchar(256) , @match_str nvarchar(256) , @setp int  )
returns int
as begin
    /*字串相似度比對 結果直越大相似度越高*/
    /*
    declare @src_str nvarchar(256); --比對來源
    declare @match_str nvarchar(256); --比對字串
    declare @setp int;    --每次步減幾個字
    */
    declare @fuzzy_str nvarchar(513);
    declare @like_str  nvarchar(513);

    set @fuzzy_str = dbo.fn_get_fuzzy_str(@match_str);

    return case
  when @src_str like @fuzzy_str then
      4000 + 1000 - len(@src_str)
  when ( len(@fuzzy_str) - @setp*2*1 >= 5 ) and @src_str like left(@fuzzy_str,len(@fuzzy_str) - @setp*2*1) then
      3000 + 1000 - len(@src_str)
  when ( len(@fuzzy_str) - @setp*2*2 >= 5 ) and @src_str like left(@fuzzy_str,len(@fuzzy_str) - @setp*2*2) then
      2000 + 1000 - len(@src_str)
  when ( len(@fuzzy_str) - @setp*2*3 >= 5 ) and @src_str like left(@fuzzy_str,len(@fuzzy_str) - @setp*2*3) then
      1000 + 1000 - len(@src_str)
  else 0
    end

end

 

應用方式

select school_name , ......
from bas_info
where
    dbo.fn_str_fuzzy_qry(school_name,N'樹林國小',1) > 0
order by dbo.fn_str_fuzzy_qry(school_name,N'樹林國小',1) desc

 

 

转载于:https://www.cnblogs.com/yyjj/p/3944508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值