Compare strings exactly in MySQL

To reliably compare the exact value of two fields you can use the md5 for comparison. This might be helpful if you are comparing two strings that have different case, or perhaps some hidden characters you can’t see.

For example typically in MySQL the following comparison of blog vs Blog will give you a match despite a difference in case because most servers use a case insensitive collation _ci.

select case 
when 'blog' = 'Blog'
then 'match'
else 'no match'
end as result;
Result: match

Yes you could play around with collations in the session for this to work but a far simpler approach is using md5:

select case 
when md5('blog') = md5('Blog')
then 'match'
else 'no match'
end as result;
Result: no match

This can be applied for other non-printable/hidden characters in the column ensuring you do have an exact match on two values (or not).

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DESIGN OF A MULTI-AGENT SYSTEM FOR AUTONOMOUS

Your code tests are just a buggy mess!

Tailored business in Shopify

Tailored business in Shopify by VN Studios

Python Tutorial — Using Python to Create Simple Animations — Part 3

Presearch Weekly Update #54 — Feb 11, 2022

PokémonGO spoof in Android 7. No root or Twrp

How to be Agile at Work and in Life

Learning: Developers’ stories

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praj Basnet

Praj Basnet

More from Medium

SQL Pivot Table complete solution

SQL vs NoSQL, Which is better?

Oracle Export-Import — Refresh Table

NOSQL VS SQL MISCONCEPTIONS