How to make SQL case sensitive string comparison in MySQL?


Firstly, we will create a table with the help of CREATE command.

Creating a table −

mysql> CREATE table InCaseSensDemo
-> (
-> Name varchar(100)
-> );
Query OK, 0 rows affected (0.50 sec)

Inserting records into the table with the help of INSERT command −

mysql> INSERT into InCaseSensDemo values('JOhN');
Query OK, 1 row affected (0.11 sec)

mysql> INSERT into InCaseSensDemo values('bob');
Query OK, 1 row affected (0.21 sec)

mysql> INSERT into InCaseSensDemo values('BoB');
Query OK, 1 row affected (0.13 sec)

mysql> INSERT into InCaseSensDemo values('Bob');
Query OK, 1 row affected (0.18 sec)

Displaying all the records with the help of SELECT command −

mysql> SELECT * from InCaseSensDemo;

The following is the output −

+------+
| Name |
+------+
| JOhN |
| bob  |
| BoB  |
| Bob  |
+------+
4 rows in set (0.00 sec)

Here is the syntax TO make comparison of case sensitive string −

SELECT * FROM yourTableName WHERE BINARY column_name = 'value';

Applying the above syntax to compare the case sensitive value −

mysql> SELECT * FROM InCaseSensDemo WHERE BINARY Name = 'bob';

The following is the output

+------+
| Name |
+------+
| bob  |
+------+
1 row in set (0.00 sec)

Updated on: 25-Jun-2020

638 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements