6

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" ')
6
  • Whats the error that you are getting? Maybe because of the date format? Date format should be separated by dash(-), somthing like this I Think A = '09-17-1990' and B = '09-30-1990' Commented Aug 20, 2019 at 1:17
  • Can you clarify how it is "not working". Can you provide some sample data and also show what the expected vs actual output is? Commented Aug 20, 2019 at 1:17
  • @RonelCalinisan No, the value its an int Commented Aug 20, 2019 at 1:39
  • @Dijkgraaf I dont know how to do it. Ive been trying a different ways to use the variables in the query but doesnt work Commented Aug 20, 2019 at 1:40
  • @Jozamvg so your date there is int and not Date? Commented Aug 20, 2019 at 1:42

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Strongly discourage the direct manipulation of strings to build a query, which is vulnerable to SQL injection. Use parameters instead: docs.databricks.com/dev-tools/python-sql-connector.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.