Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Posts
    34

    Unanswered: Using case in where clause

    Hi....my requirement is I've to select data from a table. In the where clause i have to use column a....if the value in the column a is null I got to use column b. Any idea will be helpful. Thanks..

  2. #2
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650

    Re: Using case in where clause

    COALESCE function might help ...

    Please take a few moments to read the thread http://dbforums.com/t854783.html

    Cheers

    Sathyaram

    Originally posted by asram
    Hi....my requirement is I've to select data from a table. In the where clause i have to use column a....if the value in the column a is null I got to use column b. Any idea will be helpful. Thanks..
    Visit the new-look IDUG Website , register to gain access to the excellent content.

  3. #3
    Join Date
    Jul 2003
    Posts
    34

    Re: Using case in where clause

    We are using version DB2 v7.1.0.68.

    I think COALESCE function can be used to check whether a column value is null. But my requirement is if a column value is null I have to use different column in the where clause.

    If column a is null
    SELECT abc from table a WHERE a='123'

    If column a is not null
    SELECT abc from table a WHERE b='123'

    How to put the above two quries as one!?

  4. #4
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5

    Re: Using case in where clause

    Try:
    SELECT abc from table a where coalesce(a,b) = '123'

    coalesce has a variable number of arguments. It will return the
    first non-null argument (left-to-right) unless they all are null, then it returns Null.

    HTH

    Andy

    Originally posted by asram
    We are using version DB2 v7.1.0.68.

    I think COALESCE function can be used to check whether a column value is null. But my requirement is if a column value is null I have to use different column in the where clause.

    If column a is null
    SELECT abc from table a WHERE a='123'

    If column a is not null
    SELECT abc from table a WHERE b='123'

    How to put the above two quries as one!?

  5. #5
    Join Date
    Jul 2003
    Posts
    34

    Re: Using case in where clause

    Originally posted by ARWinner
    Try:
    SELECT abc from table a where coalesce(a,b) = '123'

    coalesce has a variable number of arguments. It will return the
    first non-null argument (left-to-right) unless they all are null, then it returns Null.

    HTH

    Andy
    Thanks......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •