select * from t where t.content like N'%%%'这条语句会把content记录中没有%的也查出来:
22222222
1111%111怎样让他只查出1111%111这一条记录
谢谢

解决方案 »

  1.   

    select * from t where charindex('%',content)>0
      

  2.   

    select * from t where charindex('%',content )>0
      

  3.   

    select * from t where t.content like N'%%%' escape '%'
      

  4.   

    select * from t where content like '%!%%' escape '!'
      

  5.   

    这个是可以,
    但是程序中是个sql模板字符串WHERE content like N'%_KEYWORD%'
    调用时,将_KEYWORD置换为用户输入的字符串,所以使用charindex只能解决这一个问题,不能通用啊
      

  6.   


    DECLARE @keyword varchar(100)
    SET @keyword = '%'SET @keyword = REPLACE(@keyword,'\','\\')
    SET @keyword = REPLACE(@keyword,'%','\%')
    SET @keyword = REPLACE(@keyword,'_','\_')
    SET @keyword = REPLACE(@keyword,'[','\[')
    SET @keyword = REPLACE(@keyword,']','\]')SELECT *
    FROM (
    SELECT string = 'abc\de' UNION ALL
    SELECT string = 'abc%de' UNION ALL
    SELECT string = 'abc_de' UNION ALL
    SELECT string = 'abc[de' UNION ALL
    SELECT string = 'abc]de' UNION ALL
    SELECT string = 'abc[^d]e' UNION ALL
    SELECT string = 'ab[c-d]e'
    ) tmp
    WHERE string LIKE '%' + @keyword + '%' ESCAPE '\'
      

  7.   

    select * from t where t.content like N'%%%' escape '%'
      

  8.   

    select * from t where t.content like N'%[%]%'
    将%用[]括起来就OK了,在程序调用时,将where条件中的所有的的%替换为[%]。