The moose likes JDBC and the fly likes Using wildcards for Select parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JDBC
Reply Bookmark "Using wildcards for Select parameters" Watch "Using wildcards for Select parameters" New topic
Author

Using wildcards for Select parameters

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I'm trying to write an application where if the person puts "ab" as a query string, they will get back a result of all rows containing a Country name beginning with "Ab".
I've tried using HAVING, WHERE, LIKE, etc., but can't seem to figure how you would return this. Using a SELECT * FROM TABLE WHERE COUNTRY = 'Ab*'; doesn't work, nor does 'Ab'*;
Anyone have any ideas what I can use to get this query statement set up? Pointers to the correct information preferable over the exact Select statement I would use.
Thanks!
Jason
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1675
Use
where country LIKE < expression containing wildcard >
Spoiler: SQL wildcards are % and _. They go inside the single ticks.
[ March 24, 2002: Message edited by: Michael Matola ]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
*breaks into a lovely rendition of "Wind Beneath My Wings"*
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1675
But I'm not dead yet!
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
One could only hope... *heavy sigh*
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Jason, I think there is a little more to completely answer your question:
SELECT * FROM TABLE WHERE COUNTRY = 'ab%';
in most databases, the queries are case sensitive. So the above queries will return 'abc', 'abd', but will not return 'Abc', or 'ABc' like you originally stated. To accomlish this, you'll have to use your vendors convert to upper case function on both the column you are searching and on the search parameter itself:
in oracle:
SELECT * FROM TABLE WHERE UPPER( COUNTRY ) = UPPER( 'ab%' );
This will return any work that begins with 'ab..', 'Ab..', 'aB...', or 'AB...'
Jamie
 
 
subject: Using wildcards for Select parameters
 
Threads others viewed
Statement and PreparedStatement
does OracleDriver really precompile a PreparedStatement?
help! perhaps the problems of configuration of JDBC on Weblogic
problem with syntax for SQL statement
how to write this query
IntelliJ Java IDE