Thursday, December 4, 2014

How to add Date and Time to output file for Jobs

In one of my project I  come across this requirement that setup a job to run every hour and the output must have a Date and Time.

I found couple of solutions

1. When using SQLServer Management Studio(SSMS) you can add below syntax for output file

SSMS--> SQL Server Agent --> Job Name--> Properties --> Steps --> Select "edit" -->  Advanced 

$(ESCAPE_SQUOTE(DATE))_$(ESCAPE_SQUOTE(TIME))

For exL:-
D:\SQLBackup\ouput_file$(ESCAPE_SQUOTE(DATE))_$(ESCAPE_SQUOTE(TIME)).txt

2.  Use Tokens in Job setup

Refer below MSDN doc
http://msdn.microsoft.com/en-us/library/ms175575.aspx

3. use %date% format with sqlcmd

for ex:-
sqlcmd -S sqlserver,port -U <user> -P <password>  -i "c:\query.sql" -o "c:\ouput_%date%.txt"

You can play around %date% format if you are looking for specific date format.

Regards
Satishbabu Gunukula

No comments:

Post a Comment