Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('Adam Smith');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('John Smith');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Carol Smith');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+-------------+
| Name        |
+-------------+
| Chris Brown |
| Adam Smith  |
| John Smith  |
| Carol Smith |
+-------------+
4 rows in set (0.00 sec)

Following is the query to implement SELECT LIKE and CHAR_LENGTH −

mysql> select * from DemoTable
   -> where Name LIKE '%Smith%'
   -> and char_length(Name) < 11;

This will produce the following output −

+------------+
| Name       |
+------------+
| Adam Smith |
| John Smith |
+------------+
2 rows in set (0.00 sec)
raja
Published on 18-Dec-2019 06:09:31
Advertisements