1

I'm trying to send today variable into SQL but it is not working.

import datetime from date

today = date.today()


stmt = "select agent_email from customer_interaction_fact where to_date(DT) >= + today + ORDER BY CONVERSATION_CREATED_TIME DESC"

3 Answers 3

4

You don't have to compute today's date in Python. Just use the PostgreSQL function CURRENT_DATE:

stmt = "SELECT ... WHERE TO_DATE(DT) >= CURRENT_DATE ..."
Sign up to request clarification or add additional context in comments.

1 Comment

This is the only answer I would consider; also worth pointing out the front end app should be aware of the TimeZone used by the server/should make sure that entries are written with a TimeZone that means "current_date" makes sense from the perspective of the user. Problems arise with the notion of "current" if the front end writes TimeZoneless dates that the back end then assumes are in a different time zone
0

What database engine you're using? You'd need to convert the python datetime object into string with format accepted by the database.

# In case YYYY-MM-DD
today_str = str(today)

stmt = f"""select agent_email 
           from customer_interaction_fact 
           where to_date(DT) >= datetime({today}, "YYYY-MM-DD") 
           order by CONVERSATION_CREATED_TIME DESC"""

Another solution, assuming the client (your program) is in the same timezone as the database engine, you could use your database engine datetime.now function. In SQLite for instance datetime('now')

Comments

-1

try like below

from datetime import date

today = date.today()


stmt = "select agent_email,aht_in_secs,queueid,EFFORTSCORE from facts.public.customer_interaction_fact where agent_email <> 'Bot' and aht_in_secs is not NULL and to_date(DT) >=" + today + "ORDER BY CONVERSATION_CREATED_TIME DESC"

1 Comment

Thanks for trying to help me, but unfortunately it did not work. However, it is resolved using CURRENT_DATE instead of passing variable.

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.