P3P0
P3P0

Reputation: 165

How to use a variables in SQL statement in databricks?

I want to use a WHERE statement with two variables within the where clause. I've done research on this looking at how to use variables in SQL statements in Databricks and Inserting Variables Using Python, Not Working. I've tried to implement the solutions provided but it's not working.

a= 17091990
b = 30091990

df = spark.sql(' SELECT * FROM table WHERE date between "a" AND "b" ')

Upvotes: 6

Views: 13233

Answers (1)

anshul_cached
anshul_cached

Reputation: 762

You can use python's formatted string literals

df = spark.sql(f"SELECT * FROM table WHERE date between {a} AND {b} ")

For more about formatted string literals you can refer to https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498

Upvotes: 4

Related Questions