Get the names beginning with a particular character using LIKE in MySQL

MySQLMySQLi Database

To get the names beginning with a particular character, you need to use LIKE. Let us first create a table:

mysql> create table DemoTable
(
   StudentFirstName varchar(20)
);
Query OK, 0 rows affected (1.01 sec)

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

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Johnny');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Ramit');
Query OK, 1 row affected (0.18 sec)

Following is the query to display records from the table using select command:

mysql> select *from DemoTable;

This will produce the following output:

+------------------+
| StudentFirstName |
+------------------+
| John             |
| Carol            |
| Johnny           |
| Robert           |
| Chris            |
| Ramit            |
+------------------+
6 rows in set (0.00 sec)

Following is the query to match the first character with LIKE: i.e. all students with first name beginning from character C:

mysql> select *from DemoTable where StudentFirstName LIKE 'C%';

This will produce the following output:

+------------------+
| StudentFirstName |
+------------------+
| Carol            |
| Chris            |
+------------------+
2 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 08:28:27
Advertisements