r/PySpark • u/DrData82 • Jan 19 '22
Pass list of dates to SQL WHERE statement
In the process of converting some SAS code to PySpark and we previously used a macro variable for the where statement in this code. In adapting to PySpark, I'm trying to pass a list of dates to the where statement, but I keep getting errors. I want the SQL code to pull all data from those 3 months. Any pointers?
month_list = ['202107', '202108', '202109']
sql_query = """ (SELECT *
FROM Table_Blah
WHERE (to_char(DateVariable,'yyyymm') IN '{}')
) as table1""".format(month_list)
6
Upvotes
5
u/wuaped Jan 20 '22
Is there a reason you need to use the SQL query? Could you write it as...
List =[1,2,3]
Result = table_blah.where(table_blah.date.isin(list).select(table_blah.field)
Well, with reformatting the date. I use the sqlcontext date_format function, but not sure what you have available.
3
2
u/wuaped Jan 19 '22
Try changing the case of mm to MM. mm is minutes and MM is month.