Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2005

Tip: Looking for answers? Try searching our database.

MySQL Select help needed

Thread view: 
Dave Brown - 19 Apr 2005 18:47 GMT
Hi All,

Trying to work out the correct sql statement

If I have a table that contains the column partpostcode and has these
entries,
PE10
PE15
PE1

What I want to do is select those rows where the partpostcode exists in
a given parameter,...

i.e something liek.

Select * where partpostcode IS CONTAINED IN "PE10 1AF"

sadly there are no IS CONTAINED IN keywords, but i'm not sure how to do
it using LIKE or IN...

Any comments ?

Thanks,
David Harper - 19 Apr 2005 21:47 GMT
> If I have a table that contains the column partpostcode and has these entries,
> PE10
[quoted text clipped - 6 lines]
>
> Select * where partpostcode IS CONTAINED IN "PE10 1AF"

Why not have your Java client code split the postcode e.g. "PE10 1AF"
into two parts, "PE10" and "1AF", then look for an exact match to the
first part?

British postcodes have a regular structure, so this will always work,
and in addition, the prefix part "PE10" always refers to a meaningful
geographic region, so it makes sense from this viewpoint too.

David Harper
Cambridge, England
Dave Brown - 19 Apr 2005 22:52 GMT
Hi David,

My concern there is when I get a Postcode that may not have a space
delimiter.

Dave.

>> If I have a table that contains the column partpostcode and has these
>> entries,
[quoted text clipped - 19 lines]
> David Harper
> Cambridge, England
David Harper - 20 Apr 2005 06:57 GMT
> My concern there is when I get a Postcode that may not have a space
> delimiter.

That's fair enough, but if someone types "PE10 1AF" without space, as
"PE101AF", you can still separate the two parts, because the second part
is *always* a single digit followed by two letters.

Therefore, if there's no space, the algorithm is as follows ...

  String postcode = "PE101AF";

  int pclen = postcode.length();

  String prefix = postcode.substring(0, pclen - 3);

  String suffix = postcode.substring(pclen - 3);

Of course, if the user enters a partial postcode such as PE10, this is
ambiguous: is is "PE10" (Peterborough district 10), or "PE1 0"
(Peterborough district 1, sub-district 0)?

In these cases, you can probably safely assume that the user is giving
the prefix part of their postcode i.e. the user meant "PE10", and not
"PE1 0".

Or you could pop up an error message and insist on a full postcode :-)

David Harper
Cambridge
Dave Brown - 20 Apr 2005 08:49 GMT
Thanks David.

BTW,  I just noticed you are from Cambridge, Have you any interest in
the East Anglian Java User Group ?   I contacted them last week to see
what kind of interest there was, currently theres only a handfull of
people on the list of interested parties. Theres a link to them from
SUN's JUG pages if you are interested.

Rgds,

Dave

>> My concern there is when I get a Postcode that may not have a space
>> delimiter.
[quoted text clipped - 25 lines]
> David Harper
> Cambridge
Rhino - 21 Apr 2005 18:30 GMT
> Hi All,
>
[quoted text clipped - 15 lines]
> sadly there are no IS CONTAINED IN keywords, but i'm not sure how to do
> it using LIKE or IN...

You definitely want to use LIKE for this problem. However, since you are
searching on multiple patterns, you'll have to OR a few LIKEs together.

LIKE uses two wild card characters, % and _. % represents any number of
unknown characters (including 0), and _ represents exactly one unknown
character. Therefore,
WHERE POSTALCODE LIKE 'P%'
says that you want any row where the postal code starts with a P and you
don't care what comes after it. Even a postal code that was *just* P with
nothing after it would satisfy this search.

WHERE POSTALCODE LIKE 'ABCDE_G' would find rows that had ABCDE in the first
five characters of the postal code, G in the seventh position, and any
character at all in the 6th position.

So, if you combine these wild card characters, you should be able to write
LIKE searches that do what you want.

You'll probably want something like:

select * from mytable
where postalcode like '%PE10%'
or postalcode like '%PE15%'

You can improve on that if you know exactly where the partial postal code
is. For example if PE10, PE15 and PE1 always start at the fourth position,
you could do this:

select * from mytable
where postalcode like '___PE10%'
or postalcode like '___PE15%'

(The example has 3 underscores at the start of each like search.)

The only problem you'll have is with the PE1 codes. Since PE1 is a subset of
PE10 and PE15, PE12 will match with PE1% but won't be a row that you want to
see.

Rhino

Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.