MySQL query to display all the fields that contain a capital letter?

MySQLMySQLi Database

To display all the fields that contain a capital letter, use the RLIKE that performs a pattern match of a string expression against a pattern.

Let us first create a table −

mysql> create table contains_capital_letterDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (1.42 sec)

Following is the query to insert some records in the table using insert command −

mysql> insert into contains_capital_letterDemo(Name) values('Larry');
Query OK, 1 row affected (0.17 sec)

mysql> insert into contains_capital_letterDemo(Name) values('larry');
Query OK, 1 row affected (0.12 sec)

mysql> insert into contains_capital_letterDemo(Name) values('john');
Query OK, 1 row affected (0.13 sec)

mysql> insert into contains_capital_letterDemo(Name) values('JOHN');
Query OK, 1 row affected (0.36 sec)

mysql> insert into contains_capital_letterDemo(Name) values('mike');
Query OK, 1 row affected (0.14 sec)

mysql> insert into contains_capital_letterDemo(Name) values('Mike');
Query OK, 1 row affected (0.27 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from contains_capital_letterDemo;

This will produce the following output −

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Larry |
| 2  | larry |
| 3  | john  |
| 4  | JOHN  |
| 5  | mike  |
| 6  | Mike  |
+----+-------+
6 rows in set (0.00 sec)

Following is the query to display all the fields that contain a capital letter −

mysql> select * from contains_capital_letterDemo WHERE CAST(Name AS BINARY) RLIKE
'[A-Z]';

This will produce the following output −

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Larry |
| 4  | JOHN  |
| 6  | Mike  |
+----+-------+
3 rows in set (0.00 sec)
raja
Published on 16-Apr-2019 13:27:14
Advertisements