How to Convert Doubles to Integers in SQL
Double-precision floating point numbers are represented by the "float" data type in SQL Server. Occasionally, it may be necessary to convert a floating point number to an integer. This is achieved through the use of the T-SQL "CAST()" function. The "CAST()" function will not automatically round decimals up or down but can be combined with the T-SQL "ROUND()" function if rounding is desired.
Open SQL Server Management Studio.
Open a new query window.
Convert a floating-point number to an integer without rounding using the CAST() function by executing the following query statement, replacing "
Convert and round a floating-point number to an integer using the CAST() function by executing the following query statement, replacing "
Tips & Warnings
- The "CONVERT()" function can be substituted for "CAST()", if preferred.
- The "ROUND()" function returns a numeric data type. If you do not cast the float as a decimal within the function call, an arithmetic overflow error can occur in certain situations when the rounded number results in more digits than the original argument.
- Attempting to convert a number less than -2,147,483,647 or greater than 2,147,483,647 will result in an arithmetic overflow error because the number is outside the range of the integer data type.