OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to use date variable in pd.read_sql query

  • Thread starter Thread starter Muhammad fahim
  • Start date Start date
M

Muhammad fahim

Guest
I am attempting to retrieve data from Oracle using Python for a Pandas dataframe. Although the code runs without any errors, it fails to display the results. The problem lies in the date format in Python. How to execute my code flawlessly in order to display the selected date data in the Pandas frame?

this is my code

Code:
import cx_Oracle
import pandas as pd
import datetime
start1 = datetime.date(2024, 6, 14)
conn = cx_Oracle.connect('hr/hr@DESKTOP-5CRGV89/orcl')
df = pd.read_sql("SELECT dDate FROM IPADDRESS WHERE TO_DATE(dDate, 'yyyy-mm-dd') between :startd and :startf", conn,params={"startd":start1,"startf":start1} ) 
print(df )

this is oracle data

Code:
TO_DATE(DDATE,'YYYY-MM-DD')
---------------------------
24-JUN-16                   
24-JUN-16                   
24-JUN-14

this is result

Code:
  df = pd.read_sql("SELECT dDate FROM IPADDRESS WHERE TO_DATE(dDate, 'yyyy-mm-dd') between :startd and :startf", conn,params={"startd":start1,"startf":start1} )
Empty DataFrame
Columns: [DDATE]
Index: []
PS C:\ERP> ^C
<p>I am attempting to retrieve data from Oracle using Python for a Pandas dataframe. Although the code runs without any errors, it fails to display the results. The problem lies in the date format in Python. How to execute my code flawlessly in order to display the selected date data in the Pandas frame?</p>
<p>this is my code</p>
<pre><code>import cx_Oracle
import pandas as pd
import datetime
start1 = datetime.date(2024, 6, 14)
conn = cx_Oracle.connect('hr/hr@DESKTOP-5CRGV89/orcl')
df = pd.read_sql("SELECT dDate FROM IPADDRESS WHERE TO_DATE(dDate, 'yyyy-mm-dd') between :startd and :startf", conn,params={"startd":start1,"startf":start1} )
print(df )
</code></pre>
<p>this is oracle data</p>
<pre><code>TO_DATE(DDATE,'YYYY-MM-DD')
---------------------------
24-JUN-16
24-JUN-16
24-JUN-14
</code></pre>
<p>this is result</p>
<pre><code> df = pd.read_sql("SELECT dDate FROM IPADDRESS WHERE TO_DATE(dDate, 'yyyy-mm-dd') between :startd and :startf", conn,params={"startd":start1,"startf":start1} )
Empty DataFrame
Columns: [DDATE]
Index: []
PS C:\ERP> ^C
</code></pre>
 
Top