Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Posts
    220

    Unanswered: Decode with OR conditions

    I am using Oracle 8.0.6.3.0 (I know we need to upgrade) and I need help writing a DECODE statement for an OR condition.

    This is what I need to do if I.P_PMVACODE = '14' OR '17' OR '24' OR '27' OR '87'

    DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DE CODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' '

    ELSE do this: ps.msgtext || ' '

    Any help would be appreciated.

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    Do you realize that for V8, SQL does not contain/support the IF construct?
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Sep 2005
    Posts
    220
    It essencially does if you use the Decode statement.

  4. #4
    Join Date
    Dec 2003
    Posts
    1,074
    I think you're on the right track, you just have too many arguments in the DECODE function

    Code:
    select * from t;
    
    
            F1
    ----------
             1
             2
             3
             4
    
    4 rows selected.
    
    select f1,
      decode(f1, 1, 'one',
       decode(f1, 2, 'two',
        decode(f1, 3, 'three',
    	 'not one, two or three'))) as d_f1
    from t;
    
    
            F1 D_F1                 
    ---------- ---------------------
             1 one                  
             2 two                  
             3 three                
             4 not one, two or three
    
    4 rows selected.
    -cf

  5. #5
    Join Date
    Sep 2005
    Posts
    220
    Thanks for the response Chuck, but I don't follow your thought here since I don't see the check of I.P_PMVACODE anywhere in the decode statement.

  6. #6
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    How about something like this: using your DECODE, split P_PMVACODE condition into two SELECT statements (once using IN, another time NOT IN) and join both results with UNION (or UNION ALL, see for yourself):
    Code:
    SELECT    DECODE (i.p_fodenleyind,
                      1, DECODE (i.p_dafehvind,
                                 0, DECODE (i.p_dafleyind, 0, 'TRP-F ', 'TRP-D '),
                                 'TRP-D '
                                ),
                      'TRP-D '
                     )
           || ps.msgtext
           || ' ' RESULT
      FROM TEST i, some_table ps
     WHERE i.p_pmvacode IN ('14', '17', '24', '27', '87')
       AND i.some_column = ps.some_column
    UNION
    SELECT ps.msgtext || '' RESULT
      FROM TEST i, some_table ps
     WHERE i.p_pmvacode NOT IN ('14', '17', '24', '27', '87')
       AND i.some_column = ps.some_column;

  7. #7
    Join Date
    Nov 2005
    Location
    Atlanta, GA USA
    Posts
    20
    Quote Originally Posted by ssmith001
    if I.P_PMVACODE = '14' OR '17' OR '24' OR '27' OR '87'

    DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DE CODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' '

    ELSE do this: ps.msgtext || ' '
    You can just repeat the decode statement for each value:

    Code:
    DECODE( I.P_PMVACODE,
        '14', DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DECODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' ',
        '17', DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DECODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' ',
        ...
        ps.msgtext || ' ') // default case

  8. #8
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking


    Or just:
    Code:
    ...
     DECODE(INSTR('14,17,24,27,87',I.P_PMVACODE),0,ps.msgtext||' ',
     DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DECODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' ')
    ...


    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  9. #9
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    Quote Originally Posted by LKBrwn_DBA

    Or just:
    Code:
    ...
     DECODE(INSTR('14,17,24,27,87',I.P_PMVACODE),0,ps.msgtext||' ',
     DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DECODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' ')
    ...


    What a nice use of the instr clause. It's a good day when you learn a new trick.

    Except, I would do it like this

    DECODE(INSTR(',14,17,24,27,87,',','||I.P_PMVACODE| |','),0,ps.msgtext||' ',
    DECODE(I.P_FODENLEYIND,1,DECODE(I.P_DAFEHVIND,0,DE CODE(I.P_DAFLEYIND, 0,'TRP-F ','TRP-D '),'TRP-D '),'TRP-D ') || ps.msgtext || ' ')

    otherwise a code of 4 (for example) would return true.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  10. #10
    Join Date
    Sep 2005
    Posts
    220
    Many thanks to all. The solution worked great.

Posting Permissions

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