Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORACLE IIF Statement

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.

like image 381
user1050619 Avatar asked Feb 09 '13 21:02

user1050619


People also ask

What is IIf statement?

You use IIf to determine if another expression is true or false. If the expression is true, IIf returns one value; if it is false, IIf returns another. You specify the values IIf returns. See some examples. Syntax.

What is difference between IF and IIf?

The critical difference between IIF (available from VS 2002 forward) and IF (available in VS 2005 forward) is that IIF is a function and evaluates all of its arguments prior to returning a value, while IF is an operator that executes like a short-circuiting conditional, only evaluating the true or false argument ...

What is IIf in SQL query?

IIF is a shorthand way for writing a CASE expression. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation.

What language is IIf?

In computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter.


2 Answers

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 
like image 197
Mateus Schneiders Avatar answered Sep 29 '22 03:09

Mateus Schneiders


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; 
like image 26
Rob van Wijk Avatar answered Sep 29 '22 03:09

Rob van Wijk