Decimal Data Type
The Decimal data type is exactly similar to the numeric data type. Both have fixed precision exact values. Both have the same syntax and the same memory usage.
Syntax:
DECIMAL(precision, scale)
Decimal Data Type Example
-- Create the table
CREATE TABLE STUDENT(
Name VARCHAR(20),
Result DECIMAL(5, 2)
);
-- Insert sample data
INSERT INTO STUDENT(Name, Result) VALUES ('John', 98.75);
INSERT INTO STUDENT(Name, Result) VALUES ('Alice', 87.50);
Output:
Explanation: The SQL script creates a table named “STUDENT” with columns “Name” and “Result” of data type DECIMAL(5,2), enforcing fixed precision numbers with 5 total digits and 2 decimal places. Sample data is inserted for students “John” and “Alice” with their respective results, adhering to the specified precision.
What is the Difference Between Numeric, Float, and Decimal in SQL server?
The SQL Server has a variety of data types to store data. These data types can accompany several forms like string, integer, double, date, time, etc. Therefore selecting the appropriate data structure is crucial for maintaining the data accuracy and optimizing the system’s performance.
This article delves into the fundamentals of three crucial data types: Numeric, Float, and Decimal.