What is the Difference Between Numeric, Float, and Decimal in SQL server?
Last Updated :
26 Mar, 2024
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.
Numeric Data Type
The Numeric data type is used to represent a number with fixed precision and fixed scale. This means, there is a defined precision for numeric data type such that if we try to store a number that is out of bounds, the numeric data type will return an error.
Syntax:
NUMERIC(precision, scale)
Numeric Data Type Example
-- Create the table
CREATE TABLE STUDENT(
Name VARCHAR(20),
Result NUMERIC(5,2)
);
-- Insert sample data
INSERT INTO STUDENT(Name, Result) VALUES ('John', 98.75);
INSERT INTO STUDENT(Name, Result) VALUES ('Alice', 87.50);
Output:
Numeric outputExplanation: The SQL script creates a table named "STUDENT" with columns "Name" and "Result" of data type NUMERIC(5,2), allowing for fixed precision numbers with 5 total digits and 2 decimal places. Sample data is inserted with the names "John" and "Alice" along with their corresponding results, where precision is enforced.
Float Data Type
The Float data type is used to store floating point numbers that are not fixed/exact. This means that the float data type does not store the exact values, rather, it stores the approximate value of the stored number.
Syntax:
FLOAT(precision)
Float Data Type Example
-- Create the table
CREATE TABLE STUDENT(
Name VARCHAR(20),
Result FLOAT(5)
);
-- Insert sample data
INSERT INTO STUDENT(Name, Result) VALUES ('John', 98.75000);
INSERT INTO STUDENT(Name, Result) VALUES ('Alice', 87.50000);
Output:
Flatt outputExplanation: The SQL script creates a table named "STUDENT" with columns "Name" and "Result" of data type FLOAT(5). Sample data is inserted into the table with names "John" and "Alice" along with their corresponding results. The FLOAT(5) data type allows for storing approximate floating-point numbers with up to 5 digits.
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:
Decimal outputExplanation: 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.
Benefits of Using Numeric, Float, and Decimal in SQL Server
- Numeric: Offers strict precision for exact numeric values, suitable for financial calculations.
- Float: Provides efficient storage for approximate floating-point numbers, ideal for scientific computations.
- Decimal: Combines fixed precision with adjustable scale, offering accuracy with flexibility, suitable for diverse applications.
Each type has distinct advantages, catering to specific requirements in SQL data storage and manipulation.
Difference Between Numeric, Float, and Decimal in SQL server
Parameters | Numeric | Float | Decimal |
---|
Size (in Bytes) | 5, 9, 13, 17 (based on precision) | 4, 8 (based on precision) | 5, 9, 13, 17 (based on precision) |
Scale | Defined by user | Not required | Defined by user |
Precision | Fixed precision (strict precision) | Approximate precision | Fixed precision (adjustable precision) |
Range | -10^38 to 10^38 | -1.79 x 10^308 to 1.79 x 10^308 | -10^38 to 10^38 |
Exact Values | Yes | No | Yes |
Storage Efficiency | Less efficient | More efficient | Less efficient |
Conclusion
All 3 data types have their advantages. However, the numeric and decimal data types have been considered the same since the 2016 update of SQL. The only notable difference between them remains the strictness and out-of-bound error in numeric type. One of the biggest differences between float and numeric/decimal lies in the fact that float stores the approximate value whereas the decimal and numeric have fixed point precision values. Based on the parameters passed to these data types, their sizes vary accordingly.
Similar Reads
What is the difference between Real and Complex Numbers?
A number system is a way of showing numbers by writing, which is a mathematical way of representing the numbers of a given set, by using the numbers or symbols in a mathematical manner. The writing system for denoting numbers using digits or symbols in a logical manner is defined as the Number syste
6 min read
Difference Between Decimal and Float in PL/SQL
Have you ever thought about the differences between Decimal and Float in PL/SQL? These two numeric data types play important roles in database programming that offer unique features for handling numeric values. In this article, we'll explore What are Decimals and Float along with their syntax, usage
2 min read
What is the difference between rational and irrational numbers?
Answer : Rational Numbers are either finite or are recurring in nature but Irrational Numbers are non-terminating as well as non-repeating in nature.A number System is portrayed as a course of action of writing to represent the numbers. It is the numerical documentation for addressing amounts of a g
3 min read
Difference between Decimal, Float and Double in .Net
Float : It is a floating binary point type variable. Which means it represents a number in it's binary form. Float is a single precision 32 bits(6-9 significant figures) data type. It is used mostly in graphic libraries because of very high demand for processing power, and also in conditions where r
2 min read
What is the difference between counting numbers and whole numbers?
Counting Numbers: Also known as Natural Numbers.Include positive integers starting from 1.Do not include zero.Counting numbers begin with 1. (1, 2, 3, ...)Whole Numbers: Include all counting numbers plus zero.Start from 0 and include all positive integers.Whole numbers begin with 0. (0, 1, 2, ...)Th
8 min read
What is the difference between a number and a digit?
Arithmetic is a branch of mathematics in which numbers are studied and perform its various operations like addition, subtraction, multiplication, and division, etc. Arithmetic is a vast concept that consists of a major portion of Mathematics. In Arithmetic, numbers are studied and all the possible o
3 min read
Difference Between Decimal and Binary Number System
Decimal Number is defined by the number which has a whole number and the fractional part separated by a decimal point. While Binary Number is defined by a number which is expressed as in the base-2 numeral system. It is always written in the form of 0 and 1. In math, a system that is used to represe
7 min read
Difference between Cardinal, Ordinal, and Nominal Numbers
Arithmetic is an elementary part of the number theory. Arithmetic is a branch of Mathematics, which consists of the study of numbers and traditional operations such as addition, subtraction, multiplication, division, finding roots, exponential, etc. It deals with properties and manipulations of numb
3 min read
Difference between Natural and Whole numbers
A number is defined as any mathematical value that is used to measure or count some values. A number is mainly used for mathematical calculations. The number can be of any type like a whole number, natural number, integers, rational number, irrational number, real number, and complex numbers. The de
3 min read
What two numbers have a sum of 8 and their difference is 2?
Numeral Systems is a mathematical notation used for counting and calculating objects, and for executing arithmetic calculations. It is a writing system for representing numbers. It gives an exceptional depiction of every number and constitutes the arithmetic and algebraic form of the number. It allo
5 min read