passing external date variable to a query python

Question:

desired_date = "2022-09-28" #@param {type:"date"}
import pandas as pd

df = pd.io.gbq.read_gbq('''
  SELECT *
  FROM `gdelt-bq.gdeltv2.geg_gcnlapi`
  WHERE DATE(date) = desired_date
  LIMIT 1000
''', project_id=project_id, dialect='standard')

Trying to pass desired_date to the SQL query, but everything I seem to do results in the following error:
Reason: 400 No matching signature for operator = for argument types: DATE, INT64. Supported signature: ANY = ANY at [4:9]

Thank you for the assistance.

Asked By: Itay Etelis

||

Answers:

You must set the value of the variable in the query, not the variable name.
For eg.,

df = pd.io.gbq.read_gbq(f'''
  SELECT *
  FROM `gdelt-bq.gdeltv2.geg_gcnlapi`
  WHERE DATE(date) = "{desired_date}"
  LIMIT 1000
''', project_id=project_id, dialect='standard')

Just using = desired_date passes the query as with date as ‘desired_date’ and not its value!

Answered By: Kris

Alright, I think I solved it.

desired_date = "2022-09-28" #@param {type:"date"}
import pandas as pd

df = pd.io.gbq.read_gbq('''
  SELECT *
  FROM `gdelt-bq.gdeltv2.geg_gcnlapi`
  WHERE DATE(date) = "%s"
  LIMIT 1000
''' % (desired_date), project_id=project_id, dialect='standard')

So it seems like when you add %s it means for numeric values, but when "%s" is added actually the string itself presented.

Answered By: Itay Etelis