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)
 Renaming a Windows file with SQL statements

Author  Topic 

JPFish79
Starting Member

1 Post

Posted - 2014-05-21 : 12:31:54
I run the following command on SQL server:

declare @strExec varchar(4000)
set @strExec = 'G:\SQLBackup\SQLMant\Judgeassign\RenJdgD.bat judgeassign_DBbackup_' + ltrim(str(year(getdate()))) + '-' + ltrim(str(month(getdate()))) + '-' + ltrim(str(day(getdate()))) + '.bak'

exec xp_cmdshell @strexec

------------------------------------------
The RenJdgD.bat does the following:

G:
cd SQLBackup\SQLData\Judgeassign
rename judgeassign_DBbackup.bak %1

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

What this does as you can see is renames the windows file appending with the date, I want to also append the time, how do I do this?



John P. Fish
Network & Support Engineer
36th District Court
WP: (313)965-2769
FAX: (313)965-2208

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-21 : 16:26:46
Append CAST(CAST(GETDATE() AS TIME) AS VARCHAR(32)) (or a substring of that if you don't want the millisecond precision) to your existing string.

Edit: TIME data type is available only in SQL 2008 or later. I missed that you are on SQL 2005. You will have to use some other method to extract the time portion. For example:
RIGHT(CONVERT(VARCHAR(32), GETDATE(),113),12)
.

If that does not quite work for you, look at other formatting options here: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Go to Top of Page
   

- Advertisement -