Solved

Query where customer name starts with letter between A - G

Posted on 2009-07-13
6
2,278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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