Solved

SQL syntax

Posted on 2008-10-01
4
281 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
4 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 250 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 250 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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