一个SQL语句问题,我的一个字段里面有% 和_ 我怎么才能够查出来呢?以前我一直用
CString str ;
str.format(select * from biao where   ziduan like '%%s%%', name);
可是当name 是% 和_ 就显示所有结果了 我该怎么查

解决方案 »

  1.   

    create table tb (col varchar(20))insert into tb select 'ab%bb'
    insert into tb select 'fdfasf'select * from tb where col like '%b\%%' escape '\'
    drop table tb
      

  2.   

    create table tb (col varchar(20))insert into tb select 'ab%bb'
    insert into tb select 'fdfasf'
    insert into tb select 'fdf%_fdf'select * from tb where col like '%b\%%' escape '\'/*
    col
    --------
    ab%bb
    */select * from tb where col like '%\%_%' escape '\'
    /*
    col
    --------
    ab%bb
    fdf%_fdf
    */drop table tb
      

  3.   

    借楼上朋友的数据create table tb (col varchar(20))
    go
    insert into tb select 'ab%bb'
    insert into tb select 'fdfasf'select * from tb where col like '%b\%%' escape '\'select * from tb where col like '%b[%]%'
    godrop table tb
    go
      

  4.   

    select * from tb
    col                  
    -------------------- 
    ab%bb
    fdfasf
    fdf%_fdf
    fdbf%_fdf
    fdbfb%_fdf
    fdfbasf
    fdbfb%h_fdf(所影响的行数为 7 行)select * from tb where col like '%[%]h%' 
    col                  
    -------------------- 
    fdbfb%h_fdf(所影响的行数为 1 行)
      

  5.   

    多谢楼上的几位大哥 现在又有个新问题了,,就是我现在要查询带单引号的 数据,,可是加\ escape就不管用了 ‘应该怎么查询啊?
      

  6.   


    create table tb (col varchar(20))insert into tb select 'ab%bb'
    insert into tb select 'fdfasf'
    insert into tb select 'fdf%_fdf'
    insert into tb select 'fd'''
    insert into tb select 'fd''fdfdasf'select * from tb where col like '%d\''%' escape '\'/*
    col
    --------
    fd'
    fd'fdfdasf
    */
    drop table tb
      

  7.   

     在C语言中  使用\对特殊字符转义比如我们要表示路径 在其他语言了 "C:\Documents and Settings\guest\桌面" 就可以了但是在C里面 要
    "C:\\Documents and Settings\\guest\\桌面"   这是对后面的\进行转义
      

  8.   


    [转]:http://topic.csdn.net/t/20050916/13/4275000.html
    1.两个双引号相当于一个单引号   
            select   ''''   
    2.like中可以用escape转义   
            SELECT   notes   
            FROM   titles   
            WHERE   notes   LIKE   '50%%   off   when   100   or   more   copies   are   purchased'     
            ESCAPE   '%'  
      

  9.   

    create table #t 
    (
    col varchar(20)
    )
    goinsert into #t select 'ab%bb'
    insert into #t select 'fdfasf'select * from #t where col like '%b\%%' escape '\'select * from #t where col like '%b[%]%'
    godrop table #t 
    go
      

  10.   

    create table tb (col varchar(20))insert into tb select 'ab%bb'
    insert into tb select 'fdfasf'select * from tb where col like '%b\%%' escape '\'
    drop table tb
      

  11.   

      select * from tb where name like 'b\_\''\%_a' escape '\'