i am trying to make sure that a variable is a number, before i update the database with it (the page just gets an error at the moment, which is ok but i would prefer if it jsut redirected back to the form
Announcement
Collapse
No announcement yet.
making sure a variable is a number
Collapse
X
-
Tags: None
-
Remember that in VBScript the variables are of the datatype of variant. So if you want to ensure that the value of the variable is an integar and not a string, use something like the following
If Not IsNumeric(someVariable) Then CInt(someVariable)
Comment
-
ok true The point I was trying to make is that because with vbscript you can not declare the variables datatype when you declare the variable like you do in vb like below
vb example - Dim intThisVariable as Integar
vbscript example - Dim intThisVariable
Therefore if you are debugging and do a response.Write intThisVariable, you may see 123 but this does not mean it is an integar at all. It very well could be a string. If you are trying to do an update on a database with the datafield set to be an integar or a number datatype and you get an error then use CInt to ensure that this variable is indeed an integar.
Comment
-
Question for you castali, since we can not know the value of the variable, assigning a specific value such as var = 0 in my case would not work. So what is wrong with using CInt to ensure that it was being read as an integar? I ask because I used this to solve a SQL query I wrote a few years ago where the value to human eyes was a number but it was actually a string and not an integar and that was causing the SQL query to generate an error. My query looked something like
sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = & " intID & ""
when the response.write sSQL was done it generated
SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234 yet there was an error so all I had to do was CInt(intID) and it worked like a charm.
Comment
-
really strange
there is no difference between Cint(20) and 20 .. for ASP3
... it is not the same for VB NET
and if you do a response.write sSQL after the Cint you wil get the same than before .... or ?
If Not IsNumeric(intID) Then
Exit Sub
Else
sSQL = "SELECT coid,coName,address,city,state,phone FROM tableA WHERE coid = "& intID &" ;"
' ....do the job
End If
should work
Comment
-
That was what I thought before hand, and I found out differently. It was because the value of intID was pulled from other databases as well as other tables from within this particular database, and sometimes the datatype of the column was text (string). Therefore when I ran the SQL query it was seeing it as a string and not as an integar so I just used CInt(intID) to ensure that the query read the value as an integar. Also sometimes the value may have been stripped from inside a longer string and then CInt was needed to make sure it was read as an integar
Comment
-
Let me ask you something else... do you mean number, as in:
1.234
+12,345
or are you looking for just DIGITS (for instance, an integer)?
If you want to check your data for that, there's a more reliable way, using regular expressions:
Code:<% Function IsDigits(str) Dim objRegEx Set objRegEx = New RegExp objRegEx.Pattern = "^\d+$" IsDigits = objRegEx.Test(str) Set objRegEx = Nothing End Function Response.Write(IsDigits("1.35")) 'Will produce false %>
Former ASP Forum Moderator - I'm back!
If you can teach yourself how to learn, you can learn anything. ;)
Comment
-
I agree with castali
Originally posted by miranda
My query looked something like
sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = & " intID & ""
when the response.write sSQL was done it generated
SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234 yet there was an error so all I had to do was CInt(intID) and it worked like a charm.
strID = "123"
sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = " & strID
You don't have to CInt it, just don't put single quotes around the coid value like below, so that SQL will treat it as a number and not a string.
strID = "123"
sSQL = "SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = '" & strID & "'"
I'm puzzled why this didn't work for you
SELECT coid, coName, address, city, state, phone FROM tableA WHERE coid = 1234
Doing a CInt or not in the vbscript variable that holds the value 1234 produces the same SQL statement as what castali earlier mentioned.
Comment
Comment