开发者

Add date to SQL database backup filename

开发者 https://www.devze.com 2022-12-22 15:09 出处:网络
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.

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.

BACKUP DATABASE [myDB] TO  DISK = N'\\开发者_JAVA技巧myPath\myDB.bak' WITH NOFORMAT, INIT,  NAME = N'myDB', SKIP, REWIND, NOUNLOAD,  STATS = 10
GO

Thanks!


DECLARE @MyFileName varchar(1000)

SELECT @MyFileName = (SELECT '\\ServerToSave\Path\MyDB_' + convert(varchar(500),GetDate(),112) + '.bak') 

BACKUP DATABASE [myDB] TO DISK=@MyFileName ...


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.


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


Use the following

DECLARE @BackupFileName varchar(20)

SELECT @BackupFileName = '\\ServerName\SharedFolder\DatabaseName_' + CONVERT (VarChar, GetDate(), 112) + '.bak'

BACKUP DATABASE [myDB] TO  DISK = @BackupFileName WITH NOFORMAT, INIT,  NAME = N'myDB', SKIP, REWIND, NOUNLOAD,  STATS = 10

Read up on Cast and Convert here http://msdn.microsoft.com/en-us/library/ms187928.aspx


Maybe you want to use windows task, just put this code in a .BAT file and add to your Windows tasks:

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "fullstamp=%YYYY%%MM%%DD%-%HH%%Min%%Sec%"
set bkfile=D:\bk-sqlserver\dbname%fullstamp%.bak
set path_sqlcmd="C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.exe"

%path_sqlcmd% -S .\SQLEXPRESS -E -Q "BACKUP DATABASE dbname TO DISK='%bkfile%' WITH FORMAT"

It's a bit long, but i think it's a practical solution.

  • Use sqlcmd for SQL Server 2005 or later

  • Use osql for SQL Server 2000 or oldies


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


You can use any of them. These Are Sample For Use Date And DateTime In Backup Database:

SELECT 'TestDb' + '_'+CAST(CAST(GETDATE() AS datetime2) AS NVARCHAR(22))+ 
'_Log.trn';--TestDb_2019-09-28 13:54:42.54_Log.trn

SELECT 'TestDb' + '_'+CAST(CAST(GETDATE() AS DATE) AS NVARCHAR(MAX))+ 
'_Log.trn';--TestDb_2019-09-28_Log.trn

SELECT 'TestDb' + '_'+CAST(sysdatetime() as nvarchar(max))+ '_Log.trn';--TestDb_2019-09-28 13:54:42.5536994_Log.trn

SELECT 'TestDb'+'_' + convert(varchar(500),GETDATE(),120) + '_Log.trn';--TestDb_2019-09-28 13:54:42_Log.trn

SELECT 'TestDb'+'_' + convert(varchar(500),GETDATE(),112) + '_Log.trn';--TestDb_20190928_Log.trn

SELECT 'TestDb' + '_'+CAST(FORMAT(getdate(), N'yyyy-MM-ddThh:mm:ss') as nvarchar(max))+ '_Log.trn';--TestDb_2019-09-28T02:08:38_Log.trn
0

精彩评论

暂无评论...
验证码 换一张
取 消