Case-sensitive string comparison in SQL (MariaDB, MySQL)

1.4k views Asked by At

I have read many questions and answers on StackOverflow (posted years ago) concerning this issue, but none that I found have worked using a 10.3.22-MariaDB server and libmysql - 5.6.43 as database client.

I am trying to do the following: SELECT * FROM table WHERE 'column' = 'data' LIMIT 1
The command above, however, will select the row both if the value of column is DaTa or data and I am endeavouring to go for the latter only.

Answers to similar questions from 2011 (MySQL) suggest:

SELECT * FROM table WHERE BINARY 'column' = 'data'
SELECT * FROM table WHERE 'column' = BINARY 'data'
SELECT * FROM table WHERE 'column' LIKE BINARY 'data'
SELECT * FROM table WHERE 'column' LIKE 'data' COLLATE latin1_general_cs

None of the commands above respected the case of the letters. Moreover, the last one did not even run due to

  1. Unrecognized keyword. (near "COLLATE" at position 45)
  2. Unexpected token. (near "latin1_general_cs" at position 53)

What is the solution to this problem in 2020? What am I doing wrong or what is wrong with my server?
Any help would be highly appreciated!

2

There are 2 answers

0
Gordon Linoff On BEST ANSWER

This should work:

WHERE BINARY column = 'data'

However, it will not work if you put column in single quotes. That would be a string as opposed to a column reference.

5
nbk On

This works without the single quotes around cloumn

SELECT * FROM table1 WHERE `column` LIKE 'data' COLLATE latin1_general_ci;

As you asked the collation has to fir to the character set, so if you have like me utf8mb5, you write:

SELECT * FROM table1 WHERE `column` LIKE _latin1 'data'   COLLATE latin1_general_ci;