Convert Varchar into Int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flickimp
    New Member
    • Dec 2006
    • 37

    #1

    Convert Varchar into Int

    Hi

    I have imported some varchar figures.

    such as 0.050 (we'll call this Field)

    I need to convert them into int to be used for calculations.

    when I do

    CAST(Field as int)

    I get...

    Server: Msg 245, Level 16, State 1, Line 1
    Syntax error converting the varchar value '0.050' to a column of data type int.

    Normally I dont get this error?

    Anyone?
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Originally posted by flickimp
    Hi

    I have imported some varchar figures.

    such as 0.050 (we'll call this Field)

    I need to convert them into int to be used for calculations.

    when I do

    CAST(Field as int)

    I get...

    Server: Msg 245, Level 16, State 1, Line 1
    Syntax error converting the varchar value '0.050' to a column of data type int.

    Normally I dont get this error?

    Anyone?
    INT datatype (SQL Server Books online (BOL)

    Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) .

    therefore.... cast(field as decimal(18,3))
    or
    cast(field as numeric(18,3))

    will return

    .050

    INT will round to the nearest whole number.

    If calculating with decimalised data, numeric data precison and scale will demand decimal or numeric datatype

    cast in itself cannot explicitly cast a 0.050 'varchar' value to a whole number
    therefore inserting any 0.050 value in an integer would round it down to zero (the nearest whole number)


    Regards

    Jim :)

    Comment

    Working...