2017-06-21 152 views
-1

我創建了以下批處理文件來運行查詢,並將結果保存在.csv文件中。Sqlcmd減去日期

sqlcmd -S MyLogin -i LocationToSql -E -o "C:\Users\user\Desktop\query result\result2-%date:~-4,4%-%date:~-7,2%-%date:~-10,2%.csv" -s";" -w 700 

現在,我節省的結果爲:result2-2017-06-21.csv
但是,我想它是:result2-2017-06-20.csv

但我不知道如何減去1天的命令。
日期的定義如下:

%date:~-4,4%-%date:~-7,2%-%date:~-10,2% 
+0

另外,如果人們downvote,請告訴我爲什麼... – Mitch

+1

[此帖可能有所幫助](https://stackoverflow.com/questions/355425/date-arithmetic-in-cmd-scripting) – gms0ulman

+0

最有可能是因爲缺乏自己的研究/努力 - 看到這個:[問] – aschipfl

回答

1

我稍微修改腳本我最近張貼在這裏:

setlocal enabledelayedexpansion 

set day=%date:~-10,2% 
set month=%date:~-7,2% 
set year=%date:~-4,4% 

echo.%day%|findstr /r /b "0">nul&&set day=%day:~1% 
echo.%month%|findstr /r /b "0">nul&&set month=%month:~1% 
set /a day-=1 
if %day% lss 1 (
    set /a day+=30 
    set /a month-=1 
    if !month!==2 (set /a day-=2 
        set /a leap=year%%4 
        if !leap!==0 set day+=1 
    ) 
    for /l %%# in (1 2 7) do if %month%==%%# set /a day+=1 
    for %%# in (8 10 12) do if %month%==%%# set /a day+=1 
    if !month!==0 (
     set month=12 
     set /a year-=1 
    ) 
) 
set day=0%day% 
set month=0%month% 

echo %year%-%month:~-2%-%day:~-2% 

這將呼應昨日的YYYY-MM-DD格式的日期。您也可以使用set /a day-=1編輯該行以減去多重天數。