convert varchar to decimal

Discussion in 'ASP General' started by Dean G, Oct 11, 2004.

  1. Dean G

    Dean G Guest

    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

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Dean G, Oct 11, 2004
    #1
    1. Advertising

  2. 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

    "Dean G" <> wrote in message
    news:%...
    >
    > 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
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Ray Costanzo [MVP], Oct 11, 2004
    #2
    1. Advertising

  3. 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

    --
    Manohar Kamath
    Editor, .netWire
    www.dotnetwire.com


    "Dean G" <> wrote in message
    news:%...
    >
    > 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
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Manohar Kamath, Oct 11, 2004
    #3
  4. Dean G

    Dean G Guest

    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



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Dean G, Oct 12, 2004
    #4
  5. "Dean G" <> wrote in message
    news:%23I8%23n$%...
    > 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.
    Chris Hohmann, Oct 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    4
    Views:
    3,271
    Hazzard
    Jul 9, 2003
  2. luna
    Replies:
    1
    Views:
    13,830
    Kevin Spencer
    Feb 6, 2004
  3. Kenneth Keeley

    Shorten a SQL VarChar result.

    Kenneth Keeley, Oct 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    681
    Scott Mitchell [MVP]
    Oct 28, 2004
  4. Vitaliy
    Replies:
    1
    Views:
    473
    Peter Otten
    May 29, 2008
  5. valpa
    Replies:
    11
    Views:
    1,512
    Steven D'Aprano
    Mar 24, 2009
Loading...

Share This Page