[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Query where customer name starts with letter between A - G

Posted on 2009-07-13
6
Medium Priority
?
2,320 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 2000 total points
ID: 24841865
Correction:

WHERE CUSTOMER_NAME >= 'A%' AND CUSTOMER_NAME <= 'G%'
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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