Solved

Query where customer name starts with letter between A - G

Posted on 2009-07-13
6
2,256 Views
Last Modified: 2012-05-07
I want modify the code below to find customer names that begin with A - G.
Do I need to write AND statement for each letter?


Thanks!
Beth
SELECT CUSTOMER_NO ,
CUSTOMER_NAME,
ADDRESS_1_BILL ,
ADDRESS_2_BILL ,
CITY_BILL ,
STATE_BILL ,
ZIPCODE_BILL
 
FROM CUSTOMER
 
WHERE CUSTOMER_NAME LIKE '%H%';

Open in new window

0
Comment
Question by:ba_trainer
  • 5
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24841826
WHERE CUSTOMER_NAME LIKE '[A-G]%';
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24841848
WHERE CUSTOMER_NAME >= 'A%' AND CUSTOMER_NAME <= 'Z%'
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24841865
Correction:

WHERE CUSTOMER_NAME >= 'A%' AND CUSTOMER_NAME <= 'G%'
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24841882
Oops disregard that, it will ignore 'A' and 'G' single character names. I'll get it right shortly. :)

WHERE CUSTOMER_NAME >= 'A' AND CUSTOMER_NAME <= 'G'
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24842003
Between misreading and typos and wrong answers I apologize for spamming this question. The 1st was a misread, the 2nd was plain wrong. The 3rd will leave out any names starting with G that are longer than 1 character.

Since I assume  you want Oracle, you may try the regex route but this may not perform well, it may force a full scan.

-- Regex not good for performence, will full table scan
SQL> select * from customer where regexp_like(customer_name, '^[A-Z]');


-- This will work efficiently, but is not elegant
SQL> select * from names where name between 'A' and 'G' or name like 'G';


-- An alternative is this: create a function based index on the first character substring

SQL> create index ix_names on names(substr(name,1,1));

-- Then search like this
SQL> select * from names where substr(name,1,1) between 'A' and 'G';
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24842589
Hi Beth, due to my own error, the solution you accepted is incorrect, since I used % without the LIKE operator. That is why I posted a followup correction. The final solution http:#24842003 really corrects and covers it all, in my opinion.

Do you mind if I re-open the question and select http:#24842003 so we can correct it for future readers?

Again, it is my fault but I want to make sure it is corrected. My apologies.


0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question