How to find capital letters with Regex in MySQL?


You can use REGEXP BINARY for this

select *from yourTableName where yourColumnName REGEXP BINARY '[A-Z]{2}';

Let us first create a table

mysql> create table FindCapitalLettrsDemo
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentFirstName varchar(20)
   -> );
Query OK, 0 rows affected (0.52 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('JOHN');
Query OK, 1 row affected (0.24 sec)
mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('Carol');
Query OK, 1 row affected (0.15 sec)
mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('bob');
Query OK, 1 row affected (0.14 sec)
mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('carol');
Query OK, 1 row affected (0.17 sec)
mysql> insert into FindCapitalLettrsDemo(StudentFirstName) values('John');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from FindCapitalLettrsDemo;

The following is the output

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
|         1 | JOHN             |
|         2 | Carol            |
|         3 | bob              | 
|         4 | carol            |
|         5 | John             |
+-----------+------------------+
5 rows in set (0.00 sec)

Here is the query to find capital letters in MySQL

mysql> select *from FindCapitalLettrsDemo
   -> where StudentFirstName REGEXP BINARY '[A-Z]{2}';

The following is the output

+-----------+------------------+
| StudentId | StudentFirstName |
+-----------+------------------+
|         1 | JOHN             |
+-----------+------------------+
1 row in set (0.14 sec)

Updated on: 30-Jul-2019

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements