Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012
    Location
    Arizona
    Posts
    9
    Provided Answers: 1

    Answered: Nested IF AND Statement in Results

    Please help I'm trying to write a nested 'IF' 'AND' statement in the results set of a DB2 query. In essence, this is what I'm trying to accomplish: IF(Interest_Rate BETWEEN .008 AND .016) AND (Open_Date NOT BETWEEN 10/24/2016 AND 01/01/2017) AND (Product_Name == 'Loans') {'YES'} else{'NO'}

    I've never written a nested IF/AND statement, and so I'm not sure which part I'm not getting right.

  2. Best Answer
    Posted by mark.bb

    "Try this:
    Code:
    select 
      t.*
    , case when Interest_Rate BETWEEN .008 AND .016 AND Open_Date NOT BETWEEN date('2016-10-24') AND date('2017-01-01') AND Product_Name = 'Loans' then 'YES' else 'NO' end
    from table(values
      (.010, date('2016-10-24'), 'Loans')
    , (.010, date('2016-10-23'), 'Loans')
    ) t (Interest_Rate, Open_Date, Product_Name);
    "


  3. #2
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    579
    Provided Answers: 3
    SELECT
    CASE
    WHEN ((condition) AND (condition) AND (condition)) THEN 'YES'
    ELSE 'NO'
    END
    AS field_name
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  4. #3
    Join Date
    Jul 2012
    Location
    Arizona
    Posts
    9
    Provided Answers: 1
    That's the easy part of the formula, but what I'm trying to figure out is the "BETWEEN" and "NOT BETWEEN" part.

  5. #4
    Join Date
    Jul 2016
    Location
    Moscow
    Posts
    294
    Provided Answers: 45
    Try this:
    Code:
    select 
      t.*
    , case when Interest_Rate BETWEEN .008 AND .016 AND Open_Date NOT BETWEEN date('2016-10-24') AND date('2017-01-01') AND Product_Name = 'Loans' then 'YES' else 'NO' end
    from table(values
      (.010, date('2016-10-24'), 'Loans')
    , (.010, date('2016-10-23'), 'Loans')
    ) t (Interest_Rate, Open_Date, Product_Name);
    Regards,
    Mark.

  6. #5
    Join Date
    Jul 2012
    Location
    Arizona
    Posts
    9
    Provided Answers: 1
    You hit the mark...Mark Thank you!

Tags for this Thread

Posting Permissions

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