In MySQL, is there a way to order my results by the length (characters) of a column?
For example:
myColumn
________________
lor
lorem
lorem ip
lorem
You can use CHAR_LENGTH() function:
ORDER BY CHAR_LENGTH( column )
Notice that CHAR_LENGTH()
works for Unicode strings as well, where LENGTH()
is OK for Latin but may give unexpected results with Unicode:
CHAR_LENGTH(str)
Returns the length of the string str, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
Have a look at the LENGTH() operator.
ORDER BY LENGTH(myColumn)
SELECT * FROM my_table ORDER BY LENGTH(my_column)