I have developed a function which uses parametrized sql based on user input to fetch records. However, I am facing a type mismatch problem in the where clause of the query. please help me fix it. Start_Date and End_Date are string stype whereas record_date is datetime64[ns]. The Error is stating the the comparison between record_date and start_date and end_date has a type mismatch.
Asked
Active
Viewed 31 times
0
-
start_date = input("Enter start date (YYYY-MM-DD): ") months = int(input("Enter number of months: ")) start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d") end_date = start_date + relativedelta(months=months) start_date = str(start_date.date()) end_date = str(end_date.date()) sql_A = f""" SELECT a.record_date, a.x FROM edm.dm_{brand_name} a LEFT JOIN edm.dm_{brand_name}_x b ON a.x = b.x AND a.record_date = b.record_date WHERE a.record_date >='{start_date}' AND a.record_date <= '{end_date}' """ – Moinak Dey Jul 05 '23 at 13:22
-
Hi, you can use the to_date function in sql to convert your string to a database date https://www.w3schools.com/sql/sql_dates.asp. That a little bit differnt for each sql Database type (Postgres /Mysql ...) The best way would be to let your database framwork handle that and use proper parameters. An sqlalchemy example would be here: https://www.tutorialspoint.com/sqlalchemy/sqlalchemy_core_using_textual_sql.htm – Ludi Jul 05 '23 at 13:29
-
not working bro – Moinak Dey Jul 05 '23 at 14:04