tags:

views:

39

answers:

3

How would I properly write this sql-english query:

"SELECT zip FROM tblContacts WHERE [the number of characters in zips is less or equal than 4]".

Thanks.

A: 

I'm sure there must be a better way, but...:

SELECT zip FROM tblContacts WHERE zip LIKE '_' OR zip LIKE '__' OR zip LIKE '___' OR zip LIKE '____';
Alix Axel
A: 
SELECT zip FROM tblContacts WHERE CHAR_LENGTH(zip) > 0
Pekka
+2  A: 

Assuming zip is a string:

WHERE CHAR_LENGTH(zip) <= 4

should work.

Edit: originally there was some confusion as to whether the desired comparison was <, <=, or >, but now the Q's been edited to clarify, so I've edited my A accordingly.

Alex Martelli
Yeah. I couldn't find an "whoops -- need to edit the post" button.
D.A.W.
@D.A.@, the `edit` link is below and to the left of your post, just below the tags "buttons" that say `mysql` and `query` -- anyway, now editing my answer since you've now clarified it "less than or equal to four", thanks!
Alex Martelli