DECIMAL (Transact‑SQL)
Overview
The DECIMAL
data type stores fixed‑precision numbers. It is synonymous with NUMERIC
and behaves identically.
Syntax
DECIMAL [ (precision [ , scale ] ) ]
Both precision
and scale
are optional. If omitted, the default is DECIMAL(18,0)
.
Range
The range depends on the specified precision and scale. For DECIMAL(p,s)
:
- Maximum value:
10^p – 10^(–s)
- Minimum value:
–(10^p – 10^(–s))
Example: DECIMAL(5,2)
stores values from –999.99 to 999.99.
Storage Size
Precision | Storage (bytes) |
---|---|
1‑9 | 5 |
10‑19 | 9 |
20‑28 | 13 |
29‑38 | 17 |
Example
CREATE TABLE Sales
(
SaleID INT PRIMARY KEY,
Amount DECIMAL(10,2) NOT NULL,
TaxRate DECIMAL(5,4) NULL
);
INSERT INTO Sales (SaleID, Amount, TaxRate)
VALUES (1, 12345.67, 0.0750);
Remarks
- Use
DECIMAL
when exact numeric precision is required, such as monetary values. - Arithmetic operations preserve scale; overflow raises an error unless
SET ARITHABORT OFF
is used. - When combined with
FLOAT
orREAL
, the result is of typeFLOAT
.