T-SQL for creating a text file with with this filename format - Month(MM)Day(dd)Year(yyyy).Log

  • WayneS (6/29/2010)


    vince.chittenden (6/29/2010)


    If the month function never returns a zero-length string (and it never will in this case, AFAICS), then why not '0' instead of '00'?

    You are correct, this would work.

    I just got into the habit years ago that to ensure I had two characters, to always specify two.

    I'm also in the same habit. It also helps a bit with "understanding" that there will be two characters always. It only costs 1 extra character, virtually no extra time even on a million rows, and makes the code more "bullet proof" (not in this case, but in most).

    --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)

  • Agile (6/28/2010)


    Thanks wayne for the suggestion, Im just wondering, Because when I use your solution, I did not declare variable for Date. Is there a difference in performance on doing that? Or just for the purpose of readability of the codes?

    declare @fn varchar(100),

    @cmd varchar(200)

    set @fn = 'E:\' +

    right('00' + convert(varchar(2), month(DateAdd(day, -1, getdate()))), 2) +

    right('00' + convert(varchar(2), day(DateAdd(day, -1, getdate()))),2) +

    convert(char(4), year(DateAdd(day, -1, getdate()))) + '.Log'

    SELECT @cmd = 'bcp "##LogWithNightShift" out ' + @fn + ' -f -fbcp.fmt -c -P"."'

    print @cmd

    --EXEC master..xp_cmdshell @cmd

    You can simplify that code a bit using the date formatting in the convert function:

    declare @cmd varchar(200)

    select @cmd =

    'bcp "##LogWithNightShift" out E:\' +

    replace(convert(varchar(10),getdate()-1,101),'/','')+

    '.Log -f -fbcp.fmt -c -P"."'

    print @cmd

    --EXEC master..xp_cmdshell @cmd

    Results:

    bcp "##LogWithNightShift" out E:\06282010.Log -f -fbcp.fmt -c -P"."

  • As a sidebar, I'd use format 112 so the file names are sortable.

    --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)

  • Thanks michael,yeah your right this one is more simple. And thanks to everyone.

    declare @cmd varchar(200)

    select @cmd =

    'bcp "##LogWithNightShift" out E:\' +

    replace(convert(varchar(10),getdate()-1,101),'/','')+

    '.Log -f -fbcp.fmt -c -P"."'

    print @cmd

    --EXEC master..xp_cmdshell @cmd

    Thanks,
    Morris

Viewing 4 posts - 16 through 18 (of 18 total)

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