Error converting varchar to float

  • Hi,

    If I have a table with one column as Float type having some data like

    value_str

    0.6132889999999997

    10561.382324

    87.725239999999999

    When I use this statement, to

    convert existing data to length of 12,

    with 5 decimal place

    update xxx set value_str =

    convert(float,str(value_str,12,5))

    It will report error of 'Error converting data type from varchar to float'

    Please advise how to solve this issue.

    Thank you

     

     

     

  • I may be missing something, but I would convert to a decimal with a precision of 5 decimal places, so you end up with something like this:

    set nocount on

    declare @myTable table  (value_str float)

    insert into @myTable

    select 0.6132889999999997

    union

    select 10561.382324

    union

    select 87.725239999999999

    select convert(decimal(12,5),convert(varchar(12),value_str)) from @myTable

    Hope this gives you some pointers.

     

    S

  • Be aware you will still see numbers like 87.725239999999999 if you do a select value_str from xxx, even after your update.

    See thread http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=367756 for an extensive discussion on precision and storage in SQL/Server.

    To get the correct precision

    Select cast(value_str as decimal(12,5)) value_str from xxx



    Best Regards,

    Otto Schreibke

    The future is a foreign country, they do things differently there.
    (Stolen from Arthur C Clarke)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply