Please Help me for Float DataType

  • I got a new field called DailyCocumption by Float DataType defined.

    The field is divided into two types of fields in the table comes from Integer. With the following command:

    update dbo.tblgas01g4c40

    set DailyConsumption = Consumption/FindDiffRead where (FindDiffRead<>0)

    If the field values ​​and the Daily All times are Ayntjr divided 30/50 to be 6. Show, the table shows zero.

    please help me,thanks.

  • You are trying to devide integer by integer, the result will be also integer.

    To get float you should first convert your arguments to float like this, convert(float,@integer1)/convert(float,@integer2).

    Btw, remember, that float is not precise type.


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

  • hi

    Thanks for the answer

    I have almost two million data using the cursor do I need? Please give more explanation

  • No, you don't need a cursor, you may write it directly in query

    update dbo.tblgas01g4c40

    set DailyConsumption = convert(float,Consumption)/convert(float,FindDiffRead)

    where FindDiffRead<>0

    This will update all the rows in dbo.tblgas01g4c40, by setting to the column DailyConsumption float value, the result of devision.

    Also, a good thought might be avoiding zero devision error making check if FindDiffRead equals zero - do something.

    for example display null or something else. For that purpose you may use nullif function, like this:

    ...set DailyConsumption = convert(float,Consumption)/nullif(convert(float,FindDiffRead),0)...

    About float, be aware of such things:

    declare @m float = 150.20;

    select CAST((@m * 100) as int) as b --you may expect 15020, but it is 15019


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow

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

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