Case
Case

Reputation: 4272

SQL wildcard Oddities, need stricter results

SELECT *
FROM list
WHERE list_item LIKE "%_1375_%"

returns results it should not. For example a result with 13753 in the list_item instead of 1375, even though the 3 comes before the underscore.

 _13753_  and _1375_ are written above and not suppose to be italic.

Is there anyway to fix that?

Upvotes: 0

Views: 73

Answers (3)

Usman
Usman

Reputation: 2345

Try this one

LIKE '%[_]1375[_]%'

Upvotes: 0

Benjamin Cox
Benjamin Cox

Reputation: 6120

From MySQL reference manual here

SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters).

So, you'll have to escape them to match a literal underscore character. Like so:

SELECT * FROM list
WHERE list_item LIKE "%\_1375\_%"

Upvotes: 2

SQLMenace
SQLMenace

Reputation: 134941

Underscore means any character. Are you looking for values with an underscore? In that case you need to escape it

SELECT *
FROM list
WHERE list_item LIKE "%\_1375\_%"

Upvotes: 1

Related Questions