Solved

SQL syntax

Posted on 2008-10-01
4
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

'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 …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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