How can I make a MySQL query asking for all the string fields in a column that have a particular length?
Thanks!
LEN() function calculates the number of characters of an input string, excluding the trailing spaces. It is an expression that can be a constant, variable, or column of either character or binary data. Returns : It returns the number of characters of an input string, excluding the trailing spaces.
SUBSTRING() : function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With