Sort Query By Length of String

We’ll be using the MySQL built-in function char_length to demonstrate how to filter and sort based on a size of a columns width in characters.

Sort by length of column

Select title, char_length(title) as title_length
From tblData
Order by char_length(title) desc

Find long title by filtering on the length of a column

Select title, char_length(title) as title_length
From tblData
Having char_length(title) > 20
Order by char_length(title) desc

Leave a Reply

Your email address will not be published. Required fields are marked *