SQL Server DECIMAL Data Type

The DECIMAL data type is one of the data types used in SQL Server to store precise numbers. It can store fixed-point values, which are numbers with a fixed number of decimal places. The DECIMAL data type is very useful for precise calculations, such as in financial or currency calculations, where it is necessary to retain the exact decimal places.

Syntax

The syntax for the DECIMAL data type is as follows:

DECIMAL(precision, scale)

Where precision specifies the total number of digits and scale specifies the number of digits to the right of the decimal point. The valid range for precision is 1 to 38, and the valid range for scale is 0 to precision.

Usage

The DECIMAL data type is typically used in scenarios that require high precision calculations, such as:

  • Financial calculations: In financial reports, it is necessary to accurately calculate various income, expenditure, and tax values, as well as post-tax profits and other indicators.
  • Currency calculations: Precise exchange rates are required when converting currencies.
  • Scientific calculations: High-precision numerical representations are required for certain scientific calculations, such as calculating pi.

Example

Here is an example of using the DECIMAL data type to store exam scores for students:

CREATE TABLE ExamScores (
    StudentID INT PRIMARY KEY,
    Score DECIMAL(4, 2) NOT NULL
);

INSERT INTO ExamScores (StudentID, Score)
VALUES (1, 78.50),
       (2, 93.75),
       (3, 87.00);

SELECT * FROM ExamScores;

In the above example, we created a table named ExamScores that contains two fields, StudentID and Score. The Score field uses the DECIMAL data type with a total of 4 digits and 2 decimal places. We inserted three records, each containing a student ID and an exam score. Finally, we used the SELECT statement to query the entire table.

Here is another example of using the DECIMAL data type to store revenue for a company:

CREATE TABLE Sales (
    Month INT NOT NULL,
    Year INT NOT NULL,
    Revenue DECIMAL(18, 2) NOT NULL
);

INSERT INTO Sales (Month, Year, Revenue)
VALUES (1, 2022, 124567.89),
       (2, 2022, 165432.10),
       (3, 2022, 198765.43);

SELECT * FROM Sales;

In this example, we created a table named Sales that contains three fields, Month, Year, and Revenue. The Revenue field uses the DECIMAL data type with a total of 18 digits and 2 decimal places. We inserted three records, each containing a month, a year, and the revenue for that month.

Conclusion

The DECIMAL data type is used in SQL Server to represent numbers with decimals accurately. It allows us to specify the total number of digits and the number of decimal places to meet different requirements. DECIMAL is typically used in scenarios that require precise calculations, such as currency calculations and financial analysis. When inserting and updating data, it is important to carefully consider the correct total number of digits and decimal places to ensure data accuracy and consistency.