MySQL: Order by field size/length

SqlMysqlSql Order-By

Sql Problem Overview


Here is a table structure (e.g. test):

Field Name Data Type
id BIGINT (20)
title varchar(25)
Description Text

A query like:

SELECT * FROM TEST ORDER BY description DESC;

But I would like to order by the field size/length of the field description.

The field type will be TEXT or BLOB.

Sql Solutions


Solution 1 - Sql

SELECT * FROM TEST ORDER BY LENGTH(description) DESC;

The LENGTH function gives the length of string in bytes. If you want to count (multi-byte) characters, use the CHAR_LENGTH function instead:

SELECT * FROM TEST ORDER BY CHAR_LENGTH(description) DESC;

Solution 2 - Sql

For those using MS SQL

SELECT * FROM TEST ORDER BY LEN(field)

Solution 3 - Sql

SELECT * FROM TEST ORDER BY CHAR_LENGTH(description);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSadiView Question on Stackoverflow
Solution 1 - SqlJoão SilvaView Answer on Stackoverflow
Solution 2 - SqlmfrederickView Answer on Stackoverflow
Solution 3 - SqlMike SherovView Answer on Stackoverflow