ORACLE IIF Statement

半城伤御伤魂 提交于 2019-11-27 03:02:54

问题


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

Statement:

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

Error: 00907-missing right parantheses

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)
)

Please provide your inputs.


回答1:


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



回答2:


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;
    



回答3:


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.


来源:https://stackoverflow.com/questions/14791684/oracle-iif-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!