Reputation: 16015
I need to select by date in a SQL query, for example
SELECT * FROM foo WHERE date = '2009-09-09'
That query works in my Hypersonic test database, but not Oracle, which seems to requires:
SELECT * FROM foo WHERE date = TO_DATE('2009-09-09', 'yyyy-mm-dd')
Is there a way to select by date uniformly across these two databases?
Upvotes: 0
Views: 2202
Reputation:
HSQLDB 2.0 supports ANSI date literals just as Oracle. So if you can upgrade to HSQLDB 2.0, you can use:
SELECT * FROM foo WHERE date_column = DATE '2009-09-09'
in both database (actually a lot more databases even)
Upvotes: 1
Reputation: 14243
If you can, you can set your NLS_DATE_FORMAT in Oracle session, that way you do not need to use the TO_DATE function, oracle will do this for you behind the scenes.
SQL> select value from v$nls_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
DD/MM/YYYY
SQL> create table nls_date_test ( id number(10) , date_entered date );
Table created.
SQL> insert into nls_date_test values ( 1 , '31/05/2009' );
1 row created.
SQL> insert into nls_date_test values ( 2 , '30/05/2009' );
1 row created.
SQL> select * from nls_date_test where date_entered = '2009-09-09';
select * from nls_date_test where date_entered = '2009-09-09'
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> alter session set nls_date_format = 'YYYY-MM-DD';
Session altered.
SQL> select * from nls_date_test where date_entered = '2009-05-30';
ID DATE_ENTER
---------- ----------
2 2009-05-30
SQL> select value from v$nls_parameters where parameter = 'NLS_DATE_FORMAT';
VALUE
----------------------------------------------------------------
YYYY-MM-DD
SQL>
Upvotes: 0
Reputation: 16015
I found the answer - you can create the TO_DATE function in HyperSonic and then the second query works in both. For example, make the class:
public class Date {
public static String toDate( String value, String format ) {
return value;
}
}
And the query
SELECT * FROM foo WHERE date = TO_DATE('2009-09-09', 'yyyy-mm-dd')
works in both.
Upvotes: 2
Reputation: 11915
A "date = 'literal string'" predicate in Oracle is usually not recommended - it is sensitive to NLS_DATE_FORMAT settings and often leads to misunderstanding on what you're looking for in a result set (in your example above do you want all records for the day or just those created exactly at midnight?)
If you need a uniform query string for both databases, you might rename the table in Oracle and create a view with the name foo and cast the date datatype to varchar2 in the view logic. You'll probably need to add a function-based index to the table to allow efficient searching on the recast value.
Upvotes: 0
Reputation: 34271
You could try H2 database as your in memory database (http://www.h2database.com). It should have decent Oracle compablity mode.
Upvotes: 1