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
Solved

Query where customer name starts with letter between A - G

Posted on 2009-07-13
6
2,263 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 47
awk and Pythagoras? 5 19
Need help with a Stored Proc on Sql Server 2012 4 28
SQL Syntax Grouping Sum question 7 24
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. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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