问题
I need to compare a date and a timestamp in a SELECT query. Normally, this is supported by most other DB platforms(MySQL, MSSQL, Oracle, etc.) but in Derby it throws this error:
Caused by: java.sql.SQLSyntaxErrorException: Comparisons between 'DATE' and 'TIMESTAMP' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation (e.g. SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1')
I don't want to use CASTs because I cannot change the SQL query. Is this a bug from Derby that will get fixed sometime? I'm currently using Derby 10.8.2.2.
Thanks!
回答1:
If you are connecting from a java program you can get specific parts of a Timestamp using various getxxx() methods (these are actually inherited from Java.util.date)
See the docs http://docs.oracle.com/javase/7/docs/api/java/util/Date.html
and check the getDate(), getDay() getHour().... methods
回答2:
Perhaps you can define a VIEW on one of the tables, and include the CAST in the VIEW. That way, you don't have to change the actual query; it will go against the view, and thus include the cast in that way.
来源:https://stackoverflow.com/questions/11017780/comparing-timestamp-and-date-in-derby