假如一个表行中内容包含有%.
要查找出这条记录,用语句select * from table where Content like '%%%'这样搜索出来的是全部内容.那要怎么样搜索出含有%的记录呢?大家都来说说!

解决方案 »

  1.   

    select * from table where Content like '%[%]%' 
      

  2.   

    create table #(name varchar(20))
    insert into # select 'aa%aaa'
    insert into # select 'bbbbb'
    select * from # where name like '%[%]%'
    /*
    aa%aaa
    */
    select * from # where charindex('%',name)>0
    /*
    aa%aaa
    */
      

  3.   

    select * from # where name like '%/%%' escape '/'
      

  4.   

    select * from # where name like '%!%%' escape '!'
      

  5.   


    select * from table where Content like '%[%]%' 
    --或者:
    select * from table where Content like '%&%%' escape '&'
      

  6.   

    charindex()
    pathindex
    一般情况下我很少用like
      

  7.   


    select * from table where Content like '%[%]%' select * from table where charindex('%',Content)>0select * from table where pathindex('%',Content)>0
      

  8.   

    一条语句的多条不同写法。
    为什么不用LIKE,是效率低吗?
      

  9.   

    都不错啊不过
    1.我测试发现chindex不能用在text,ntext这些字段.like就可以.但用在字符串中是很不错的
    2.用[],有些查找是不正确的,因为[]是指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
    所以select * from t1 where username like '%[!!![-dddd--]]%'
    查找出来的结果有
    username                                                                                            ------------ 
    11[][][]  
    ,这样只要有单个字符[就条件满足,显然跟我们原来的条件不同.
    3.用 like ...escape 最符合条件
    select * from t1
    where username like '%![!!![-dddd--]]%' escape '!'但是用这个如果遇到通配符的话也要用指定的escape符号隔开才有用
    select username from t1
    where username like '%![]%' escape '!'username                                                                                             
    ----------------------------
    11[][][](所影响的行数为 1 行)
    这样却得不到数据,因为里面的通配符没有隔开
    select username from t1
    where username like '%!11[][][]%' escape '!'
    username                                                                                             
    ------------------------------------- (所影响的行数为 0 行)
    换成隔开的就行了
    select username from t1
    where username like '%!11![]![]![]%' escape '!'
    username                                                                                             
    ----------------------------------------- 
    11[][][](所影响的行数为 1 行)
    效率的问题大概都不多吧.不知道我的测试有没有问题就是.欢迎高手指正
      

  10.   

    [code]
    --这样测试的
    declare @i int
    declare @count int
    declare @temptime int
    set @temptime=0
    set @count=0
    set @i=0
    while (@i<100)
    begin
    declare @time1 datetime
    set @time1=getdate()
    select *
    from testsearch
    --where TextContent like '%[%]%'
    ---不能用where charindex('%',TextContent)>0
    where TextContent like '%!%%' escape '!'
    set @temptime=datediff(millisecond,@time1,getdate())
    set @count=@count+@temptime
    select '平均时间为'=@count/100
    set @i=@i+1
    end
    go
    [/code]
      

  11.   


    --这样测试的
    declare @i int
    declare @count int
    declare @temptime int
    set @temptime=0
    set @count=0
    set @i=0
    while (@i<100)
    begin
    declare @time1 datetime
    set @time1=getdate()
    select *
    from testsearch
    --where TextContent like '%[%]%'
    ---不能用where charindex('%',TextContent)>0
    where TextContent like '%!%%' escape '!'
    set @temptime=datediff(millisecond,@time1,getdate())
    set @count=@count+@temptime
    select '平均时间为'=@count/100
    set @i=@i+1
    end
    go
      

  12.   

    select * from table where Content like '\%%'escape'\' 搜出以%开头的任何字符如'%abc'select * from table where Content like '%\%%'escape'\' 搜出包含%的任何字符如'abc%def'select * from table where Content like '\%\%\%'escape'\' 搜出只有三个%的元组其他类推
      

  13.   

    使用 ESCAPE 关键字定义转义符
    这样就可以了
    如:
    Select * From UserInfo Where Address like '%/%%' escape '/'
      

  14.   

    e.g:% 包含零个或多个字符的任意字符串。
    WHERE title LIKE '%computer%' 将查找在书名中任意位置包含单词 "computer" 的所有书名。
    _(下划线) 任何单个字符。
    WHERE au_fname LIKE '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(Dean、Sean 等)。
    [ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
    WHERE au_lname LIKE '[C-P]arsen' 将查找以 arsen 结尾并且以介于 C 与 P 之间的任何单个字符开始的作者姓氏,例如 Carsen、Larsen、Karsen 等。[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。
    WHERE au_lname LIKE 'de[^l]%' 将查找以 de 开始并且其后的字母不为 l 的所有作者的姓氏。