Alex S
Alex S

Reputation: 4884

Returning dates in pyodbc using the cursor.execute select command

I'm trying to use pyodbc to return dates that match certain criteria from an Oracle database. For example, I want to get all the dates between January 1, 2010 and January 1, 2011. To do this I'm doing this:

x = cursor.execute("""select Column from Table where OutputDate > '2010/01/01' and OutputDate < '2011/01/01'"""

Something similar worked when I was using an Access database, but now it gives me this error:

pyodbc.DataError: ('22007', '[22007] [Microsoft][ODBC driver for Oracle][Oracle]ORA-01861: literal does not match format string (1861) (SQLExecDirectW)')

I'm on Windows 7 and the driver I'm using is Microsoft ODBC for Oracle (which I guess is obvious from the error)

Does "literal does not match format string" mean that I can't use < and > with dates on this database? Could anyone help me?

Thanks a lot, Alex

Upvotes: 0

Views: 1732

Answers (1)

Ben
Ben

Reputation: 52893

Literal does not match format string means that you're trying to compare a date and a string. Change your query to the following and it should work.

select column
  from table 
 where outputdate between to_date('2010/01/01','yyyy/mm/dd') 
       and to_date('2011/01/01','yyyy/mm/dd')

Unless of course outputdate is not a date in which case it's best to transfer this into a date as well for your comparisions to work.

Note the between operator, which you can use in this situation.

There's a list of Oracle datatypes here; the site is good for everything.

I should add that I much prefer cx_Oracle for communicating with Oracle from python; though that is just personal preference.

Upvotes: 1

Related Questions