Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query in MySQL for string fields with a specific length

Tags:

string

sql

mysql

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

Thanks!

like image 589
ana Avatar asked Feb 09 '11 02:02

ana


People also ask

How do you define the length of a string in SQL?

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.

How do I select a part of a string in MySQL?

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.


1 Answers

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; 
like image 88
BoltClock Avatar answered Oct 01 '22 10:10

BoltClock