SA0247A : Don’t use FLOAT, REAL, MONEY, SMALLMONEY or SQL_VARIANT data types
Introduction
Section titled “Introduction”Avoid using certain data types in SQL Server like FLOAT , REAL , MONEY , SMALLMONEY , and SQL_VARIANT due to precision and compatibility issues.
Description
Section titled “Description”In SQL Server, using data types like FLOAT and REAL can lead to precision issues because they are approximate-number types. This means not all values can be represented exactly, leading to rounding errors, which is problematic when precision is critical. Similarly, MONEY and SMALLMONEY do not store currency information alongside numeric values and have limited precision, which can also lead to inaccuracies in calculations. The SQL_VARIANT data type, while flexible, has numerous compatibility limitations and is not supported in several SQL Server features.
For example:
-- Avoid using FLOAT for precise calculationsCREATE TABLE Transactions ( Amount FLOAT);Using FLOAT for Amount can lead to rounding errors in financial calculations. It’s recommended to use DECIMAL or NUMERIC for better precision.
-
MONEYandSMALLMONEYlack currency context and may cause precision loss in complex calculations. -
FLOATandREALare imprecise and can lead to rounding errors, makingDECIMALa better choice. -
SQL_VARIANTis limited by its lack of support in several SQL Server features, including computed columns and full-text functions.
How to fix
Section titled “How to fix”Replace deprecated or imprecise data types with more precise and supported alternatives in SQL Server.
Follow these steps to address the issue:
1.Identify columns that use deprecated or imprecise data types like FLOAT , REAL , MONEY , SMALLMONEY , and SQL_VARIANT .
2.Determine the appropriate replacement data type based on the use case:
-
For precise numeric calculations, replace
FLOATorREALwithDECIMALorNUMERIC. Specify precision and scale to suit your needs. -
For currency-related values, replace
MONEYorSMALLMONEYwithDECIMALorNUMERICand add a currency context if needed. -
If
SQL_VARIANTis used, consider using a specific data type that aligns with the intended use to avoid compatibility issues.3.Alter the table to change the data type of the identified columns:
-- Example of altering a table to change data type ALTER TABLE Transactions ALTER COLUMN Amount DECIMAL(18, 2);4.Review any application logic or stored procedures that interact with these columns to ensure that they are compatible with the new data types.
5.Test your database and application to verify that the change does not introduce errors or unexpected behavior.
For example:
-- Correct usage with DECIMAL for precise calculationsCREATE TABLE Transactions ( Amount DECIMAL(18, 2));The rule has a ContextOnly scope and is applied only on current server and database schema.
Parameters
Section titled “Parameters”Rule has no parameters.
Remarks
Section titled “Remarks”The rule requires Analysis Context. If context is missing, the rule will be skipped during analysis.
Effort To Fix
Section titled “Effort To Fix”5 minutes per issue.
Categories
Section titled “Categories”Design Rules, Bugs
Additional Information
Section titled “Additional Information”There is no additional info for this rule.