I have a script that looks like:
sqlcmd -S ServerName -d database -E -i "C:\Report1.sql" -o E:\Folder\SubFolder\FileDate_%date:~-4%_%date:~4,2%.csv -s"," -w 700
The purpose is to automagically run a .sql file and export the results to a .csv file with the Date in the file name.
This script works fine if I run it from the Command Prompt, but when I throw it into a SQL Server Agent Job, it fails with the error message:
Sqlcmd: Error: Error occurred while opening or operating on file E:\Folder\SubFolder\FileDate_%date:~-4%_%date:~4,2%.csv (Reason: The filename, directory name, or volume label syntax is incorrect). Process Exit Code 1. The step failed.
Both are being run by Administrator and I'm using SSMS 2014. Also, the Job is Type: Operating system(CmdExec) and Run as: SQL Server Agent Service Account.
Any suggestions or alternate methods are welcome. My assumption is sqlcmd has a problem with the date-getting commands in the file name