Domanda

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:

SELECT DISTINCT column1, column2, ..., (select from ... where ...), ...
FROM table1, table2, table3 ...
WHERE ...
AND ...
ORDER BY column1

I'm getting stuck on the following piece of code which is present in every correlated subquery:

AND to_date(foin.BEGINDATUM, 'dd/mm/yyyy') 
BETWEEN to_date(beha.STARTDATUM, 'dd/mm/yyyy') 
AND to_date(beha.EINDDATUM, 'dd/mm/yyyy')

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.

The following solution was a good idea but didn't solve the error message:

AND trunc(foin.BEGINDATUM) BETWEEN trunc(beha.STARTDATUM) 
AND trunc(NVL(beha.EINDDATUM, '01/01/2100'))

Anyone has an idea?

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?

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?

È stato utile?

Soluzione

Since all three columns are already of data type date, there is no need to call to_date. That would force the dates to be implicitly cast to a varchar2, using the session's current NLS_DATE_FORMAT before being explicitly converted back to a date using the specified format mask. If the two masks are different, you'll get an error.

Assuming that you want to ignore the time component of all three columns

AND trunc( foin.begindatum ) BETWEEN trunc( beha.startdatum ) 
                                 AND trunc( beha.einddatum )
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top