Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsNumeric function not working correctly

I have very strange problem with IsNumeric function in classic asp fail. Something like this happens in my code:

Response.write Score                // 79.617
Response.write IsNumeric(Score)     // false
Response.write IsNumeric("79.617")  // true

Has anyone had an idea why this could happen?

In the specifications it is said that the functions works with strings that can be converted into numbers, and from the example above you can see i get "true" result. But what can then cause my issue?

like image 832
gotqn Avatar asked Aug 20 '12 14:08

gotqn


People also ask

What can I use instead of IsNumeric?

Avoid using the IsNumeric() function, because it can often lead to data type conversion errors, when importing data. On SQL Server 2012 or later, use the Try_Convert() or Try_Cast() function instead. On earlier SQL Server versions, the only way to avoid it is by using LIKE expressions.

What does the IsNumeric function do?

Returns a Boolean value indicating whether an expression can be evaluated as a number. The required expressionargument is a Variant containing a numeric expression or string expression. IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.

What is the output of IsNumeric?

Using ISNUMERIC() function and getting the output. Here, 0 is returned as output as the stated expression is not numeric. Example-3 : Using ISNUMERIC() function and getting the output using a variable.

How do I use IsNumeric in SQL Server?

The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.


1 Answers

This means Score is simply not a string but rather something else, most likely coming from database.

To be on the safe side, use your own function:

Function My_IsNumeric(value)
    My_IsNumeric = False
    If IsNull(value) Then Exit Function
    My_IsNumeric = IsNumeric(CStr(value))
End Function

Response.write My_IsNumeric(Score)

The CStr() will make sure to convert anything except Null to a string, and to handle Null coming from database you have the IsNull() function.

like image 144
Shadow Wizard Hates Omicron Avatar answered Oct 11 '22 18:10

Shadow Wizard Hates Omicron