DECODE function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessy
    New Member
    • Oct 2006
    • 106

    DECODE function

    i was asked in an interview to use the decode function to print High if salary was greater than 2500 and low if it was less than 2000... but i dont know how to use relational operators in decode ??!! is that possible ?? help plz
  • Saii
    Recognized Expert New Member
    • Apr 2007
    • 145

    #2
    You can do it using CASE as it is an alterative for implemeting if-then-else logic in SQL statements.

    Comment

    • SQLSilverSurfer
      New Member
      • Apr 2007
      • 5

      #3
      DECODE is a power function, and that was a trick question.

      Here are some details on DECODE on this link.
      http://www.psoug.org/reference/decode_case.htm l

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Use case within Decode

        Since Expressions can't be used with decode

        The output of case should be input for decode.

        U can also do it using only CASE, no need of decode at all.

        Code:
        SELECT decode(
        CASE 
        WHEN sal>2500 THEN '1'
        WHEN sal<2000 THEN '2'
        END  ,'1','High','2','Lo') FROM EMP
        try on SCOTT schema EMP table

        Comment

        • bonzi1405
          New Member
          • May 2007
          • 5

          #5
          Originally posted by jessy
          i was asked in an interview to use the decode function to print High if salary was greater than 2500 and low if it was less than 2000... but i dont know how to use relational operators in decode ??!! is that possible ?? help plz

          As per the requirement, below script will print
          Low for <2000
          High for>2500
          but null for 2000 to 2500


          select decode(least(&s alary,2000),200 0,decode(greate st(&salary,2500 ),&salary,'High '),'Low') print from dual

          Comment

          Working...