mysql select by string length

How to Select By String Length in MySQL

Typically, web developers and database administrators select database rows based on value of one or more columns. But sometimes you may need to select by string length in MySQL. In this article, we will learn how to select by string length in MySQL.


How to Select By String Length in MySQL

You can use char_length() or length() function to determine string length in MySQL. length() function returns number of bytes occupied by string in multi-byte charsets while char_length() returns number of characters.

Here is the syntax to order rows based on length() function.

SELECT * FROM table 
ORDER BY LENGTH(column);

OR

SELECT * FROM table 
ORDER BY CHAR_LENGTH(column);

You can also use length() or char_length() function to filter rows based on string length. Here is an example to select rows from a table where column length > 10.

SELECT * FROM table_name WHERE LENGTH(column) > 10;

OR

SELECT * FROM table_name WHERE CHAR_LENGTH(column) > 10;

In this article, we have learnt how to select by string length. You can customize it as per your requirement.

Also read:

How to Remove All Tables in MySQL
How to Skip Tables in MySQLDump
How to Reset MySQL Query Cache
How to Delete All Rows in MySQL Table
How to Fix Unknown Column in Field List

Leave a Reply

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