SQL Server FLOAT(N) Data Type

The FLOAT(N) data type in SQL Server is used to store floating-point numbers. Unlike DECIMAL, FLOAT(N) uses binary floating-point numbers, which can represent a larger range of values but with lower precision. The N in FLOAT(N) represents the precision of the numeric value, and can be any integer between 1 and 53.

Syntax

The syntax for creating a table using the FLOAT(N) data type is as follows:

CREATE TABLE TableName (
    ColumnName FLOAT(N)
);

Where TableName is the name of the table, ColumnName is the name of the column using the FLOAT(N) data type, and N is the precision of the numeric value, which can be any integer between 1 and 53.

Use Cases

FLOAT(N) is suitable for storing data that requires representing very large or very small numeric values, such as scientific or financial calculations. Since FLOAT(N) uses binary floating-point numbers, DECIMAL data type should be used for high-precision requirements.

Examples

Example 1

Here is an example using the FLOAT(N) data type to store a set of student grades:

CREATE TABLE StudentGrade (
    ID INT PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    Grade FLOAT(4) NOT NULL
);

INSERT INTO StudentGrade (ID, Name, Grade)
VALUES (1, 'Alice', 90.5),
       (2, 'Bob', 87.3),
       (3, 'Charlie', 92.8);

SELECT * FROM StudentGrade;

In the example above, we created a table named StudentGrade with three fields: ID, Name, and Grade. The Grade field uses the FLOAT(4) data type, with a precision of 4. We inserted three records, each containing a student’s ID, name, and grade.

Example 2

Here is an example using the FLOAT(N) data type to calculate the area of a circle:

DECLARE @Radius FLOAT(24) = 2.5;
DECLARE @PI FLOAT(24) = 3.14159265358979323846;
DECLARE @Area FLOAT(24);

SET @Area = @PI * POWER(@Radius, 2);

SELECT @Area;

In the example above, we declared a circle with a radius of 2.5 and calculated its area. We used the FLOAT(24) data type to store the radius, pi, and area to ensure sufficient precision.

Conclusion

The FLOAT(N) data type is suitable for storing data that requires representing very large or very small numeric values, such as scientific or financial calculations. Since it uses binary floating-point numbers, the DECIMAL data type should be used for high-precision requirements. When using the FLOAT(N) data type, the value of N should be determined based on actual needs to minimize storage space and improve performance.