MySQL query to find all rows where string contains less than four characters?


Use CHAR_LENGTH() and find the count of characters in every string and then get the strings which are less than four characters. Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (1.60 sec)

mysql> insert into DemoTable values('Carol');
Query OK, 1 row affected (0.25 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (1.83 sec)

mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.32 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Name  |
+-------+
| John  |
| Bob   |
| Carol |
| David |
| Sam   |
+-------+
5 rows in set (0.00 sec)

Following is the query to find all rows where string contains less than four characters −

mysql> select *from DemoTable where CHAR_LENGTH(Name) < 4;

This will produce the following output −

+------+
| Name |
+------+
| Bob  |
| Sam  |
+------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements