I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an in-line IF. For those interested, here’s the code:
create or replace FUNCTION IIF (testExpression NUMBER, trueResult NUMBER, falseResult NUMBER) RETURN NUMBER AS BEGIN /* A simple in-line IF function for use with SQL queries. If the test expression evaluates to any non-zero value, it is considered to be true, and the trueResult is returned. Otherwise, falseResult is returned. */ IF (testExpression is null) or (testExpression = 0) THEN return falseResult; ELSE return trueResult; END IF; END IIF;
This isn’t rocket science. Now, here’s the big mystery: if I execute the following SQL statements, everything is just fine, and works exactly as I expect:
SELECT IIF(1, 'true', 'false') FROM DUAL; SELECT IIF(0, 'false', 'true') FROM DUAL;
However, the following generates a really bizarre error from Oracle:
SELECT IIF((0 = 1), 'false', 'true') FROM DUAL;
That error is as follows:
ORA-00907: missing right parenthesis.
Clearly, that isn’t the case. Would anyone happen to have an explanation for this little bit of bizarreness?
It’s taking a whole lot of self-control at the moment to restrain myself from hurling the Oracle server out the window. Oracle seems rife with these kinds of inanities.
EDIT: Is there some kind of magic syntax I have to use to use an equality operator in a select statement?
Maybe you can explain what you are trying to do. I think you are looking for the
CASEorDECODEfunctions, but I can’t be sure. It seems like you are working against the grain of the SQL language for some reason.The error occurs because Oracle doesn’t expect a relational operator in the
selectclause of the query:See this Ask Tom article.