Solved

SQL Select between letters R and Z

Posted on 2014-01-18
5
1,426 Views
Last Modified: 2014-01-19
I'm trying to use

Select title from mylist where title between 'r' and 'z'

It selects all the r's but I dont see any title beginning with 'z'.
How do I make this command inclusive for the last letter as well?
0
Comment
Question by:arthurh88
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 39791635
Hi Arthur,

"between" is inclusive of the two delimiters.  In your example, all of the letters between 'r' and 'z', including 'r' and 'z'.

I suspect that any title that starts with 'z' is more than once character in length.  In that case, the name would not be between 'r' and 'z' as the sort order would place it after 'z'.

Try this:

  where substring (title, 1, 1) between 'a' and 'z'.


You might next stumble onto a case issue.  If so, make sure that you call the lower() function on the first character of the title.

  where lower (substring (title, 1, 1)) between 'a' and 'z'.


Good Luck,
Kent
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 500 total points
ID: 39791643
You can also do something like this:
between 'r' and 'zzzzz'

Ron
0
 

Author Closing Comment

by:arthurh88
ID: 39791711
yes that worked awesome.   between 'a' and 'ezz'  thanks!
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39792046
I hate to see people use the wrong solution because it codes easier.

As soon as you have a title that starts with 'ezzanything' that doesn't work.  Nor does it work if the title contains mixed case letters.  It may work in this one specific application, but it's a poor habit to get into.
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 39792293
@arthurh88
You should have used a few more z's just in case you do have a title that starts with ezz.  In addition, if you're titles are in both Upper and Lower case, you will run into a problem.  You should add the Lower function to your Where condition as Kdo said.

The benefit of using zzzzz's in a range is that it allows you to define your delimiters more specifically such as shown below:

BEG_RANGE      END_RANGE
AAAAAAAAAA      GLIDZZZZZZ
GLIEAAAAAA          POGUZZZZZZ
POGVAAAAAA        ZZZZZZZZZZ
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

708 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

15 Experts available now in Live!

Get 1:1 Help Now