MySQL Order Table By Character Length

As part of debugging a bit of code I needed to know the longest possible field lengths that a record contains. You might need to know this if you are performing a database migration. The following query returns a field, along with the length of the string, and orders the results by the number of characters in that string.

SELECT field, CHARACTER_LENGTH(field) as fieldCharacterCount
FROM table
ORDER BY fieldCharacterCount DESC

 

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
12 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.