sql server is always in the division result is 0 (no decimal precision)

Occasionally found in SQL Server division result is always zero phenomenon, the phenomenon is called, but not called BUG, ​​because in fact the result of the calculation is not wrong.

As a concrete example:

SELECT 500/520; -- 0

Can be seen by this example, the results statement is 0. This is because SQL Server will automatically go to take the accuracy of the calculation results based on the maximum precision divisor and the dividend, where the divisor and the dividend are integers, so the result is also an integer.

To verify this conclusion:

SELECT 500.0/520; -- 0.961538
SELECT 500/520.0; -- 0.961538

It can be seen by the above verification, as long as in the dividend and the divisor is a decimal, the result will be a decimal number.

In this case, in SQL Server if there is division in the result is always zero phenomenon, do not panic, just know the reason, you can simply be solved by the following techniques:

SELECT (500 + 0.0)/520; -- 0.961538

The trick is to add a decimal divisor dividend or a 0, and the results have a precision, there would always be a 0.

 

"Death is like water disappearing in the water, causing almost no trace of waves."

Guess you like

Origin www.cnblogs.com/yanggb/p/11841895.html