osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
set day=%day:~1,4%%day:~6,2%%day:~9,2%
set dd=%day:~6,2%
set mm=%day:~4,2%
set yyyy=%day:~0,4%
i got
BRAND_~6,2.63005
DEPARTMENT_4,26,2.63005
PRODUCT_4,26,2.63005
Assistant
It appears you are trying to extract a formatted date (YYYYMMDD) from the output of an SQL query. The SQL query is returning a date, and your intention is to manipulate that string into a more understandable format. However, your current approach to setting day, dd, mm, and yyyy is not resulting in the expected output, which is likely due to issues with how you are slicing and referencing the string.
User
output shold be
Assistant
If you're expecting the output of your script to be in the format YYYYMMDD, here’s how the complete script would look, including the necessary steps to ensure everything works as intended:
Complete Batch Script to Get Date in YYYYMMDD Format
User
set /p day=<date.dat
echo !day!
set mm=%day:~6,2%
echo !mm!
it shoul be 02, why display ~6,2
Assistant
The issue you're encountering is related to the way you are using the command and how variable expansion works in batch files. When you run the command , the variable holds the value read from the file. However, if you're trying to use Delayed Expansion to print and manipulate the value of the variable with , it requires the command.
Let’s fix your script step by step.
Here’s how the corrected script can look:
osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.dat
set /p day=<date.dat
:: Remove leading spaces if necessary
set day=%day: =%
:: Optionally display the raw value for debugging
echo Raw day value: %day%
:: The format of day in YYYY-MM-DD, need to do string slicing correctly
:: Now, assuming the string format is correct, reassign the values correctly
set yyyy=%day:~0,4%
set mm=%day:~5,2%
set dd=%day:~8,2%
:: Confirm outputs for debugging
echo YYYY: %yyyy%
echo MM: %mm%
echo DD: %dd%
:: Combine for YYYYMMDD format
set formattedDate=%yyyy%%mm%%dd%
echo Formatted Date: %formattedDate%
Key Changes and Explanations:
Fix Neglected Spaces: You might want to remove leading spaces from day. The command set day=%day: =% removes any spaces.
Output Debugging: I've added lines that echo the raw output and the parsed yyyy, mm, dd values. This helps in debugging.
Correct Indexing: Ensure that the indexing in set is appropriate to match the actual string you're dealing with. Traditionally, SQL Server convert(varchar, getdate(), 120) returns dates in the YYYY-MM-DD format. Thus, using set mm=%day:~5,2% and set dd=%day:~8,2% is correct given this format.
Final Formatting: After extracting the day, month, and year, you can concatenate them into a YYYYMMDD format.
Please test the above script within a command prompt environment that has access to the necessary DB and correct user permissions. Adjust the indexes if your SQL output format differs.
@echo off
REM Run the SQL command and capture the output to date.dat
osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.dat
REM Read the content of date.dat into the variable day
set /p day=<date.dat
REM Remove leading spaces
set day=%day: =%
REM Output for debugging
echo Raw day value: %day%
REM Extract year, month, and day
set yyyy=%day:~0,4%
set mm=%day:~5,2%
set dd=%day:~8,2%
REM Output for debugging
echo YYYY: %yyyy%
echo MM: %mm%
echo DD: %dd%
REM Combine for final formatted date
set formattedDate=%yyyy%%mm%%dd%
REM Display the final formatted date
echo Formatted Date: %formattedDate%
REM Optionally, you can also write it to a file
echo %formattedDate% > formatted_date.dat
Explanation of Key Lines
SQL Command:
osql ... > date.dat - This runs your SQL command and writes the output date to date.dat.
Set Day:
set /p day=<date.dat - This reads the first line of date.dat into the variable day.
Trim Leading Spaces:
set day=%day: =% - Trims any leading spaces that might be before the date string.
Extracting Components:
set yyyy=%day:~0,4% - Extracts the year.
set mm=%day:~5,2% - Extracts the month.
set dd=%day:~8,2% - Extracts the day.
Formatted Date:
set formattedDate=%yyyy%%mm%%dd% - Concatenates the year, month, and day into the YYYYMMDD format.
Output the Result:
echo Formatted Date: %formattedDate% - This displays the final formatted date.
Example Output
Assuming the output from your SQL query correctly provided a date like 2023-10-12, the final output displayed by the script would be:
Formatted Date: 20231012
If you enable the line that writes to the file, it will also save 20231012 to formatted_date.dat. Make sure to have access to your database and the required permissions for the osql command to run successfully.
set
set /p day=<date.dat
day
day
!
EnableDelayedExpansion
Here's a corrected version of your batch script with Delayed Expansion properly implemented, so you can extract and display the month as intended:
Updated Batch Script
@echo off
setlocal enabledelayedexpansion
REM Run the SQL command and capture the output to date.dat
osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.dat
REM Read the content of date.dat into the variable day
set /p day=<date.dat
REM Optionally output the raw value for debugging
echo Raw day value: !day!
REM Extract month using Delayed Expansion
set mm=!day:~5,2!
REM Output the extracted month
echo MM: !mm!
endlocal
Key Changes and Explanations
Enable Delayed Expansion:
The command setlocal enabledelayedexpansion allows you to use ! for variable expansion instead of %, which is necessary when you're modifying or using a variable in the same block of code.
Using Delayed Expansion:
When you refer to the variable day, you should use !day! instead of %day% to correctly access its value after it's been set.
Extract Month:
The extraction of the month with set mm=!day:~5,2! will now correctly fetch the month portion of the date.
Example Output
Assuming that date.dat contains a line like 2023-10-12:
Raw day value: 2023-10-12
MM: 10
If you want it to output as 02 for October, you need to ensure that your SQL query is returning the date in the expected format. The convert(varchar(10), getdate()-1, 120) should return it as YYYY-MM-DD which is standard ISO format.
If your expected output for mm is 02 when you meant the month number instead (for February), you might want to adjust your SQL query accordingly when testing.
In the provided batch script, the line set mm=!day:~5,2! is correct but will extract the month directly from the given date. For the date 2023-10-12, mm will indeed be 10 since it's the tenth month. Make sure your expected output aligns with the actual month you want to extract.