MySQL: is a SELECT statement case sensitive?

MysqlSelect

Mysql Problem Overview


Can anyone tell me if a MySQL SELECT query is case sensitive or case insensitive by default? And if not, what query would I have to send so that I can do something like:

SELECT * FROM `table` WHERE `Value` = "iaresavage"

Where in actuality, the real value of Value is IAreSavage.

Mysql Solutions


Solution 1 - Mysql

They are case insensitive, unless you do a binary comparison.

Solution 2 - Mysql

You can lowercase the value and the passed parameter :

SELECT * FROM `table` WHERE LOWER(`Value`) = LOWER("IAreSavage")

Another (better) way would be to use the COLLATE operator as said in the documentation

Solution 3 - Mysql

Comparisons are case insensitive when the column uses a collation which ends with _ci (such as the default latin1_general_ci collation) and they are case sensitive when the column uses a collation which ends with _cs or _bin (such as the utf8_unicode_cs and utf8_bin collations).

Check collation

You can check your server, database and connection collations using:

mysql> show variables like '%collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+

and you can check your table collation using:

mysql> SELECT table_schema, table_name, table_collation 
       FROM information_schema.tables WHERE table_name = `mytable`;
+----------------------+------------+-------------------+
| table_schema         | table_name | table_collation   |
+----------------------+------------+-------------------+
| myschema             | mytable    | latin1_swedish_ci |

Change collation

You can change your database, table, or column collation to something case sensitive as follows:

-- Change database collation
ALTER DATABASE `databasename` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

-- or change table collation
ALTER TABLE `table` CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

-- or change column collation
ALTER TABLE `table` CHANGE `Value` 
    `Value` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin;

Your comparisons should now be case-sensitive.

Solution 4 - Mysql

USE BINARY

This is a simple select

SELECT * FROM myTable WHERE 'something' = 'Something'

= 1

This is a select with binary

SELECT * FROM myTable WHERE BINARY 'something' = 'Something'

or

SELECT * FROM myTable WHERE 'something' = BINARY 'Something'

= 0

Solution 5 - Mysql

String comparison in WHERE phrase is not case sensitive. You could try to compare using

WHERE `colname` = 'keyword'

or

WHERE `colname` = 'KeyWord'

and you will get the same result. That is default behavior of MySQL.

If you want the comparison to be case sensitive, you could add COLLATE just like this:

WHERE `colname` COLLATE latin1_general_cs = 'KeyWord'

That SQL would give different result with this one: WHERE colname COLLATE latin1_general_cs = 'keyword'

latin1_general_cs is common or default collation in most database.

Solution 6 - Mysql

The collation you pick sets whether you are case sensitive or not.

Solution 7 - Mysql

The default is case insensitive, but the next most important thing you should take a look at is how the table was created in the first place, because you can specify case sensitivity when you create the table.

The script below creates a table. Notice down at the bottom it says "COLLATE latin1_general_cs". That cs at the end means case sensitive. If you wanted your table to be case insensitive you would either leave that part out or use "COLLATE latin1_general_ci".

   CREATE Table PEOPLE (

       USER_ID  INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,

       FIRST_NAME  VARCHAR(50) NOT NULL,
       LAST_NAME  VARCHAR(50) NOT NULL,

       PRIMARY KEY (USER_ID)

   )

   ENGINE=MyISAM DEFAULT CHARACTER SET latin1
    COLLATE latin1_general_cs AUTO_INCREMENT=0;

If your project is such that you can create your own table, then it makes sense to specify your case sensitivity preference when you create the table.

Solution 8 - Mysql

Solution 9 - Mysql

Marc B's answer is mostly correct.

If you are using a nonbinary string (CHAR, VARCHAR, TEXT), comparisons are case-insensitive, per the default collation.

If you are using a binary string (BINARY, VARBINARY, BLOB), comparisons are case-sensitive, so you'll need to use LOWER as described in other answers.

If you are not using the default collation and you are using a nonbinary string, case sensitivity is decided by the chosen collation.

Source: https://dev.mysql.com/doc/refman/8.0/en/case-sensitivity.html. Read closely. Some others have mistaken it to say that comparisons are necessarily case-sensitive or insensitive. This is not the case.

Solution 10 - Mysql

Try with:

order by lower(column_name) asc;

Solution 11 - Mysql

Note also that table names are case sensitive on Linux unless you set the lower_case_table_name config directive to 1. This is because tables are represented by files which are case sensitive in Linux.

Especially beware of development on Windows which is not case sensitive and deploying to production where it is. For example:

"SELECT * from mytable" 

against table myTable will succeed in Windows but fail in Linux, again, unless the abovementioned directive is set.

Reference here: http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html

Solution 12 - Mysql

You can try it. hope it will be useful.

SELECT * FROM `table` WHERE `Value` COLLATE latin1_general_cs = "IAreSavage"

Solution 13 - Mysql

String fields with the binary flag set will always be case sensitive. Should you need a case sensitive search for a non binary text field use this: SELECT 'test' REGEXP BINARY 'TEST' AS RESULT;

Solution 14 - Mysql

In my case neither BINARY nor COLLATE nor CHARACTER SET works with my UTF8 table.

I have usernames in my table like henry, Henry, susan, Susan or suSan and find the respective users by comparing the byte sequences of the names.

The following function creates the byte sequences:

function makeByteString($string){
	$tmp = "";
	for($i=0;$i<strlen($string);$i++){
		$sign = substr($string,$i,1);
		$tmp.=ord($sign);
	}
	return $tmp;
}

The SQL query finds the correct id:

$sql = "SELECT id, username FROM users WHERE `username` = ? ";
$stmt = $conn->prepare($sql);
$stmt->execute([$strUsername]); //e.g. susan, Susan or suSan
$rows = $stmt->rowCount();
if($stmt && $rows>0){
  while ($row = $stmt->fetch()) {
    if(makeByteString($strUsername) == 
                   makeByteString(trim($row["username"]))){
	  $id = $row['id'];
	}
  }
}	

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoodleOfDeathView Question on Stackoverflow
Solution 1 - MysqlMarc BView Answer on Stackoverflow
Solution 2 - MysqlColin HebertView Answer on Stackoverflow
Solution 3 - MysqlostrokachView Answer on Stackoverflow
Solution 4 - MysqlSomeoneView Answer on Stackoverflow
Solution 5 - MysqlIfan IqbalView Answer on Stackoverflow
Solution 6 - Mysqlchuck taylorView Answer on Stackoverflow
Solution 7 - MysqlKen JohnsonView Answer on Stackoverflow
Solution 8 - MysqlJacob NelsonView Answer on Stackoverflow
Solution 9 - MysqlA248View Answer on Stackoverflow
Solution 10 - MysqlVS-javaView Answer on Stackoverflow
Solution 11 - MysqlWes GrantView Answer on Stackoverflow
Solution 12 - MysqlZahidView Answer on Stackoverflow
Solution 13 - Mysqluser2288580View Answer on Stackoverflow
Solution 14 - Mysqlguenter47View Answer on Stackoverflow