- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Order strings by length of characters IN mYsql?
You can order by length of characters with the help of CHAR_LENGTH() function from MySQL. The function returns the number of characters i.e. 4 for the following string −
AMIT
To order strings by length of characters, the following is the syntax −
select *from yourTableName order by CHAR_LENGTH(yourColumnName);
To understand the above concept, let us first create a table. The following is the query to create a table −
mysql> create table OrderByCharacterLength −> ( −> BookName varchar(200) −> ); Query OK, 0 rows affected (1.97 sec)
Insert some records in the table with the help of insert command. The query is as follows −
mysql> insert into OrderByCharacterLength values('Let us C'); Query OK, 1 row affected (0.31 sec) mysql> insert into OrderByCharacterLength values('Introduction to C'); Query OK, 1 row affected (0.20 sec) mysql> insert into OrderByCharacterLength values('Data Structure And Algorithm in Java '); Query OK, 1 row affected (0.13 sec) mysql> insert into OrderByCharacterLength values('C in Depth'); Query OK, 1 row affected (0.17 sec) mysql> insert into OrderByCharacterLength values('Java Projects'); Query OK, 1 row affected (0.23 sec)
Let us display all records in a sequence as inserted in the table above. The query is as follows −
mysql> select *from OrderByCharacterLength;
The following is the output −
+---------------------------------------+ | BookName | +---------------------------------------+ | Let us C | | Introduction to C | | Data Structure And Algorithm in Java | | C in Depth | | Java Projects | +---------------------------------------+ 5 rows in set (0.00 sec)
Here is the query that display all records arranged according to the length of characters. If a column value has minimum length then it gets higher priority and will get displayed first.
The query is as follows −
mysql> select *from OrderByCharacterLength order by CHAR_LENGTH(BookName);
The following is the output −
+---------------------------------------+ | BookName | +---------------------------------------+ | Let us C | | C in Depth | | Java Projects | | Introduction to C | | Data Structure And Algorithm in Java | +---------------------------------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL Order By specific strings?
- Order By Length of Column in MySQL
- MySQL ORDER BY strings with underscore?
- Program to equal two strings of same length by swapping characters in Python
- Alphanumeric Order by in MySQL for strings mixed with numbers
- Print common characters of two Strings in alphabetical order in C++
- Python code to print common characters of two Strings in alphabetical order
- Java code to print common characters of two Strings in alphabetical order
- Print all distinct circular strings of length M in lexicographical order in C++
- C++ program to get length of strings, perform concatenation and swap characters
- Order by number of chars in MySQL?
- Convert list of strings and characters to list of characters in Python
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- Advanced sorting in MySQL to display strings beginning with J at the end even after ORDER BY
