Note that there are some explanatory texts on larger screens.

plurals
  1. POSubtracting Dates in Oracle - Number or Interval Datatype?
    primarykey
    data
    text
    <p>I have a question about some of the internal workings for the Oracle DATE and INTERVAL datatypes. According to the <a href="http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm#g196492" rel="noreferrer" title="Oracle 11.2 SQL Reference">Oracle 11.2 SQL Reference</a>, when you subtract 2 DATE datatypes, the result will be a NUMBER datatype.</p> <p>On cursory testing, this appears to be true:</p> <pre><code>CREATE TABLE test (start_date DATE); INSERT INTO test (start_date) VALUES (date'2004-08-08'); SELECT (SYSDATE - start_date) from test; </code></pre> <p>will return a NUMBER datatype. </p> <p>But now if you do:</p> <pre><code>SELECT (SYSDATE - start_date) DAY(5) TO SECOND from test; </code></pre> <p>you get an INTERVAL datatype. In other words, Oracle can convert the NUMBER from the DATE subtraction into an INTERVAL type.</p> <p>So now I figured I could try putting in a NUMBER datatype directly in the brackets (instead of doing 'SYSDATE - start_date' which results in a NUMBER anyways):</p> <pre><code>SELECT (1242.12423) DAY(5) TO SECOND from test; </code></pre> <p>But this results in the error: </p> <pre><code>ORA-30083: syntax error was found in interval value expression </code></pre> <p>So my question is: what's going on here? It seems like subtracting dates should lead to a NUMBER (as demonstrated in SELECT statement #1), which CANNOT be automatically cast to INTERVAL type (as demonstrated in SELECT statement #3). But Oracle seems to be able to do that somehow if you use the DATE subtraction expression instead of putting in a raw NUMBER (SELECT statement #2).</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload