
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to match underscore in a MySQL String?
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)
- Related Articles
- MySQL query to change a string by displaying only the part of string after underscore?
- MySQL Regular expressions: How to match digits in the string with d?
- Parse a string to get a number from a large string separated by underscore
- How to match a regular expression against a string?
- Underscore as a table name in MySQL is possible?
- Can we give underscore in a MySQL table name?
- Can 'false' match some string in MySQL?
- MySQL ORDER BY strings with underscore?
- Select rows containing a string in a specific column with MATCH and AGAINST in MySQL
- Update a column in MySQL and remove the trailing underscore values
- How to match a particular word in a string using Pattern class in Java?
- How to query an Array[String] for a Regular Expression match?
- How can we add an underscore before each capital letter inside a java String?
- How to match one character in MySQL in place of %?
- Using MySQL IN() for some column values with underscore

Advertisements