'If else' like structure in Db2

Posted on 2007-10-02
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 45

    Accepted Solution

    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

    SELECT *
    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:

    SELECT *
    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

    thanks kdo, you the man :-)

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now