Check variable is number
Moderator: DosItHelp
-
- Posts: 1
- Joined: 25 Oct 2012 08:38
Check variable is number
I have a process that checks the output of the previous command is greater than 0.
for /f "delims=" %%a in ('sqlcmd -S host -E -h-1 -Q "SET NOCOUNT ON; SELECT MAX(Com_Num) AS MaxCompVal FROM status"') do @set MaxComp=%%a
if %MaxComp% > 0 goto FPass
:FPass
<Commands>
The above process runs successfully if the sqlcmd returns a number but sometimes the database is not available when its in maintenance and I am getting an error message. I want perform whether the MaxComp value is a number befor evaluating it.
In short I want to check a command vairable value is all numbers or not and based on the evaluation I want to do some action.
Thanks in advance.
for /f "delims=" %%a in ('sqlcmd -S host -E -h-1 -Q "SET NOCOUNT ON; SELECT MAX(Com_Num) AS MaxCompVal FROM status"') do @set MaxComp=%%a
if %MaxComp% > 0 goto FPass
:FPass
<Commands>
The above process runs successfully if the sqlcmd returns a number but sometimes the database is not available when its in maintenance and I am getting an error message. I want perform whether the MaxComp value is a number befor evaluating it.
In short I want to check a command vairable value is all numbers or not and based on the evaluation I want to do some action.
Thanks in advance.
Re: Check variable is number
Code: Select all
@echo off &setlocal enableDelayedExpansion
call :isNumber -5
call :isNumber 0
call :isNumber +5
call :isNumber notANumber
call :isNumber
set "var=50" &call :isNumber var
set "var=notANumber" &call :isNumber var
pause
exit
:isNumber
:: (
set "_=" &set "_=!%~1!" ||set "_=%~1"
::
set /a $err = 0
::
if !_!.==. set /a $err = 1
::
if !_! neq 0 set /a !_! %% !_! 2>nul ||set /a $err = 1
::
if !$err! == 0 (echo. isNumber !_! ) else echo. NOTaNumber !_!
:: )
exit /b %$err%
Code: Select all
isNumber -5
isNumber 0
isNumber +5
NOTaNumber notANumber
NOTaNumber
isNumber 50
NOTaNumber notANumber
Druk op een toets om door te gaan. . .
Re: Check variable is number
This should generally work.
The above could falsely report a number if the value is a valid numeric expression but not a number - something like "5+10".
The following will strictly test if the value is a valid decimal number:
Dave Benham
Code: Select all
setlocal enableDelayedExpansion
2>nul set /a "!MaxComp!" && if !MaxComp! gtr 0 goto FPass
The following will strictly test if the value is a valid decimal number:
Code: Select all
setlocal enableDelayedExpansion
2>nul set /a "test=!MaxComp!" || set "test="
if !test!==!maxComp! if !MaxComp! gtr 0 goto FPass
Dave Benham
Re: Check variable is number
One way to check if a variable is not negativ is following code.
This code work perfectly when the number have not any characters as the numbers.
i.E "2", "23", "234", "2345", "23456"
But not "2.345" or "23,456"
To understand the FINDSTR command:
caret -- is the the beginn of the value
[1-9] -- one number from 1 to 9 for the first character
[0-9]* -- more numbers from 0-9 posibble or not more numbers
$ -- is the end of the value
Here the code block for the problem:
Code: Select all
@echo off
for /l %%a in (-15,1,15) do echo %%a| findstr /r "^[1-9][0-9]*$">nul && echo %%a
This code work perfectly when the number have not any characters as the numbers.
i.E "2", "23", "234", "2345", "23456"
But not "2.345" or "23,456"
To understand the FINDSTR command:
caret -- is the the beginn of the value
[1-9] -- one number from 1 to 9 for the first character
[0-9]* -- more numbers from 0-9 posibble or not more numbers
$ -- is the end of the value
Here the code block for the problem:
Code: Select all
...
echo %MaxComp%| findstr /r "^[1-9][0-9]*$">nul && goto :FPass
...
Re: Check variable is number
This should work:
firmindani wrote:I have a process that checks the output of the previous command is greater than 0.
for /f "delims=" %%a in ('sqlcmd -S host -E -h-1 -Q "SET NOCOUNT ON; SELECT MAX(Com_Num) AS MaxCompVal FROM status"') do @set MaxComp=%%a
for /f "delims=0123456789A" %%a in ("%MaxComp%") do if "%%a"=="A" if not "%MaxComp%"=="0" goto :FPass
echo MaxComp variable fails comparison
pause
goto :EOF
:FPass
<Commands>
Re: Check variable is number
Edit:
This is for a one digit output number Only in range of 1 to 9
why not using
This is for a one digit output number Only in range of 1 to 9
why not using
Code: Select all
For %%a in (1 2 3 4 5 6 7 8 9) Do ( IF "%MaxComp%" == "%%a" IF not "%MaxComp%" == "0" ( Do_Command ) )
Last edited by abc0502 on 26 Oct 2012 06:11, edited 1 time in total.
Re: Check variable is number
abc0502 wrote:why not usingCode: Select all
For %%a in (1 2 3 4 5 6 7 8 9) Do ( IF "%MaxComp%" == "%%a" IF not "%MaxComp%" == "0" ( Do_Command ) )
This assumes the number is from 1 to 9 and nothing else, right?
Re: Check variable is number
yes, he check if the output is greater than "0"
Edit:
and there is no need for the 2nd If that check if the output is not equal to zero,
as it won't be in the range 1 to 9
Edit:
and there is no need for the 2nd If that check if the output is not equal to zero,
as it won't be in the range 1 to 9
Re: Check variable is number
The OP hasn't stated that the number is from 0 to 9. I assumed when he says "variable is all numbers' that the number can be 2 or 3 digits etc.
Re: Check variable is number
Oh, i didn't thought about that , what came in my mind that it could be a number as an error level "one digit" not a number that could consist of more that one digit.