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 :)

Link to comment
Share on other sites

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 */

Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.