convert varchar to decimal

D

Dean G

I need to compare two values. one from a text field 'bid' and the other
from a field in an sql server database 'maxbid'.

The problem is the column in the database has decimal as its data type
and i'm getting a type mismatch. does anyone know how to convert 'bid'
into decimal from varchar? the field datatype doesnt necessarily have
to be decimal although i need two decimal places so it cant be an int.

Thanks,
Dean
 
R

Ray Costanzo [MVP]

Are you trying to do this comparison at the database level or in your code?
If database, show query. If code, show code.

Ray at home
 
M

Manohar Kamath

You can use CSng to convert it into a Single, but then make sure the
variable is not empty/numeric before you do the conversion.

If Trim(myVar) <> vbNullString And IsNumeric (myVar) Then
myVarSng = CSng(myVar)
End If
 
D

Dean G

thanks manohar that did the trick, i still had to change it from decimal
to float before it would work though, if anyone has the same problem.

Dean
 
C

Chris Hohmann

Dean G said:
thanks manohar that did the trick, i still had to change it from decimal
to float before it would work though, if anyone has the same problem.

Decimals are weird little creatures in VBScript. Unlike integers, longs,
etc... they are not a recognized variant subtype. Let me qualify that by
saying VarType() will return 14, but TypeName() will fail. Try
TypeName(decimalvalue) and you will receive the following error:

"Variable uses an Automation type not supported in VBScript"

So when performing arithmetic operations, one is forced to convert
decimals to a supported subtype using functions such as CInt,CDbl, and
most importantly CCur. The reason I emphasize CCur is that the currency
subtype is in fact a decimal with 4 point precision, masquerading as a
supported VBScript variant subtype. The problem with CInt is that you
lose data on the conversion and the problem with CDbl is that they
represent approximations of a number. It's beyond the scope of this
thread but basically it has to do with the fact that computers represent
numbers (and everything else) in binary. As such if a number cannot be
represented as the sum of the powers of 2, an approximation is made.

Response.Write 1 - .94
6.00000000000001E-020.99

So when dealing with decimals of precision less than or equal to 4, CCur
is a good choice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top