Case sensitive matches in MySQL

This is an interesting tidbit that I came across in my aimless wanderings around on the Internet. Unfortunately I don’t remember the place that I found it – it was during a random Google search that I had made buried in the second page somewhere.

So the requirement was to do a case sensitive search for values in a MySQL table. So if you wanted to search for records that contained the term ‘foo’ in the title from a table ‘FooName’ containing the following records -

FooName

Title Description
foobar a bar of foo
Foo royal foo
Fooka car of foo
foo just foo

The typical select query -

Select Title from FooName where Title like '%foo%'

Returns

foobar
Foo
Fooka
foo

This may not be exactly what you wanted since ‘foo’ is all in lowercase and the query above returns all values that have the letters ‘foo’ in that order regardless of case. In order to ensure case sensitivity in MySQL just modify your query like so -

Select Title from FooName where binary Title like '%foo%'

Returns

foobar
foo

About these ads

One comment on “Case sensitive matches in MySQL

Comments are closed.