Why does using an Underscore character in a LIKE filter give me all the results?

后端 未结 6 1695
我在风中等你
我在风中等你 2020-11-29 00:52

I wrote the below SQL query with a LIKE condition:

SELECT * FROM Manager
WHERE managerid LIKE \'_%\'
AND managername LIKE \'%_%\'
6条回答
  •  悲&欢浪女
    2020-11-29 01:35

    The underscore is the wildcard in a LIKE query for one arbitrary character.

    Hence LIKE %_% means "give me all records with at least one arbitrary character in this column".

    You have to escape the wildcard character, in sql-server with [] around:

    SELECT m.* 
    FROM Manager m 
    WHERE m.managerid    LIKE  '[_]%'
    AND   m.managername  LIKE '%[_]%'
    

    See: LIKE (Transact-SQL)

    Demo

提交回复
热议问题