Learn how to a build a cloud-first strategyRegister Now


'If else' like structure in Db2

Posted on 2007-10-02
Medium Priority
Last Modified: 2012-05-05
hi guys
i have a simple query

from my front end i have a drop -down 'Minutes' which can have 6 values 'o','H','K','L','U'
For each of these values i have a column in the table PLANS.these are the corresponding columns

Now In my  query , i want to check what the user has selected in the drop down and query against the
appripriate column. For example, if user has selected 'o' , then query against 'o_min'

select *
                 if    $min_type$ is 'o'
                     (A.o_min = #passed value#)           //if user has selected 'o' in drop-down.
                else  if    $min_type$ is 'H'
                     (A.H_min = #passed value#)          //if user has selected 'H' in drop-down.                     
            and so on...

* $min_type$  is the value of whatever is selected in the front-end. It can have either of these values
'o','H','K','L','U'. I am passing the value of $min_type$  programatically in the query

any ideas, how i can make this work. Is ther a 'If else' like construct which i can use?

Question by:jaggernat
LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 20000966
Hi jaggernat,

A short time ago, I'd have done this in a very complicated fashion.  DaveSlash showed me the way.  :)

You've got two equally good choices.

1.  You can build the query, and append the correct WHERE clause to it.
2.  You can build the query with all of the relevent filters and select the correct one based on a varaibled

FROM sometable
WHERE (option = 'o' and ....)
   OR (option = 'H' and ...)
   OR (option = 'K' and ...)
   OR (option = 'L' and ...)
   OR (option = 'U' and ...)

Each of the elipses represents the rest of the filter for that type.

If 'option' doesn't exist in the table, write out the full query, and substitute the option string in the query source before you submit it.  It will wind up looking something like this:

FROM sometable
WHERE ('H' = 'o' and ....)
   OR ('H' = 'H' and ...)
   OR ('H' = 'K' and ...)
   OR ('H' = 'L' and ...)
   OR ('H' = 'U' and ...)

This query will return the results for option = 'H'

Good Luck,
LVL 10

Author Comment

ID: 20004025
thanks kdo, you the man :-)

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question