How to filter strings based on field character length

I have a table with the column "id" and "description". Need to find those lines where the description contains more than 100 characters?

EDITOR: now got the question above. But is there a way to get the number of characters?

+4
source share
3 answers

Use the string function char_length ().

SELECT * FROM table WHERE char_length (description)> 100

More details here. http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char-length

+8
source

Is this what you are looking for?

select * from table where char_length(description) > 100 

Just a comment about length (). Actually, I am lazy and I will copy it from the official documentation:

Length (str): returns the length of the string str, measured in bytes. A multibyte character counts as several bytes. This means that for a string containing five double-byte characters, LENGTH () returns 10, while CHAR_LENGTH () returns 5.

So length () is not suitable for solving this problem. Instead, user char_length ().

+6
source

Although you really have to create a separate question for editing made after the answer, here is how you get a character count, where the description is longer than 100 characters:

 SELECT char_length(description) FROM table WHERE char_length(description) > 100 
0
source

Source: https://habr.com/ru/post/1395723/


All Articles