Query results that have less than X characters in MySQL?


You can use CHAR_LENGTH() along with the WHERE clause. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> FullName varchar(50)
   -> );
Query OK, 0 rows affected (1.75 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris Brown');
Query OK, 1 row affected (0.40 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.91 sec)
mysql> insert into DemoTable values('Robert Miller');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('John Smith');
Query OK, 1 row affected (0.89 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| FullName      |
+---------------+
| Chris Brown   |
| David Miller  |
| Robert Miller |
| John Smith    |
+---------------+
4 rows in set (0.00 sec)

Here is the query to get records that have less than X characters in MySQL −

mysql> select *from DemoTable where char_length(FullName) < 12;

This will produce the following output −

+-------------+
| FullName    |
+-------------+
| Chris Brown |
| John Smith  |
+-------------+
2 rows in set (0.00 sec)

Updated on: 17-Dec-2019

103 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements