Add date to SQL database backup filename

前端 未结 7 585
旧时难觅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:20

    DECLARE @var nvarchar(max) ='C:\Projects\myDatabase ' +replace(rtrim(convert(char,getdate())), ':',',')+'.bak';
    BACKUP DATABASE myDatabase TO DISK = @var
    

    output:

    C:\Projects\myDatabase Jun 3 2015 6,33AM.bak

提交回复
热议问题