I\'m using the below to backup a db from a SQL job. Can someone tell me how to add the current date to the output filename? Preferably in YYYYMMDD format.
BACK
If you want to include the date and time, so you can use:
DECLARE @MyFileName varchar(200)
SELECT @MyFileName='\\ServerToSave\Path\MyDB_' + REPLACE(convert(nvarchar(20),GetDate(),120),':','-') + '.bak'
BACKUP DATABASE [myDB] TO DISK=@MyFileName ...
The 120 in the Convert
gives you the yyyy-mm-dd hh:mi:ss(24h)
The REPLACE
function is necessary because the filename can not have the :
character.