Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concatenate String and Variables

Author  Topic 

ryanlcs
Yak Posting Veteran

62 Posts

Posted - 2007-07-04 : 20:51:03
I have below code working fine.
BACKUP DATABASE [eCARDB] TO  DISK = N'E:\1.bak' WITH  NOINIT ,  NOUNLOAD ,  NAME = N'eCARDB backup',  NOSKIP ,  STATS = 10,  NOFORMAT 


But I need to add a date to the backup file name. So I did this, but it failed. Seems like the concatenation doesnt work out.
BACKUP DATABASE [eCARDB] TO  DISK = N'E:\' + LTRIM(STR(DATEPART(YEAR,GETDATE()))) + '1.bak' WITH  NOINIT ,  NOUNLOAD ,  NAME = N'eCARDB backup',  NOSKIP ,  STATS = 10,  NOFORMAT 


Any idea?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-04 : 21:01:43
form the filename in a variable and pass the variable to in

declare @fname nvarchar(100)
select @fname = N'E:\' + CONVERT(VARCHAR(4), DATEPART(YEAR,GETDATE())) + '1.bak'

BACKUP DATABASE [eCARDB] TO DISK = @fname WITH NOINIT , NOUNLOAD , NAME = N'eCARDB backup', NOSKIP , STATS = 10, NOFORMAT



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ryanlcs
Yak Posting Veteran

62 Posts

Posted - 2007-07-04 : 22:27:45
That works. Thanks a lot.
Go to Top of Page
   

- Advertisement -