简体   繁体   中英

SQL Server RTM version is not rounding up number

I have uploaded my product to Azure and SQL Server, but I found really interesting issue working with Azure. Azure by default provides RTM version of SQL Server, which is creating this issue.

The RTM version image for calculation - the screenshot shows how calculation outputs the value.

The second screenshot is my local SQL Server 2014 - so why does the RTM version have this issue? Is there any settings or solution for this to return proper value like in SQL Server 2014?

图 1

图 2

Decimal 0.00750 is value 7.4999999999999997E-3 when cast to float (although SSMS will show 0.0075). Using SQLCMD, you can see the actual value:

sqlcmd -Q"DECLARE @c float = 0.00750;SELECT @c as float,ROUND(@c, 3) AS rounded;"

Results:

float                    rounded
------------------------ ------------------------
   7.4999999999999997E-3    7.0000000000000001E-3

If you change the database compatibility level to 120 (SQL 2016) of the database (on-prem, Azure SQL Database Managed Instance, or Azure SQL Database):

ALTER DATABASE YourDatabase SET COMPATIBILITY_LEVEL = 120;

and repeat the same query, then the result is incorrectly rounded like your SQL 2016 instance:

float                    rounded
------------------------ ------------------------
   7.4999999999999997E-3    8.0000000000000002E-3

Remember that float is approximate and cannot exactly represent all decimal values. So Azure SQL Database is the correct rounding behavior but the behavior can be controlled with the database compatibility level to provide backwards compatibility, if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM