Numeric Data Types in Microsoft SQL Server

Introduction

In Microsoft SQL Server (MSSQL), the DECIMAL, FLOAT, and NUMERIC data types are used to store numerical values, but they have differences in terms of precision, storage, and behavior. Here's a comparison of these data types in MSSQL:

1. DECIMAL

  • DECIMAL is used to store exact decimal numbers.
  • It's commonly referred to as a "fixed-point" data type because it maintains a specific number of decimal places.
  • You specify the total number of digits and the number of decimal places (scale) when defining the column.
  • It's suitable for financial calculations where precision is crucial.
  • Storage is based on the specified precision and scale.
  • It takes up a fixed amount of storage space.
  • Example: DECIMAL(10, 2) can store numbers like 12345.67.
  • CREATE TABLE Sales (
        OrderID INT,
        TotalAmount DECIMAL(10, 2)
    );
    
    INSERT INTO Sales (OrderID, TotalAmount) VALUES (1, 12345.67);
    INSERT INTO Sales (OrderID, TotalAmount) VALUES (2, 9876.54);
    
    SELECT * FROM Sales;
    

    In this example, the TotalAmount column stores decimal values with a total of 10 digits, including 2 decimal places. It's suitable for financial calculations where exact precision is required.

2. FLOAT

  • FLOAT is used to store approximate floating-point numbers.
  • It's a "floating-point" data type, meaning that it stores numbers in scientific notation.
  • It offers a wider range of values but might have slight inaccuracies due to the nature of floating-point representation.
  • Storage space usage depends on the system's floating-point precision (typically 4 or 8 bytes).
  • FLOAT is suitable for scientific and engineering calculations where precision is less important than range.
  • Example: FLOAT can store values like 3.14159 or 1.23e-5.
CREATE TABLE Temperature (
    City VARCHAR(50),
    Celsius FLOAT
);

INSERT INTO Temperature (City, Celsius) VALUES ('New York', 20.5);
INSERT INTO Temperature (City, Celsius) VALUES ('Los Angeles', 25.75);

SELECT * FROM Temperature;

In this example, the Celsius column stores floating-point values, which might have slight inaccuracies due to the floating-point representation. It's suitable for scientific or engineering calculations where precision is less important than a wide range of values.

3. NUMERIC

  • In MSSQL, NUMERIC is a synonym for the DECIMAL data type.
  • It's used to store exact decimal numbers with a specified precision and scale.
  • You can use NUMERIC interchangeably with DECIMAL.
  • Like DECIMAL, you specify the total number of digits and the number of decimal places (scale) when defining the column.
  • It's suitable for scenarios where you need precise decimal calculations.
  • Storage is based on the specified precision and scale.
  • Example: NUMERIC(8, 3) can store numbers like 1234.567.
CREATE TABLE Products (
    ProductID INT,
    Price NUMERIC(8, 3)
);

INSERT INTO Products (ProductID, Price) VALUES (101, 12.345);
INSERT INTO Products (ProductID, Price) VALUES (102, 98.765);

SELECT * FROM Products;

Like the DECIMAL example, the Price column stores exact decimal values with a total of 8 digits, including 3 decimal places.

Remember that while DECIMAL and NUMERIC are often interchangeable, it's important to consult the documentation of SQL Server for any nuances or differences in behavior.


Similar Articles