Joseph Lam
Joseph Lam

Reputation: 6109

Sorting unicode character in MySQL from least # of characters to most # of characters?

I have a MySQL database with thousands of rows in it. Let's assume each row only has one column and each cell contains a Unicode word (i.e. Chinese word). How do I sort the records so the result set comes back with the word that has the least # of characters first?

For example: If I have AA, BB, CCC, D, EEEE, then I want the result set to be sorted as such: D, AA, BB, CCC, EEEE

Thank you.

Upvotes: 1

Views: 97

Answers (2)

Gurwinder Singh
Gurwinder Singh

Reputation: 39487

You can use char_length(your_column) as unicode character will take more than one byte.

select * from table 
order by char_length(col), col;

Upvotes: 2

sagi
sagi

Reputation: 40481

First order by the length of the column, and then by the column it self :

SELECT *
FROM YourTable
ORDER BY length(YourCol),
         YourCol

Upvotes: 0

Related Questions