MySQL Order Table By Character Length

24th September 2009 - 1 minute read time

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.

  1. SELECT FIELD, CHARACTER_LENGTH(FIELD) AS fieldCharacterCount
  2. FROM TABLE
  3. ORDER BY fieldCharacterCount DESC

 

Add new comment

The content of this field is kept private and will not be shown publicly.