Solved

Query where customer name starts with letter between A - G

Posted on 2009-07-13
6
2,245 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
Comment Utility
WHERE CUSTOMER_NAME LIKE '[A-G]%';
0
 
LVL 40

Expert Comment

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

Accepted Solution

by:
mrjoltcola earned 500 total points
Comment Utility
Correction:

WHERE CUSTOMER_NAME >= 'A%' AND CUSTOMER_NAME <= 'G%'
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now