Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the plus sign (+) before a string value accepted in Oracle?

Ok, this is perhaps a quirkiness of the Oracle parser.

The following query works. Note the + before 'Y' on the last line.

SELECT *
  FROM (SELECT 'Y' AS field FROM DUAL
        UNION ALL
        SELECT 'X' AS field FROM DUAL) t
 WHERE t.field = +'Y'

Why is the Oracle parser accepting this? For a second I thought it was because of the old outer join syntax but in that syntax the + is surrounded with parentheses.

This works as well:

select +'Y1' from dual;

and this:

select 'A' || + 'Y1' from dual;

This works (oracle converts the string to a number):

select -'1' from DUAL;

but not this ([Error] Execution (223: 9): ORA-01722: invalid number ):

select -'A' from DUAL;

I wonder why the + can be used before a varchar2 value. The Arithmetic Operators section doesn't mention specific rules that would apply to string values.

like image 573
costa Avatar asked Nov 24 '15 23:11

costa


1 Answers

The unary + operator is defined as identity See the Table 4-1 "SQL Operator Precedence" in About SQL Operators.

Also:

select + date '2015-01-01' from dual;

January, 01 2015 00:00:00


Edited to add.

"Identity" being to return its argument. For another example from a different language see Clojure's identity function. Wikipedia has a page for "identity function".

like image 90
Shannon Severance Avatar answered Sep 28 '22 08:09

Shannon Severance