Contractor UK Bulletin Board  PayStream

Go Back   Contractor UK Bulletin Board > Contractor UK Forums > Technical
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 10th July 2008, 12:38   #1
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,425
Default SQL to restrict rows by first letter / number

Thicky question.

Say you have names for bands and you want to display all that begin with a specific letter.

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Easy, you can:

SELECT band_name
FROM bands
WHERE band_name LIKE 'A%'

But what if you want to have # select all bands that don't begin with A - Z ( e.g. they begin with a number).

such as 22 Jacks, or 1080?

Using one piece of SQL to handle the trivial A-Z and the # option without going mental silly?
__________________
Last week I cashed a cheque and the bank bounced.
DimPrawn is offline   Reply With Quote
Old 10th July 2008, 12:40   #2
TheBigYinJames
Contractor Among Contractors
 
TheBigYinJames's Avatar
 
Join Date: Jun 2008
Posts: 1,437
Default

Quote:
Originally Posted by DimPrawn View Post
Thicky question.
Using one piece of SQL to handle the trivial A-Z and the # option without going mental silly?
Can't see how you can do it without a big load of ORs or a temp table.
TheBigYinJames is offline   Reply With Quote
Old 10th July 2008, 12:45   #3
Moscow Mule
Super poster
 
Join Date: Mar 2007
Location: London
Posts: 4,062
Default

Can you do something like

Select * from bands where subst(band_name:1:1) not exist('ABCDECFGHIJKLMNOPQRSTUVWXYZ')

Syntax is wrong, but you get the idea...
__________________
Level 20 Xeno Geek.
Moscow Mule is offline   Reply With Quote
Old 10th July 2008, 12:48   #4
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,425
Default

The SQL will have one paramater, the first letter (A-Z) or # passed in, where hash means 'not A-Z'.

Don't want to make something eloborate and some smarty pants laughs at me.
__________________
Last week I cashed a cheque and the bank bounced.
DimPrawn is offline   Reply With Quote
Old 10th July 2008, 12:55   #5
expat
Super poster
 
Join Date: Jul 2005
Location: Brutopia
Posts: 4,697
Default

Quote:
Originally Posted by DimPrawn View Post
Thicky question.

Say you have names for bands and you want to display all that begin with a specific letter.

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Easy, you can:

SELECT band_name
FROM bands
WHERE band_name LIKE 'A%'

But what if you want to have # select all bands that don't begin with A - Z ( e.g. they begin with a number).

such as 22 Jacks, or 1080?

Using one piece of SQL to handle the trivial A-Z and the # option without going mental silly?
SELECT band_name
FROM bands
WHERE UPPER(substr(band_name,1,1)) < 'A'
OR UPPER(substr(band_name,1,1)) > 'Z'

This uses Oracle's UPPER(string) to convert to upper case, your flavour of SQL will doubtless have something like it (I presume that you want a case-independent test).
__________________
Support our troops.
Bring them home.
expat is offline   Reply With Quote
Old 10th July 2008, 13:15   #6
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,425
Default

Quote:
Originally Posted by expat View Post
SELECT band_name
FROM bands
WHERE UPPER(substr(band_name,1,1)) < 'A'
OR UPPER(substr(band_name,1,1)) > 'Z'

This uses Oracle's UPPER(string) to convert to upper case, your flavour of SQL will doubtless have something like it (I presume that you want a case-independent test).
Great but you haven't read the requirement have you?

There is one parameter, either the letter I want or everything that does not start with a letter (# passed in) in one simple, efficient, fast SQL statement.

I know SQL very well and it's pretty obvious how to select everything that's not A to Z.

__________________
Last week I cashed a cheque and the bank bounced.
DimPrawn is offline   Reply With Quote
Old 10th July 2008, 14:08   #7
rsingh
Not worth listening to
 
Join Date: Sep 2007
Posts: 20
Default

Assuming that you pass in @P and have validated it, then something like the following should work.

Code:
SELECT band_name
FROM bands
WHERE 
(@P = '#' AND LEFT(band_name,1) NOT LIKE [a-z])
OR 
(@P != '#'  AND LEFT(band_name,1) = @P)
rsingh is offline   Reply With Quote
Old 10th July 2008, 14:35   #8
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,425
Default

Quote:
Originally Posted by rsingh View Post
Assuming that you pass in @P and have validated it, then something like the following should work.

Code:
SELECT band_name
FROM bands
WHERE 
(@P = '#' AND LEFT(band_name,1) NOT LIKE [a-z])
OR 
(@P != '#'  AND LEFT(band_name,1) = @P)
Now that's what I'm talking about!

__________________
Last week I cashed a cheque and the bank bounced.
DimPrawn is offline   Reply With Quote
Old 10th July 2008, 14:47   #9
Moscow Mule
Super poster
 
Join Date: Mar 2007
Location: London
Posts: 4,062
Default

Quote:
Originally Posted by DimPrawn View Post
I know SQL very well

Quote:
Originally Posted by DimPrawn View Post
Now that's what I'm talking about!

But you can't do an OR?
__________________
Level 20 Xeno Geek.
Moscow Mule is offline   Reply With Quote
Old 10th July 2008, 14:50   #10
DimPrawn
Godlike
 
DimPrawn's Avatar
 
Join Date: Jul 2005
Posts: 10,425
Default

Quote:
Originally Posted by Moscow Mule View Post
But you can't do an OR?
That's in module 2, to be learned next week.
__________________
Last week I cashed a cheque and the bank bounced.
DimPrawn is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT. The time now is 06:55.


Advertisers
PayStream

CUK Navigation

Contractor Alliance
Formed a new Ltd Co?

20% off business insurance
£10 off Bauer & Cottrell contract reviews
Find co-workers & client introductions

Increase your value to clients here

Fast Company Formation
Same day online company formation £75 + VAT

Form your Ltd Co Here

Contractor Services


 
Content Relevant URLs by vBSEO 2.4.0 © 2005, Crawlability, Inc.