Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > selecting records from a range of values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-11-07, 05:03
Panoy Panoy is offline
Registered User
 
Join Date: Mar 2007
Posts: 56
Question selecting records from a range of values

Hi to all,

I wanted to retrieve records that are within the range of 2 letters.

Example:

I want to retrieve the student's surnames that starts with A and ends with C. I tried to run a query that uses the BETWEEN keyword in my WHERE clause, but the results turned out to return only records from A to B only.

The same output goes if you use the field>='A%' and field<='B%' which is the same as using the BETWEEN keyword on your query.

What I did is just use the LEFT function of MySQL and compared it to the start letter and end letter, which gave me the results I wanted.

My question is that if there is another better or efficient way to do this? What if some other RDBMS does not support the LEFT function?

I want to ask also why the BETWEEN keyword does not return records that start with the letter C in my query, but does retrieve records that start with letter A. Which I have specified in my BETWEEN query such as this:

Ex: BETWEEN 'A%' and 'C%'

Thanks and god bless.
Reply With Quote
  #2 (permalink)  
Old 11-11-07, 06:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
it is entirely possible that there might be a database system somewhere that does not have the LEFT function (in that case, look for the SUBSTRING function, which is part of the SQL standard)

here are a bunch of names --

Adams
Applethwaite
Babson
Boweridge
Collins
Coulter

when you say BETWEEN 'A' and 'C', or the equivalent, >= 'A' and <= 'C', Collins and Coulter are not included because both are greater than 'C'

if you want to use BETWEEN for this, then you have two choices --

1. WHERE LEFT(name,1) BETWEEN 'A' AND 'C'

2. WHERE name BETWEEN 'A' AND 'D' (assuming there is no person with the actual one-letter surname 'D')

note also that wildcards will only work with LIKE
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 11-11-07, 19:18
Panoy Panoy is offline
Registered User
 
Join Date: Mar 2007
Posts: 56
I will be trying out solution #1. I think its more optimized than my own query. Thanks again
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On