Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Date in BCP out file name Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 1:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5, Visits: 20
Hi,

How can I insert current date in BCP out file name?

I use this command, in SQL JOB step (type: Operating System ComdExec)
bcp "SELECT * FROM NW.dbo._au_NAPI" queryout "E:\kon_DATE.csv" -CACP -c -T -t"\";\"" -r"\"\"" -S DB

Have you any ideas?

Thanks,
gmac
Post #1421963
Posted Wednesday, February 20, 2013 7:33 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:54 PM
Points: 1,432, Visits: 3,229
you are running bcp from a shell, so get the current date (M/D/Y) part into a variable:

set DATEVAR=%DATE:~4,10%

as part of a filename you probably want to change the / to - like this:

set mydate=%DATEVAR:/=-%

then you can use %mydate% in your batch command wherever you want the current date, like today would be: 02-20-2013




The probability of survival is inversely proportional to the angle of arrival.
Post #1422085
Posted Wednesday, February 20, 2013 8:20 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:20 AM
Points: 37,072, Visits: 31,630
sturner (2/20/2013)
you are running bcp from a shell, so get the current date (M/D/Y) part into a variable:

set DATEVAR=%DATE:~4,10%

as part of a filename you probably want to change the / to - like this:

set mydate=%DATEVAR:/=-%

then you can use %mydate% in your batch command wherever you want the current date, like today would be: 02-20-2013


What language are you using? This doesn't appear to be T-SQL.

Never mind. I get it. These are batch commands at the command line level. Well done.

Can you think of a way to have the date come out in the ISO format of YYYYMMDD to support sortable file names using the batch commands?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1422114
Posted Wednesday, February 20, 2013 8:38 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:54 PM
Points: 1,432, Visits: 3,229
Jeff Moden (2/20/2013)

Can you think of a way to have the date come out in the ISO format of YYYYMMDD to support sortable file names using the batch commands?


didling with the String manipulation function in shell is a pain, but yes you can do it.

from what I understand you have to play some tricks to combine variables.
(I looked at this: http://stackoverflow.com/questions/7846560/how-concatenate-two-variables-in-batch-script)

anyway, I would start by getting each part of the current DATE into three variables:

set MM=%DATE:~4,2%
set DD=%DATE:~7,2%
set YY=%DATE:~10,4%

then create your new yymmdd variable by combining them:
setlocal EnableDelayedExpansion

set yyyymmdd=!%YY%%MM%%DD%!

I hate messing with batch scripting because it is so ugly (not like unix shells) but if you are a DBA you end up needing to use it at times.





The probability of survival is inversely proportional to the angle of arrival.
Post #1422121
Posted Thursday, February 21, 2013 1:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5, Visits: 20
If I insert variable in SQL JOB Step, the JOB exec stopped with error.
When I insert throughout code (variables, bcp command...) in .bat file, and I reference this file in SQL JOB, the JOB was successfully executed.

This is very interesting! Why get error, if code in JOB step, and why not if code insert in .bat file?

Sorry for my english!

Thanks
gmac
Post #1422440
Posted Thursday, February 21, 2013 6:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 21, 2014 1:54 PM
Points: 1,432, Visits: 3,229
gmac 41947 (2/21/2013)
If I insert variable in SQL JOB Step, the JOB exec stopped with error.
When I insert throughout code (variables, bcp command...) in .bat file, and I reference this file in SQL JOB, the JOB was successfully executed.

This is very interesting! Why get error, if code in JOB step, and why not if code insert in .bat file?


Those variables and string manipulation functions are native to the DOS command shell only. So you have to be running from a .bat file to use them.

Now that you have told me this was a SQLAgent job step, you could have generated file name with the current date in it using SQL, then passed the file name as a parameter to the .bat file.

Anyway, glad you got it working.





The probability of survival is inversely proportional to the angle of arrival.
Post #1422532
Posted Thursday, February 21, 2013 3:30 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, September 11, 2014 3:31 PM
Points: 358, Visits: 2,324
Since you are running a SQL Server Agent job, you can use SQL Server Agent tokens.
The one for date the Job started running is: $(ESCAPE_NONE(STRTDT))

Here's a link to the 2008R2 help...

http://msdn.microsoft.com/en-us/library/ms175575.aspx



Post #1422816
Posted Friday, February 22, 2013 6:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5, Visits: 20
PhilPacha (2/21/2013)
Since you are running a SQL Server Agent job, you can use SQL Server Agent tokens.
The one for date the Job started running is: $(ESCAPE_NONE(STRTDT))

Here's a link to the 2008R2 help...

http://msdn.microsoft.com/en-us/library/ms175575.aspx


Great, thank you!
I'm going to try it!

gmac
Post #1423016
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse