1

Strangely when I compare '-' (hyphen) with a '_' (underscore) , I'm getting weird results. Although I expect the output of both the below statements to be false , the first one returns true while the second one returns false.

  1. Select case when '-' like '_' then 'true' else 'false' end -- returns true
  2. Select case when '_' like '-' then 'true' else 'false' end -- returns false

Any clarification would be helpful.

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Girija Acharya
  • 31
  • 1
  • 10

1 Answers1

3

the underscore (_) and the percent(%) are wildcards when used in SQL LIKE clause

if you want these characters to be interpreted not as wildcards, you must escape them, like this

http://www.sqldbpros.com/2013/01/escaping-from-an-underscore-in-a-sql-server-wildcard-like-search/

Leo
  • 6,480
  • 4
  • 37
  • 52