×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Where clause with wildcard

Where clause with wildcard

Where clause with wildcard

(OP)
I would like to use a single prepared statement (JDBC).

For example:  SELECT NAME WHERE REGION = ? AND SITE = ?

The issue is that the region and site qualifiers are optional.  Is there a way that I could set the region/site values to a wildcard, so that they don't get used?

RE: Where clause with wildcard

what's JDBC?  is that what handles the question marks?

(because the question marks aren't part of ANSI SQL)

you've also overlooked the FROM clause winky smile


SELECT NAME
  FROM daTable
 WHERE ( REGION = ? OR ? IS NULL )
   AND ( SITE = ? OR ? IS NULL )

r937.com | rudy.ca

RE: Where clause with wildcard

and if ? is null?

this isn't an sql question is it

do we have a JDBC forum?

r937.com | rudy.ca

RE: Where clause with wildcard

and if ? is null?
I don't think a parameter may be null
this isn't an sql question is it
definitively not

RE: Where clause with wildcard

Try this (untested):

SELECT NAME FROM sometable
WHERE COALESCE(REGION = ?,TRUE) AND COALESCE(SITE = ?, TRUE)


(If a parameter is not null, the "REGION/SITE = value" part will be evaluated. If the parameter is null, the COALESCE part will be TRUE for all values of REGION/SITE.)

The ? are used in dynamic SQL, just as specified by the ISO/ANSI SQL standard.
(Look for "<dynamic parameter specification> ::= <question mark>".)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close