Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Datetime in the filename

Posted on 2011-02-11
7
Medium Priority
?
555 Views
Last Modified: 2012-06-27
I have the following job running daily and dropping a file to the specified location.

EXEC xp_cmdshell 'bcp "exec ErX.dbo.sp_Ap1" queryout \\vat\users\L\IReport.xml -c -T'

How do I incorporate datetime into the file name IReport.
Thanks!
0
Comment
Question by:MPI_IT
[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
7 Comments
 
LVL 2

Expert Comment

by:ScottJones74
ID: 34873372
ireport_2_11_11.xml
0
 
LVL 7

Expert Comment

by:tlovie
ID: 34873407
This might work:

EXEC xp_cmdshell 'bcp "exec ErX.dbo.sp_Ap1" queryout \\vat\users\L\IReport' + convert(varchar(8), getdate(), 112)  + '.xml -c -T'
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34873474
declare @cmd varchar(128)
declare @filename varchar(24)

set @filename = 'IReport_' + CAST(DATEPART(Y, GETDATE()) as VARCHAR) + '_' +
                             CAST(DATEPART(M, GETDATE()) as VARCHAR) + '_' +
                             CAST(DATEPART(D, GETDATE()) as VARCHAR) + '.xml'
set @cmd = 'bcp "exec ErX.dbo.sp_Ap1" queryout \\vat\users\L\' + @filename + ' -c -T'                            
EXEC xp_cmdshell @cmd
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34873504
Change it to
declare @cmd varchar(128)
declare @filename varchar(24)

set @filename = 'IReport_' + CAST(DATEPART(YY, GETDATE()) as VARCHAR) + '_' + 
                             CAST(DATEPART(MM, GETDATE()) as VARCHAR) + '_' + 
                             CAST(DATEPART(DD, GETDATE()) as VARCHAR) + '.xml'
set @cmd = 'bcp "exec ErX.dbo.sp_Ap1" queryout \\vat\users\L\' + @filename + ' -c -T'                             
EXEC xp_cmdshell @cmd

Open in new window

0
 

Author Comment

by:MPI_IT
ID: 34873894
Hi ewangoya,
It woked well with date part. Is it possible to incorporate time in the file name too (hhmmss) ?
Thanks !


0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 1000 total points
ID: 34873931
try
declare @cmd varchar(128)
declare @filename varchar(128)

set @filename = 'IReport_' + CAST(DATEPART(YY, GETDATE()) as VARCHAR) + '_' + 
                             CAST(DATEPART(MM, GETDATE()) as VARCHAR) + '_' + 
                             CAST(DATEPART(DD, GETDATE()) as VARCHAR) + '_' +  
                             CAST(DATEPART(HH, GETDATE()) as VARCHAR) + '_' + 
                             CAST(DATEPART(MINUTE, GETDATE()) as VARCHAR) + '_' +
                             CAST(DATEPART(SS, GETDATE()) as VARCHAR) + '.xml'
set @cmd = 'bcp "exec ErX.dbo.sp_Ap1" queryout \\vat\users\L\' + @filename + ' -c -T'                             
EXEC xp_cmdshell @cmd

Open in new window

0
 

Author Closing Comment

by:MPI_IT
ID: 34874447
It worked perfectly. Thank you !
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

730 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