MySQL - How to select data by string length

MysqlSelectString Length

Mysql Problem Overview


SELECT * FROM table ORDER BY string_length(column);

Is there a MySQL function to do this (of course instead of string_length)?

Mysql Solutions


Solution 1 - Mysql

You are looking for CHAR_LENGTH() to get the number of characters in a string.

For multi-byte charsets LENGTH() will give you the number of bytes the string occupies, while CHAR_LENGTH() will return the number of characters.

Solution 2 - Mysql

SELECT * FROM table 
ORDER BY LENGTH(column);

Documentation on the LENGTH() function, as well as all the other string functions, is available here.

Solution 3 - Mysql

Having a look at MySQL documentation for the string functions, we can also use CHAR_LENGTH() and CHARACTER_LENGTH() as well.

Solution 4 - Mysql

The function that I use to find the length of the string is length, used as follows:

SELECT * FROM table ORDER BY length(column);

Solution 5 - Mysql

> In my case I get data using mobile number length greater than 10 digits using the below query

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

Solution 6 - Mysql

I used this sentences to filter

SELECT table.field1, table.field2 FROM table WHERE length(field) > 10;

you can change 10 for other number that you want to filter.

Solution 7 - Mysql

select * from *tablename* where 1 having length(*fieldname*)=*fieldlength*

Example if you want to select from customer the entry's with a name shorter then 2 chars.

select * from customer where 1 **having length(name)<2**

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGalView Question on Stackoverflow
Solution 1 - MysqlhszView Answer on Stackoverflow
Solution 2 - MysqlKaleb BraseeView Answer on Stackoverflow
Solution 3 - MysqlNeverHopelessView Answer on Stackoverflow
Solution 4 - MysqlRitoView Answer on Stackoverflow
Solution 5 - MysqlGanesan JView Answer on Stackoverflow
Solution 6 - MysqlJesús SánchezView Answer on Stackoverflow
Solution 7 - MysqlLevi MaierView Answer on Stackoverflow