Hi I have a value that I want to make sure it is only numeric in stored procedure. I have following code
if (IsNumeric(myField) =0)
begin
--does some work
end;
It should check myField if the number is non numeric it does work if not it just continues. For some reason above code is throwing error. Any idea how to fix that. Error is that exception was thrown varchar to int. Please let me know Thanks
Check it like this instead:
Your expression is valid, I suspect that you are getting a value that is considered a numeric by the function, but cannot be converted to an integer. Try the following...
The convert statement will blow up with the error your are reporting...
Check this question out: T-sql - determine if value is integer
Some more examples of "Numeric" values that cannot be converted to an integer
Add a select
convert(int,myField)
in thebegin/end
to see the actual field value causing the error to occurAs others have pointed out,
isnumeric()
will return 1 on occasions where the string cannot be converted.The following tests for a positive integer:
The following tests for a positive integer or floating point number:
As a note, none of these methods test for numeric overflow. You could test for that with something like:
(assuming there are no leading 0s).