 |
|
10th July 2008, 12:38
|
#1
|
|
Godlike
Join Date: Jul 2005
Posts: 10,425
|
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.
|
|
|
10th July 2008, 12:40
|
#2
|
|
Contractor Among Contractors
Join Date: Jun 2008
Posts: 1,437
|
Quote:
Originally Posted by DimPrawn
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.
|
|
|
10th July 2008, 12:45
|
#3
|
|
Super poster
Join Date: Mar 2007
Location: London
Posts: 4,062
|
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.
|
|
|
10th July 2008, 12:48
|
#4
|
|
Godlike
Join Date: Jul 2005
Posts: 10,425
|
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.
|
|
|
10th July 2008, 12:55
|
#5
|
|
Super poster
Join Date: Jul 2005
Location: Brutopia
Posts: 4,697
|
Quote:
Originally Posted by DimPrawn
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.
|
|
|
10th July 2008, 13:15
|
#6
|
|
Godlike
Join Date: Jul 2005
Posts: 10,425
|
Quote:
Originally Posted by expat
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.
|
|
|
10th July 2008, 14:08
|
#7
|
|
Not worth listening to
Join Date: Sep 2007
Posts: 20
|
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)

|
|
|
10th July 2008, 14:35
|
#8
|
|
Godlike
Join Date: Jul 2005
Posts: 10,425
|
Quote:
Originally Posted by rsingh
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.
|
|
|
10th July 2008, 14:47
|
#9
|
|
Super poster
Join Date: Mar 2007
Location: London
Posts: 4,062
|
Quote:
Originally Posted by DimPrawn
I know SQL very well

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

|
But you can't do an OR?
__________________
Level 20 Xeno Geek.
|
|
|
10th July 2008, 14:50
|
#10
|
|
Godlike
Join Date: Jul 2005
Posts: 10,425
|
Quote:
Originally Posted by Moscow Mule
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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 06:22.
|  |
| Advertisers |
|
| 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
|
|