惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
罗磊的独立博客
月光博客
月光博客
GbyAI
GbyAI
腾讯CDC
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
雷峰网
雷峰网
B
Blog RSS Feed
美团技术团队
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
D
Docker

博客园 - PC167

caiquan0 sheeeeee loggggg ato ctrl const loginnnnn pauto Shenmi llllll controller ctypes helper python test 1 Index was outside the bounds of the array. (Microsoft.SqlServer.Smo) contains 方法 mysql connector 和 sqlserver ado.net 的区别 javascript slice javascript bind 转,SelectNodes + XPath jsgen 搭建 【转】基于第一个PhoneGap(cordova)的应用详解 回车和换行 html中空格转义字符 输出乘法表 sql server 2008 r2 报错 razor 拼接字符串 转,CV和resume的区别 b/s开发者的困境 sql 下,float和numeric
sql server 相似度对比
PC167 · 2014-08-29 · via 博客园 - PC167

转自: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