0

Using plain Oracle SQL, I would use:

select *
from mytable
where 
to_date(date_d, 'dd-mon-yyyy') == to_date('01-Jan-2013', 'dd-mon-yyyy')

Now using SQLAlchemy, I'm at a loss.

from datetime import datetime

dt = myTable.c.date_d == datetime(2013, 1, 1)
q = session.query(myTable).filter(dt)
q.first()

Will give me incorrect results, because date_d is varchar, so it won't translate to date unless I run an Oracle function to convert the datatype within the query. How can I do that?

2
  • 1
    Step 1 - stop storing dates as text. Commented Apr 29, 2014 at 0:19
  • @DanBracuk, consider this an intellectual exercise. Commented Apr 29, 2014 at 1:11

1 Answer 1

2

Turns out I didn't need to convert the datatype. SQLAlchemy seems to do that transparently. However, If I feel like doing it explicitly:

from sqlalchemy import func

dt = func.to_date(myTable.c.date_d, 'dd-mon-yyyy') == datetime(2013, 1, 1)
q = session.query(dt)
q.first()

My incorrect results were due to an unrelated mistake.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.