0

I have an SQL query in my python script that starts with the following query:

query = """
DECLARE @StartDate AS Date 
SET @StartDate = '2018-10-31'

SELECT  ...
FROM ...
...
""" 

At the end of my python script im creating two csv files as such:

df.to_csv('C:/path/a_05-11-18.csv', sep=",",
                       encoding='utf-8', index=False)
df2.to_csv('C:/path/b_05-11-18.csv',
                           sep=",", encoding='utf-8', index=False)

I would like that when I change the date, in the

SET @StartDate = '2018-11-05'

that my dates in my file name changes accordingly.

num3ri
  • 664
  • 11
  • 15
Roger Steinberg
  • 1,228
  • 7
  • 29

1 Answers1

0

Store the date in a variable as string.

Change it to a desired format if you need to. For that, see here Parse date string and change format.

Now let's say the the new date string is stored in the variable modified_date.

When you're writing the csv file, the first argument of the to_csv function should look like this: 'C:/path/part_of_filename_etc' + modified_date + '.csv'

ba_ul
  • 1,701
  • 4
  • 20
  • 33