Are you using sqlcmd to execute the query ?
Assuming so, here’s a way to do it ( though I’m sure there must be a cleaner way to do it):
<pre>sqlcmd -U username -P password -S SERVER\INSTANCE -Q “SELECT TOP(1) week_ending_date FROM your_table” -o <b>date.txt</b> -h -1
set /p <b>var</b>= < <b>date.txt</b>
sqlcmd -U username -P password -S SERVER\INSTANCE -Q “YOUR SQL QUERY” -o <b>file</b></pre>
In summary, it executes a query to read the ending date and puts the result into a temp file, whose contents are then assigned to a variable, which is used to construct the file name.
Hope this helps.