MySQL case-sensitive LIKE search

less than 1 minute read

When searching for partial strings in MySQL with LIKE you will match case-insensitive by default.

SELECT name FROM users WHERE name LIKE 't%'
+--------------------+
| name               |
+--------------------+
| Test               |
| test               |
+--------------------+

If you want to match case-sensitive, you can cast the value as binary and then do a byte-by-byte comparision vs. a character-by-character comparision. The only thing you need to add to your query is BINARY.

SELECT name FROM users WHERE name LIKE BINARY 't%'
+--------------------+
| name               |
+--------------------+
| test               |
+--------------------+

Leave a Comment