SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date in BCP out file name


Date in BCP out file name

Author
Message
gmac 41947
gmac 41947
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
sturner
sturner
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5186 Visits: 3259
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.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217549 Visits: 41995
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sturner
sturner
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5186 Visits: 3259
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.
gmac 41947
gmac 41947
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
sturner
sturner
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5186 Visits: 3259
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.
PhilPacha
PhilPacha
SSC Eights!
SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)SSC Eights! (940 reputation)

Group: General Forum Members
Points: 940 Visits: 2529
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



gmac 41947
gmac 41947
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search