Solved

SQL Select between letters R and Z

Posted on 2014-01-18
5
1,566 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

777 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