37

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
santa
  • 12,234
  • 49
  • 155
  • 255

2 Answers2

79
SELECT * FROM table_name WHERE CHAR_LENGTH(column_name) < 5
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Derek Prior
  • 3,497
  • 1
  • 25
  • 30
9

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.

Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143