santa
santa

Reputation: 12512

Select rows from database by strlen

Is there a way to select database table rows where a given value is a certain length, for example, less than 5 chars long?

In PHP that would be strlen.

Is there anything similar in MySQL?

Upvotes: 37

Views: 41881

Answers (2)

Timo Huovinen
Timo Huovinen

Reputation: 55673

LENGTH("my_string") Return the length of a string in bytes

SELECT * FROM table_name WHERE LENGTH(column_name) < 5

Keep in mind that characters can be made up of multiple bytes like those in UTF-8.

Upvotes: 9

Derek Prior
Derek Prior

Reputation: 3517

SELECT * FROM table_name WHERE CHAR_LENGTH(column_name) < 5

Upvotes: 79

Related Questions