SQL Server REAL Data Type
The REAL data type in SQL Server is used to store single-precision floating-point numbers. It occupies 4 bytes and can store values ranging from -3.40E +38 to +3.40E +38 with a precision of approximately 7 digits. The REAL data type is suitable for storing data that needs to represent larger or smaller numerical values, such as scientific or engineering calculations.
Syntax
In SQL Server, the syntax for the REAL data type is as follows:
REAL
Usage
The REAL data type is suitable for storing data that needs to represent larger or smaller numerical values, such as scientific or engineering calculations.
Compared to the FLOAT data type, the REAL data type occupies less storage space but has lower precision. Therefore, if high precision is required, the DECIMAL data type should be used.
Examples
Here are two examples using the REAL data type.
Creating a table:
CREATE TABLE Temperature (
Date DATE,
Temperature REAL
);
Inserting data:
INSERT INTO Temperature (Date, Temperature)
VALUES ('2022-01-01', 25.4),
('2022-01-02', 23.9),
('2022-01-03', 24.8),
('2022-01-04', 21.5),
('2022-01-05', 20.6);
Querying data:
SELECT *
FROM Temperature;
Results:
| Date | Temperature |
|---|---|
| 2022-01-01 | 25.4 |
| 2022-01-02 | 23.9 |
| 2022-01-03 | 24.8 |
| 2022-01-04 | 21.5 |
| 2022-01-05 | 20.6 |
Calculating the average temperature:
SELECT AVG(Temperature) AS AverageTemperature
FROM Temperature;
Results:
| AverageTemperature |
|---|
| 23.24 |
Conclusion
The REAL data type is suitable for storing data that needs to represent larger or smaller numerical values, such as scientific or engineering calculations. It occupies 4 bytes and can store values ranging from -3.40E +38 to +3.40E +38 with a precision of approximately 7 digits. If high precision is required, the DECIMAL data type should be used.