ana
ana

Reputation: 1595

Query in MySQL for string fields with a specific length

How can I make a MySQL query asking for all the string fields in a column that have a particular length?

Thanks!

Upvotes: 37

Views: 56479

Answers (2)

John K.
John K.

Reputation: 5474

select * 
from myTable 
where length(myColumn) = 5

Upvotes: 6

BoltClock
BoltClock

Reputation: 723388

Use LENGTH() for checking length in bytes:

SELECT str FROM sometable WHERE LENGTH(str) = 5;

Or CHAR_LENGTH() for checking length in number of characters (useful for multi-byte strings):

SELECT str FROM sometable WHERE CHAR_LENGTH(str) = 5;

Upvotes: 66

Related Questions