Saturday 30 December 2017

How can I make SQL case sensitive string comparison on MySQL?

itemprop="text">

I have a function that returns five
characters with mixed case. If I do a query on this string it will return the value
regardless of case.



How can I make MySQL string
queries case sensitive?


itemprop="text">
class="normal">Answer



href="http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html"
rel="noreferrer">http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html






The default character set and collation are latin1 and latin1_swedish_ci, so
nonbinary string comparisons are case insensitive by default. This means that if you
search with col_name LIKE 'a%', you get all column values that start with A or a. To
make this search case sensitive, make sure that one of the operands has a case sensitive
or binary collation. For example, if you are comparing a column and a string that both
have the latin1 character set, you can use the COLLATE operator to cause either operand
to have the latin1_general_cs or latin1_bin
collation:




col_name
COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE
latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name
LIKE 'a%' COLLATE
latin1_bin





If you want a column always to be treated in case-sensitive fashion, declare it
with a case sensitive or binary
collation.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...