现在要实现这样一个功能,如果数据库中存有数据如
a
ab
abc
abcdd
abcde
abcdf
但是我现在输入的查询条件是abcda,这样能查出与abcda最相近的单词
abcdd
abcde
abcdf

解决方案 »

  1.   


    关注:select * from tb where name LIKE 'abcda[a-z]'  or  charindex(substring('abcda',1,len('abcda')-1),'abcda')>1
      

  2.   

    我没有说清楚,大概类似金山词霸那样的查询结果,就是如果我要查的条件如abcda不存在,那么可以查询到与这个词最近的单词abcdd这样
      

  3.   

    SQL Server 2000提供了一个比较字符串相似性的函数——DIFFERENCE()。
      

  4.   

    试试这样看行不?create table #(code varchar(10))
    insert into #
    select 'a'
    union all select 'ab'
    union all select 'abc'
    union all select 'abcdd'
    union all select 'abcde'
    union all select 'abcdf'declare @a varchar(10)
    set @a='abcda'
    select * from # where code like left(@a,len(@a)-1)+'%'drop table #
      

  5.   

    这个在SQL中实现好像不是很好实现,在应用程序中较简单。DIFFERENCE()函数好像也不好用,得不到想要的结果。
      

  6.   

    select code from Table1
    WHERE Len(code) = Len('abcda')
    ORDER BY  DIFFERENCE('abcda', code) desc
      

  7.   

    我没试DIFFERENCE好不好使。如果自己来做这件事情,也未为不可:1.首先用完整的abcda作为条件来查询: SELECT * FROM TB1 WHERE C1='abcda'
    2.看返回结果集数量满不满足要求,如果需要其它相似结果,进行下一步。
    3.将abcda进行截位后模糊查询处理
       首先先截一位,得到abcd,用模糊查询  C1 LIKE 'abcd*'
       检查返回结果集数量,如需继续查询更多相似结果,则再截一位后用 C1 LIKE ‘abc*' 来查询。这种方式非常灵活,完全由自己控制。
      

  8.   

    上面只是说思路步骤,还原到程序也不难,利用循环就行了:FOR (I = 0;I < 字串长度函数(查询字串); I++)
    {
       查询字串=截串函数(查询字串,1 , 字串长度函数(查询字串)- I );
       //第一次执行时,I=0,得到结果仍是原串
       if (i==0) 
            {sqlStr = "SELECT * FROM 表 where c1='" + 查询字串 + "'";}
       else
            {sqlStr = "SELECT * FROM 表 where c1 LIKE '" + 查询字串 + "*'";}
       执行查询
       判断查询结果,如果满足要求,不打算继续查询,则退出循环
    }
      

  9.   

    create table #(code varchar(10))
    insert into #
    select 'a'
    union all select 'ab'
    union all select 'abc'
    union all select 'abcdd'
    union all select 'abcde'
    union all select 'abcdf'declare @a varchar(10)
    set @a='abcda'
    select * from # where code like left(@a,len(@a)-1)+'%'drop table #code       
    ---------- 
    abcdd
    abcde
    abcdf(所影响的行数为 3 行)沙果老弟的方法不错.
      

  10.   

    不会吧这么简单的问题?
    select * from table_name
    where code like 'abcd_'各位大师我是新手,不对的话请原谅!!
      

  11.   

    select * from 表名 where 要查询的字段 〉参数
    order by 要查询的字段
      

  12.   

    楼上的这个简单好用,theforever(碧海情天)谢谢碧海晴天,不过你的方法查询的数据会有重复,谢谢大家,我结帖了