views:

43

answers:

1

Hi,

I built a small query tool for Oracle using OracleCommand and OracleDataAdapter. Users just input a full query (no parameters), execute and the results are shown in a datagridview. So far so good, although I tried an invalid query, e.g.:

SELECT * FROM mytable WHERE dateColumn = '1-JAN-10'

This query is not valid SQL for Oracle. You have to use the to_date() function to compare with date literals. SQL developer also rejects it, but somehow my query tool just works. Does that mean my OracleCommand is a bit of a wizard here or am I doing something wrong? Also is there a way to omit this behavior because the purpose of the tool is testing queries, which should work always...

Thanks

+3  A: 

The query may be valid for Oracle. You don't have to use to_date() if you give the date string in your session's date format, though it's generally better to do that anyway to avoid issues like this.

It sounds like you have a different NLS_DATE_FORMAT in your tool's environment to that in SQL Developer, or the session date format is being set implicitly by OracleCommand.

You can select value from nls_session_parameters where parameter = 'NLS_DATE_FORMAT' to see what it is from SQL*Plus and SQL Developer, and from your tool; and from nls_database_parameters to see which is overriding the database default.

Looks like your tool may have DD-MON-RR and you're expecting some other format elsewhere, but without checking those tables it's hard to say where you're using the database default and where you're overriding it at session level. I'd guess that is the DB default though and you have an override in your other environments.

From SQL Developer, try alter session set nls_date_format='DD-MON-RR'; and then re-run your invalid query - should work there too.

Alex Poole
Sounds about right but I assumed (and read somewhere) that date literals don't exist in Oracle (probably older versions), and SQL developer also gave errors on it. Strange thing is that SQL developer seems to have a restriction that doesn't allow date literals. I want that too...
Koen
SQL Developer allows date literals, but it lets you set your date format too; see what you have under Tools->Preferences->NLS->NLS_DATE_FORMAT. Probably something other than DD-MON-RR, I'd guess, and different to the database default. Just to confuse things further, you can also use ANSI date literals, e.g. `myDate = DATE '2010-09-09'`, but that's another topic; except those didn't exist in earlier versions, which might be what you heard.
Alex Poole
Ok we use 10g, so I guess that classifies for "earlier versions", because I tested that repeatedly...
Koen