Queeken
Queeken

Reputation:

MySQL: Selecting rows ordered by character count

In MySQL, how can I order my query by character count?

Upvotes: 9

Views: 10987

Answers (1)

Paolo Bergantino
Paolo Bergantino

Reputation: 488434

Try using the LENGTH function:

SELECT * FROM table ORDER BY LENGTH(myField);

Depending on what you're doing, you might want to use CHAR_LENGTH instead:

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.

If you don't know what that means, you probably want LENGTH.

Upvotes: 23

Related Questions