title: Type mismatch error in Oracle SQL correlated subquery when comparing dates
body: <p>I'm executing a big Oracle SQL select statement, with multiple (about 25) correlated subqueries in the select part of the outer query. The basic idea is:</p>
  
  <pre><code>SELECT DISTINCT column1, column2, ..., (select from ... where ...), ...
  FROM table1, table2, table3 ...
  WHERE ...
  AND ...
  ORDER BY column1
  </code></pre>
  
  <p>I'm getting stuck on the following piece of code which is present in every correlated subquery:</p>
  
  <pre><code>AND to_date(foin.BEGINDATUM, 'dd/mm/yyyy') 
  BETWEEN to_date(beha.STARTDATUM, 'dd/mm/yyyy') 
  AND to_date(beha.EINDDATUM, 'dd/mm/yyyy')
  </code></pre>
  
  <p>I am getting the error 'type mismatch', in some cases.
  This piece of code makes sure some data from one table with the alias 'foin' has a starting date between two dates from another table aliased as 'beha'. All columns have the DATE data type, it might be possible they have NULL values, but even when adding the NVL function in my piece of code it still gives me the type mismatch error.</p>
  
  <p>The following solution was a good idea but didn't solve the error message:</p>
  
  <pre><code>AND trunc(foin.BEGINDATUM) BETWEEN trunc(beha.STARTDATUM) 
  AND trunc(NVL(beha.EINDDATUM, '01/01/2100'))
  </code></pre>
  
  <p>Anyone has an idea?</p>
  
  <p>Edit: related question: is this error only caused by comparing two fields with different datatypes or comparing a date to a NULL value or could there be another explanation?</p>
  
  <p>Edit2: the result of this oracle sql query is put into MS Excel, the connection is made with an ODBC which I don't really know anything about. Could this error be related to MS Excel?</p>