
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL Query to Find Rows with Strings 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)
Advertisements