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


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)

Updated on: 26-Feb-2020

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements