

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Sort by character length in MySQL
To sort by character length in MySQL use the ORDER BY LENGTH(). Let us first create a table:
mysql> create table orderingAADemo -> ( -> Value varchar(100) -> ); Query OK, 0 rows affected (1.30 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into orderingAADemo values('A'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('B'); Query OK, 1 row affected (0.13 sec) mysql> insert into orderingAADemo values('AA'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderingAADemo values('C'); Query OK, 1 row affected (0.12 sec) mysql> insert into orderingAADemo values('CCC'); Query OK, 1 row affected (0.22 sec)
Following is the query to display records from the table using select command:
mysql> select *from orderingAADemo;
This will produce the following output
+-------+ | Value | +-------+ | A | | B | | AA | | C | | CCC | +-------+ 5 rows in set (0.00 sec)
Following is the query to sort by character length in descending order:
mysql> select *from orderingAADemo -> order by length(Value) DESC, Value;
This will produce the following output
+-------+ | Value | +-------+ | CCC | | AA | | A | | B | | C | +-------+ 5 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to sort by certain last string character?
- Python – Sort Matrix by Maximum String Length
- Python – Sort by Rear Character in Strings List
- MongoDB aggregation framework to sort by length of array?
- Python – Sort String list by K character frequency
- Order By Length of Column in MySQL
- Sort character values from a column mixed with character and numbers in MySQL?
- Sort String Array alphabetically by the initial character only in Java
- Order strings by length of characters IN mYsql?
- How to sort by value with MySQL ORDER BY?
- MySQL show tables sort by table name?
- How to sort by arbitrary keywords in MySQL?
- Filter column value by the first character in MySQL
- Sort by date & time in descending order in MySQL?
- How to ORDER BY last 2 character string in MySQL?
Advertisements