SQL SERVER – Get Answer in Float When Dividing of Two Integer

Many times we have requirements of some calculations amongst different fields in Tables. One of the software developers here was trying to calculate some fields having integer values and divide it which gave incorrect results in integer where accurate results including decimals was expected.

Something as follows,

Example,

USE [AdventureWorks]
GO
CREATE TABLE [dbo].ConvertExample(
[ID]          [int] NULL,
[Field1]    [int] NULL,
[Field2]    [int] NULL,
[Field3]    [int] NULL,
[Field4]    [int] NULL
)
GO
INSERT INTO [dbo].ConvertExample
VALUES (1,30,40,60,80)
GO
INSERT INTO [dbo].ConvertExample
VALUES (2,20,10,50,80)
GO
INSERT INTO [dbo].ConvertExample
VALUES (3,15,140,90,60)
GO
INSERT INTO [dbo].ConvertExample
VALUES (1,60,0,5,2)
GO
SELECT *
FROM [dbo].ConvertExample
GO
SELECT (SUM(Field1)+SUM(Field2)+SUM(Field3))/SUM(Field4)
AS AnswerInt
FROM [dbo].ConvertExample
GROUP BY ID
GO

However, We need to CAST or CONVERT the numerator to solve this or we can say that to get the results we wanted.

Example using CAST

SELECT CAST((SUM(Field1)+SUM(Field2)+SUM(Field3))AS FLOAT)/SUM(Field4) AS AnswerFloat1
FROM [dbo].ConvertExample
GROUP BY ID
GO

Example Using CONVERT:

SELECT CONVERT(FLOAT,(SUM(Field1)+SUM(Field2)+SUM(Field3)))/SUM(Field4) AS AnswerFloat2
FROM [dbo].ConvertExample
GROUP BY ID
GO

Conclusion

When we expect the results in floating or decimals then we need to convert or cast the numerator part to get the accurate results with FLOAT or DECIMAL Whichever needed.

Reference : Pinal Dave (http://blog.SQLAuthority.com)

About these ads

17 thoughts on “SQL SERVER – Get Answer in Float When Dividing of Two Integer

  1. Nice post and something that is very much useful in the day-to-day programming life.

    Just wanted to add to what is already explained, that, SQL Server automatically does a cast to the data type having the highest precedence. So the result of INT and INT will be INT, but INT and FLOAT will be FLOAT because FLOAT has a higher precedence. If you want a different data type, you need to do an EXPLICIT cast.

    For example:

    SELECT 100/3 AS Result
    /*
    Result
    ———–
    33
    */

    SELECT 100/3.0 AS Result
    /*
    Result
    —————————————
    33.333333
    */

    The precedence of each data type is documented here: http://msdn.microsoft.com/en-us/library/ms190309(SQL.90).aspx

    Like

  2. Thank you Jacob,

    I like your additional explanation. I just realize after reading your explanation that I should include your explanation in the post itself so it is visible to everybody right away.

    Also this has brought another idea in my mind that when creating calculated field the same scenario happens. E.g. When we create calculated field from additions of INT it will give us final calculated field as INT. If we want that in another datatype we should have either follow your suggestion or CAST them.

    Regards,
    Pinal

    Like

    • Certainly this produces a float, but the value depends on precedence of / & *, which is non-obvious to casual readers. I would specify A / (B * 1.0) or (A / B) * 1.0 or A * 1.0 / B. The first & third do what the original questioner wanted. The second does integer division and turns the answer into a float.

      Like

    • Certainly it produces a float, but I don’t think the value is what the original post wanted. The integer divide has to happen first, and the result of that is converted to float. A / (B * 1.0) and A * 1.0 / B b both perform floating point division.

      > sqldf(“select 8 / 3 * 1.0″)
      8 / 3 * 1.0
      1 2

      Like

  3. Pingback: SQL SERVER - Puzzle - Computed Columns Datatype Explanation Journey to SQL Authority with Pinal Dave

  4. Pingback: SQL SERVER - Puzzle - Solution - Computed Columns Datatype Explanation Journey to SQL Authority with Pinal Dave

  5. This has caught me many times…
    Putting the 1.0 at the beginning of the line makes a difference:

    declare @a int,@b int,@c float
    set @a=1
    set @b=2

    select @a/@b – Result 0
    select @a/@b*1.0 – Result 0.0
    select 1.0*@a/@b – Result 0.500000

    Like

  6. Thank You very much for this examples..I have tried i got answer..But one thing if i need 1.20 instead of 1.2089876 what i have to do?

    Like

  7. Thanks for the post Dave et al. I must say, after beginning my professional career as a developer, your blog has been a top reference of mine (along with MSDN of ciurse) when I need help solving a problem with SQL.

    Like

  8. hello sir my table is able to store the float value like 75.5 but if i am going to store like 125.00,50.00 etc it will store 125 , 50 not giving me decimal places
    what is the solution if i want to store the values with 00 in decimal places?

    Like

  9. Pingback: SQL SERVER – Weekly Series – Memory Lane – #048 | Journey to SQL Authority with Pinal Dave

  10. Thank you so much for your post, i’m learning on MSSQL and have been searching how to sum multiple columns, divide it and get a result with required decimal point…your blog has again answered to my questions and I always got answers from your blog…good blog and best blog so far IMHO…

    A BIG THANK AGAIN.

    JM

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s