+ -
当前位置:首页 → 问答吧 → 模糊匹配的SQL语句写法

模糊匹配的SQL语句写法

时间:2011-12-10

来源:互联网

有两个表table a,table b 均有一个字符串字段str.

1)字符串形式为字符+空格间隔的组织,空格的个数不确定 
“XXXX XXXXX XXXXXXX”

2)现在要做的事情是,将在B表中找到这样的记录,能够满足以下条件,

  A的str中被空格间格的子串均为B.str的子集,如:

a.str为:'6688 sasaaaa wty',则b.str的如下记录可以满足要求:

6688sasaaaawty
wtysasaaaa6688

6688sasaaarytywty
1236688sasaaaa7wihwty

因为以上4条记录均包括 a.str的三个子串。



作者: danielxiong   发布时间: 2011-12-10

把a的字串分出来,然后进行like

作者: gold_water   发布时间: 2011-12-10

按空格切分字符串再做比较吧。

作者: ssp2009   发布时间: 2011-12-10

SQL code

--SQL去除字符串中连续的分割符
create function [dbo].[m_delrepeatsplit] 
( 
    @str varchar(2000), 
    @split nvarchar(200) 
) 
returns nvarchar(2000) 
as   
begin   
       declare @count int,@i int,@isnull int 
       declare @newchar nvarchar(200),@nn nvarchar(300) 
       set @count=len(@str);set @i=1;set @isnull=1;set @nn=''; 

       while @i<@count+1 
       begin 
           set @newchar=substring(@str,@i,1) 
           if(@isnull=1) 
           begin 
              set @nn=@nn+@newchar;    
              if(@newchar=@split) 
                  begin 
                     set @isnull=0; 
                  end 
              else 
                  begin 
                     set @isnull=1; 
                  end    
           end 
           else 
              begin 
                  if(@newchar=@split) 
                     begin 
                         set @isnull=0; 
                     end 
                  else 
                     begin 
                         set @nn=@nn+@newchar;    
                         set @isnull=1; 
                     end    
              end 
           set @i=@i+1; 
       end 
   
    return  @nn 
end 


----测试
create table tbA
(
  [str] varchar(100) 
)

create table tbB
(
  [str] varchar(100) 
)

insert into tbA select '6688      sasaaaa  wty'

insert into tbB 
select '6688sasaaaawty' union all
select 'wtysasaaaa6688' union all
select '6688sasaaarytywty' union all
select '1236688sasaaaa7wihwty' union all
select '111'

;with cte as
(
select substring(t2.[str],number,charindex(' ',t2.[str]+' ',number)-number) AS [str]
  from master..spt_values t1,(select [dbo].[m_delrepeatsplit](str,' ') AS [str]  from tbA) t2
  where type='p' and number<=len(t2.[str]) 
  and charindex(' ',' '+t2.[str],number)=number
)
select * from tbB B where exists(select 1 from cte where  CHARINDEX([str],B.[str])>0)

--结果
str
6688sasaaaawty
wtysasaaaa6688
6688sasaaarytywty
1236688sasaaaa7wihwty

作者: koumingjie   发布时间: 2011-12-10

引用 3 楼 koumingjie 的回复:
SQL code


--SQL去除字符串中连续的分割符
create function [dbo].[m_delrepeatsplit]
(
@str varchar(2000),
@split nvarchar(200)
)
returns nvarchar(2000)
as
begin
declare @count……


方法有写复杂,关键是A表中有重复的空格,应先去除,再将str按空格拆分

6688
sasaaaa
wty

最后用charindex判断

作者: koumingjie   发布时间: 2011-12-10

引用 3 楼 koumingjie 的回复:

SQL code

--SQL去除字符串中连续的分割符
create function [dbo].[m_delrepeatsplit]
(
@str varchar(2000),
@split nvarchar(200)
)
returns nvarchar(2000)
as
begin
declare @count int,@i int……

已经有很多正解了

作者: bushy   发布时间: 2011-12-10

请教,我在sql server 2000下运行不了呢? 不知道是何原因?

报 ;with cte as
  第 1 行: ';' 附近有语法错误。

再次求教

作者: danielxiong   发布时间: 2011-12-11

是不是 2000 缓存写法不同,我是菜鸟,再请教

作者: danielxiong   发布时间: 2011-12-11

查了一下,发现with cte 是2005的写法,2000中该如何写呀,再谢

作者: danielxiong   发布时间: 2011-12-11