troubleshooting Question

Windows Batch File using SQLCMD

Avatar of sherbug1015
sherbug1015Flag for United States of America asked on
Microsoft SQL ServerScripting LanguagesWindows Batch
1 Comment1 Solution1063 ViewsLast Modified:
I need to set the current date in a windows batch file and pass that date in a query using SQLCMD.

What I have so far is:

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)  -  This does not return the correct date.  Any ideas on what is wrong?

SQLCMD -s "myserver" -E -Q  "SET NOCOUNT ON;Declare @varoutput varchar(20);SELECT @varoutput = Select WorkDate from BusinessDayCalendar where WorkDate =mydate and nthBusinessDayMonth = 5 and Holiday is Null;SELECT Case when @varoutput is Null then '0' else @varoutput end;"

in the SQLCMD how do I return the value from the query.  For example  if today is the 5th business day then it should return a date, if not then it will return a 0.  I need to test whether it returned a date or not so I can execute a SQL Server Agent Job.

Any help would be appreciated.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros