If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > "Variable" where clause?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-09, 10:12
NattyLight NattyLight is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
"Variable" where clause?

Hello

Not sure if using case will work here. We have an input file with, say 11 fields, 3 of which are the key, 8 optional. We want to right one single statement to query the DB, however, any of the 8 optional could be blank on the input. Say on one record, ssn is blank, on another its populated. Is it possible to code a select statement that would have the where ssn = if its populated, but not read if ssn is blank?

Don't really want a huge if statement with as many selects written

thanks
Reply With Quote
  #2 (permalink)  
Old 06-12-09, 11:20
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
NattyLight, there are a few ways this might be done.

One is to build the query string with if statements used to add the other Where clauses as needed. Then run it dynamically.

Another is to do something like this:
Code:
SELECT columns
FROM table-name
WHERE Key_Col1 = variable
  AND Key_Col2 = variable
  AND Key_Col3 = variable
  AND Opt_Char_Col1 LIKE variableC || '%'
  AND Opt_Numeric_Col1 BETWEEN COALESCE(variableN,0) AND COALESCE(VariableN,max-value)
This assumes the Character variableC is an empty string ( '' ) is you don't want to filter on it or a value if you do.
Also, the Numeric VariableN is a NULL if you don't want to filter on it or an actual value if you do.

I might come up with a third way if neither of these work.
Reply With Quote
  #3 (permalink)  
Old 06-12-09, 11:35
NattyLight NattyLight is offline
Registered User
 
Join Date: Jun 2009
Posts: 2
Is there a way to string the select statement together without building a cursor?

Last edited by NattyLight; 06-12-09 at 13:36.
Reply With Quote
  #4 (permalink)  
Old 06-15-09, 02:04
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
a constuct like this might be possible:

WHERE
...
AND ( optional_column_1 = :read_value1 OR :read_value1 = '' )
AND ( optional_column_2 = :read_value2 OR :read_value2 = '' )
...

or this one:

...
AND :read_value1 IN ( optional_column_1 , '' )
AND :read_value2 IN ( optional_column_2 , '' )
...

Last edited by umayer; 06-15-09 at 02:09.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On