Date in BCP out file name

  • 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

  • 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.

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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.

  • 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

  • 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.

  • 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

  • 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply