0

I have a varchar column inside a very simple MySQL Database table that contains 2 different types of product references :

one like : 14521_451_288, and the others like 45742154

to be able to manage them, I'm using the following code :

WHERE ref LIKE '%_%'

But all the results are returned, even the references without underscore. Am I missing something basic here?

Vincent Duprez
  • 3,772
  • 8
  • 36
  • 76
  • _ stand for any one character in a LIKE expression. That's why you return all values. You need to escape the _ character. – Joseph B May 05 '14 at 15:07

2 Answers2

1

_ stand for any one character in a LIKE expression. That's why you return all values. You need to escape the _ character.

Try this:

WHERE ref LIKE '%\_%';

References:

String Literals on MySQL Reference Manual

Related SO question

Community
  • 1
  • 1
Joseph B
  • 5,519
  • 1
  • 15
  • 19
0

Try using

where ref <> instr('_') > 0

because _ stands for any single character in the like expression while % stands for any characters in the expression.

juergen d
  • 201,996
  • 37
  • 293
  • 362