Advertisement
Advertisement


MySQL - How to select data by string length


Question

SELECT * FROM table ORDER BY string_length(column);

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

2015/05/29
1
308
5/29/2015 4:06:04 PM

Accepted Answer

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.

2015/09/10
501
9/10/2015 6:04:30 PM

select * from table order by length(column);

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

2009/12/09

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

2013/06/27

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

SELECT * FROM table ORDER BY length(column);
2017/09/01

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.

2019/09/05

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**
2019/11/07

Source: https://stackoverflow.com/questions/1870937
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]