Add date to SQL database backup filename

前端 未结 7 648
旧时难觅i
旧时难觅i 2021-02-01 14:03

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         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 14:22

    Try this.

    DECLARE @MyFileName varchar(50)
    SELECT '\\ServerToSave\Path\MyDB_' + convert(nvarchar(20),GetDate(),112) + '.bak'
    BACKUP DATABASE [myDB] TO DISK=@MyFileName ...
    

    The 112 in the Convert gives you the YYYYMMDD format

提交回复
热议问题