Jump to content

Selecting Where First Character Is Not A-Z


Mcod

Recommended Posts

Hi there, another short question regarding a query:

 

I want to show some data in categories called A - Z and in another category which contains non A-Z items.

 

For the a - z queries this is easy:

 

$thequery = mysql_query("SELECT * FROM allitems where thename like 'a%' ORDER by thename ASC LIMIT $offset, $limit");

 

However, I would also like to have a category with items that do not start with a - z, for example items like !Great or items that start with a number like 1Great or even things like *Great

 

Sure, I could do it in a dirty way like:

 

where thename NOT like 'a%' AND thename NOT like 'b%' AND thename NOT like 'c%'

 

but I am sure there is a better solution with some REGEX query or something similar which I don't know about.

 

Maybe you have an idea how this would work out best?

 

Thank you :)

I'm quite sure you can go with string comparisons:

WHERE thename < 'A' OR (thename >= '[' AND thename < 'a') OR thename >= '{'

[ and { being the characters immediately after Z and z respectively.

 

What would be nice is if you had a column just for the first letter (always in upper- or lowercase), then it'd be really easy:

WHERE thename BETWEEN 'A' AND 'Z' /* letters */
WHERE thename NOT BETWEEN 'A' AND 'Z' /* not letters */

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.