ORACLE IIF Statement

后端 未结 3 1155
失恋的感觉
失恋的感觉 2020-12-01 13:48

I get an error while writing the IIF statement, table and the statement given below.

Statement:

SELECT IIF(EMP_ID=1,\'True\',\'False\') from Employee;
         


        
相关标签:
3条回答
  • 2020-12-01 14:14

    In PL/SQL, there is a trick to use the undocumented OWA_UTIL.ITE function.

    SET SERVEROUTPUT ON
    
    DECLARE
        x   VARCHAR2(10);
    BEGIN
        x := owa_util.ite('a' = 'b','T','F');
        dbms_output.put_line(x);
    END;
    /
    
    F
    
    PL/SQL procedure successfully completed.
    
    0 讨论(0)
  • 2020-12-01 14:25

    Two other alternatives:

    1. a combination of NULLIF and NVL2. You can only use this if emp_id is NOT NULL, which it is in your case:

      select nvl2(nullif(emp_id,1),'False','True') from employee;
      
    2. simple CASE expression (Mt. Schneiders used a so-called searched CASE expression)

      select case emp_id when 1 then 'True' else 'False' end from employee;
      
    0 讨论(0)
  • 2020-12-01 14:27

    Oracle doesn't provide such IIF Function. Instead, try using one of the following alternatives:

    DECODE Function:

    SELECT DECODE(EMP_ID, 1, 'True', 'False') from Employee
    

    CASE Function:

    SELECT CASE WHEN EMP_ID = 1 THEN 'True' ELSE 'False' END from Employee
    
    0 讨论(0)
提交回复
热议问题