49

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;

Table:

CREATE TABLE SCOTT.EMPLOYEE
(
   EMP_ID       INTEGER                          NOT NULL,
   EMP_FNAME    VARCHAR2(30 BYTE)                NOT NULL,
   EMP_LNAME    VARCHAR2(30 BYTE)                NOT NULL,
   EMP_ADDRESS  VARCHAR2(50 BYTE)                NOT NULL,
   EMP_PHONE    CHAR(10 BYTE)                    NOT NULL,
   EMP_GENDER   CHAR(1 BYTE)
)

Error:

00907-missing right parantheses

Please provide your inputs.

MotKohn
  • 3,485
  • 1
  • 24
  • 41
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 3
    AFAIK, Oracle does not have an IIF function. You can use CASE WHEN instead: `select CASE WHEN emp_id = 1 THEN 'True' ELSE 'False' END from employee` – romar Feb 09 '13 at 21:21

3 Answers3

80

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
mtpultz
  • 17,267
  • 22
  • 122
  • 201
Mateus Schneiders
  • 4,853
  • 3
  • 20
  • 40
23

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;
    
Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Rob van Wijk
  • 17,555
  • 5
  • 39
  • 55
0

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.
Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45