0

I'm trying to filter my SQL code in Python using a specified date in my where statement. For some reason it isn't filtering based on the date and I'm unsure why. Want to filter on report_dt > '2018-01-01' but result output is giving all data

import pandas as pd
startdate='2018-01-01'
test=pd.read_sql("""SELECT top 10 report_dt FROM db.table where report_dt>{} """.format(startdate) , connection)
print(test)


REPORT_DT
0  2014-02-15
1  2014-02-15
2  2014-02-15
user3242036
  • 645
  • 1
  • 7
  • 16

3 Answers3

0

sample Number is how it's done as per the link below.

startdate = '2018-01-01'
test = pd.read_sql("SELECT report_dt FROM db.table where report_dt > ? sample 10", connection, params=(startdate,))

Source: http://community.teradata.com/t5/Database/Limit-rows/td-p/5238

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
0

It's best to pass parameters rather than formatted strings to queries. Give this a try:

startdate = '2018-01-01'
test = pd.read_sql("SELECT top 10 report_dt FROM db.table where report_dt > ?", connection, params=(startdate,))

A better explanation than I can give can be found here (doesn't relate to Teradata specifically, but the concepts apply).

sjw
  • 6,213
  • 2
  • 24
  • 39
0

Check this link for which syntax paramstyle is configured on the underlying database. https://www.python.org/dev/peps/pep-0249/#id41 - paramstyle section. Paramstyle depends on the underlying database configuration.

Eg- for teradata database-

**
import teradatasql
teradatasql.paramstyle
result- 'qmark'

or

import importlib

def get_paramstyle(conn):
   name = conn.__class__.__module__.split('.')[0]
   mod = importlib.import_module(name)
   return mod.paramstyle

  get_paramstyle(conn=DBconnection) # DBconnection is connection object to the 
  database you want to connect.

#Pass date parameters to pd.read_sql.
date_param = ['2020-04-17']
SQL_string =  " SELECT * FROM tablename WHERE tablename.load_date >= ? "
df = pd.read_sql(sql=SQL_string, con= DBconnection, 
index_col='load_date',parse_dates='load_date',params=(date_param))