To match underscore in a MySQL string, you can use the below syntax −
select *from yourTableName where yourColumnName LIKE '%\_%';
Let us first create a table −
mysql> create table DemoTable ( ClientId varchar(200) ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('CLI_101'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('CLI1110'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('_CLI102'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('CLI103_'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('CLI1111'); Query OK, 1 row affected (0.16 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+ | ClientId | +----------+ | CLI_101 | | CLI1110 | | _CLI102 | | CLI103_ | | CLI1111 | +----------+ 5 rows in set (0.00 sec)
Here is the query to match underscore in String using LIKE −
mysql> select *from DemoTable where ClientId LIKE '%\_%';
This will produce the following output displaying the strings with underscore −
+----------+ | ClientId | +----------+ | CLI_101 | | _CLI102 | | CLI103_ | +----------+ 3 rows in set (0.00 sec)
You can use alternate query −
mysql> select *from DemoTable where ClientId REGEXP '_';
This will produce the following output −
+----------+ | ClientId | +----------+ | CLI_101 | | _CLI102 | | CLI103_ | +----------+ 3 rows in set (0.00 sec)