Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL syntax

Posted on 2008-10-01
4
Medium Priority
?
307 Views
Last Modified: 2010-03-20
This is a Script I would like to Run at Month END to create a special Backup with the Month and year in the file name.


USE [700];
GO
BACKUP DATABASE [700]
TO DISK = 'E:\DB\' & DATENAME(month, getdate()-5) & DATENAME(year, getdate()-5) &'.Bak'
   WITH FORMAT,
      MEDIANAME = 'E_SQLServerBackups',
      NAME = DATENAME(month, getdate()-5) &' End of Month';
GO

Open in new window

0
Comment
Question by:kwitcom
2 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 1000 total points
ID: 22614187
Change your & to + in your string concatenation.  

Otherwise ... I don't see any problem.
0
 
LVL 11

Assisted Solution

by:aaronakin
aaronakin earned 1000 total points
ID: 22614216
Try this...

USE [700];
GO
DECLARE
      @FileName            VARCHAR(500),
      @BackupName            VARCHAR(500)

SELECT @FileName = 'E:\DB\'+ CAST(YEAR(GETDATE()) AS VARCHAR(4)) +'_'+ RIGHT('0'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2) +'.bak'
SELECT @BackupName = RIGHT('0'+CAST(MONTH(GETDATE()) AS VARCHAR(2)),2) +'/'+ CAST(YEAR(GETDATE()) AS VARCHAR(4)) +' End of Month'

BACKUP DATABASE [700]
TO DISK = @FileName
   WITH FORMAT,
      MEDIANAME = 'E_SQLServerBackups',
      NAME = @BackupName;
GO
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question