Search code examples
sqlcmd

How do I name the output textfile to YYYYMMDD based on the system date?


How do I name the output textfile to YYYYMMDD based on the system date?

sqlcmd -S DataBBB -i c:\scripts\followup.sql
    -o %DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt -s ; -W -u

Now the output text file is 01_31_2012.txt. How can I change it to 2012_01_31.txt?


Solution

  • Tried it under Windows 7 Premium (German), may be dependent upon OS and Local Time Format.

    sqlcmd -S DataBBB -i c:\scripts\followup.sql 
                                -o %date:~-4%_%date:~3,2%_%date:~0,2%.txt -s; 
    

    You have to edit this part for your system

    %date:~-4%_%date:~3,2%_%date:~0,2%.txt
    

    The statement used the command line internal %date% - variable with the extension :~start,length. So you can create the filename with different parts from the date- variable.