Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Fetch rows where a field value is less than 5 chars

Tags:

sql

mysql

I need to fetch all the rows where the 'zip' field is less than 5 characters. How can I achieve this using only SQL? I google'd first, but only found info on CHAR_LENGTH().

ie, psudeo code: SELECT * FROM users WHERE STRLEN(zip_code) < 5

Thanks!

like image 487
John Himmelman Avatar asked Feb 11 '10 18:02

John Himmelman


People also ask

How do I select a row with minimum value in SQL?

To select data where a field has min value, you can use aggregate function min(). The syntax is as follows. SELECT *FROM yourTableName WHERE yourColumnName=(SELECT MIN(yourColumnName) FROM yourTableName);

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

For MySQL you would use the LENGTH() function ie.

select * from users where length(zip_code) < 5

Refer to the docs at http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length for more information.

like image 173
wimvds Avatar answered Oct 17 '22 16:10

wimvds