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.

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

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.

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.

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.

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.

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.

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?

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…

Community Initiatives

About Pinal Dave

Pinal Dave is a Pluralsight Developer Evangelist. He has authored 11 SQL Server database books, 17 Pluralsight courses and have written over 3200 articles on the database technology on his blog at a http://blog.sqlauthority.com. Along with 11+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). His past work experiences include Technology Evangelist at Microsoft and Sr. Consultant at SolidQ. Follow @pinaldave
Send Author Pinal Dave
an email at pinal@sqlauthority.com

Email Subscription

Enter your email address to subscribe to this blog and receive notifications of new posts by email.