datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to rename a backup file with date suffix?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to demonstrate the renaming of database backup file with timestamp (datestamp, datetime stamp).

The last script shows how to include the datetimestamp in the filename at command execution time.

-- SQL dynamic command shell execution
DECLARE  @Command NVARCHAR(512)
DECLARE  @DateSuffix CHAR(8)
SELECT @DateSuffix = convert(CHAR,GETDATE(),112)
SELECT @Command = 'ren \\114.212.116.112\D$\data\backup\MYDB1\MYDB1.BAK ' +
                  'MYDB1' + @DateSuffix + '.BAK'
PRINT @Command
-- ren \\114.212.116.112\D$\data\backup\MYDB1\MYDB1.BAK MYDB120090322.BAK
 
EXEC MASTER.dbo.xp_cmdshell
  @Command
 
SELECT @Command = 'move \\114.212.116.112\D$\data\backup\MYDB1\' + 'MYDB1'
                  + @DateSuffix + '.BAK' + ' ' + '\\114.212.116.112\D$\data\backup\MYDB1\archive'
PRINT @Command
/*
  move \\114.212.116.112\D$\data\backup\MYDB1\MYDB120090322.BAK 
         \\114.212.116.112\D$\data\backup\MYDB1\archive
*/
EXEC MASTER.dbo.xp_cmdshell @Command
GO
 
-- SQL database backup with datetime stamp in backup file name
-- SQL database backup with dynamic backup file name
USE MASTER;
DECLARE  @FileName NVARCHAR(1024)
SET @FileName = 'F:\data\AdventureWorks2008\backup\AW2008_' +
                 REPLACE(REPLACE(CONVERT(VARCHAR(100),GETDATE(),111),'/','_'),
                                             ' ','_') + '.BAK'
 
SET @FileName = REPLACE(@FileName,'__','_')
PRINT @FileName
-- F:\data\AdventureWorks2008\backup\AW2008_2009_03_22.BAK
 
BACKUP DATABASE AdventureWorks2008 TO  DISK = @FileName WITH NOFORMAT,
INIT,  NAME = N'AdventureWorks2008-Full Database Backup'

GO
------------

-- Database backup with date in backup file name

DECLARE @BackupDate CHAR(8), @Backup nvarchar(1024)

SET @BackupDate = CONVERT(CHAR(8),CURRENT_TIMESTAMP,112)

SET @Backup = N'F:\data\backup\inventory' + @BackUPDate + '.bak'

BACKUP DATABASE Inventory TO DISK = @Backup

WITH INIT, NAME = N'Inventory Backup', STATS = 10

------------

Related link:

http://msdn.microsoft.com/en-us/library/ms175046.aspx

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.