MySQL select for exact case sensitive match with hyphen in records


For exact case sensitive match, use BINARY after WHERE clause in MySQL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> EmployeeCode varchar(100)
   -> );
Query OK, 0 rows affected (0.64 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('EMP-1122');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('emp-1122');
Query OK, 1 row affected (0.43 sec)

mysql> insert into DemoTable values('EMP-6756');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('EMP-8775');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| EmployeeCode |
+--------------+
| EMP-1122     |
| emp-1122     |
| EMP-6756     |
| EMP-8775     |
+--------------+
4 rows in set (0.00 sec)

Following is the query to fetch record with exact case sensitive match −

mysql> select *from DemoTable WHERE BINARY  EmployeeCode='EMP-1122';

This will produce the following output −

+--------------+
| EmployeeCode |
+--------------+
| EMP-1122     |
+--------------+
1 row in set (0.00 sec)
raja
Published on 11-Jul-2019 15:13:18
Advertisements