SQL Query: order by length of characters?

PhpSqlMysql

Php Problem Overview


Is it possible to order sql data rows by the total number of characters?

e.g. SELECT * FROM database ORDER BY data.length()

Php Solutions


Solution 1 - Php

I think you want to use this: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length

SELECT * FROM table ORDER BY CHAR_LENGTH(field)

You can use just simply LENGTH(), but beware, because it counts the byte number (which won't give you the expected result with multibyte strings).

Solution 2 - Php

SELECT * FROM database ORDER BY Len(data)

Solution 3 - Php

SELECT * FROM table ORDER BY length(data) desc

Where data is varchar field

Solution 4 - Php

For anyone doing with Sqlite

SELECT * FROM table ORDER BY LENGTH(field) DESC

Solution 5 - Php

SELECT * FROM YourTable ORDER BY LENGTH(Column_Name) DESC

e.g;

SELECT * FROM Customer ORDER BY LENGTH(CustomerName) DESC

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
QuestionTorbenLView Question on Stackoverflow
Solution 1 - PhptamasdView Answer on Stackoverflow
Solution 2 - PhpAlex ReitbortView Answer on Stackoverflow
Solution 3 - PhpMichael PakhantsovView Answer on Stackoverflow
Solution 4 - PhpzeeawanView Answer on Stackoverflow
Solution 5 - PhpKashifView Answer on Stackoverflow