SQL Decimal

Galaxy Glossary

How do you use the DECIMAL data type in SQL to store precise numbers?

The DECIMAL data type in SQL is used to store numbers with a specific precision and scale, ensuring accuracy for financial or scientific applications. It's crucial for storing values that require exact representation, like monetary amounts or measurements.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The DECIMAL data type in SQL is designed for storing numbers with a fixed precision and scale. This means you specify both the total number of digits (precision) and the number of digits after the decimal point (scale). This is in contrast to FLOAT or DOUBLE types, which can lose precision when representing decimal values. Using DECIMAL ensures that your data is stored accurately, especially when dealing with monetary values or scientific measurements. For example, storing currency values as DECIMAL prevents rounding errors that can accumulate over time. The precision and scale are crucial parameters that define the range and accuracy of the numbers you can store. A higher precision allows for more digits before and after the decimal, but it also takes up more storage space. The scale determines the maximum number of digits after the decimal point. Choosing the right precision and scale is essential for efficient storage and accurate calculations.

Why SQL Decimal is important

Using DECIMAL is crucial for financial applications and scientific computations where accuracy is paramount. It prevents rounding errors that can lead to significant inaccuracies in calculations and reporting. This data type ensures that your data is stored and manipulated with the precision you need.

Example Usage


-- Create a sample table
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderTotal DECIMAL(10, 2)
);

-- Insert some sample data
INSERT INTO Orders (OrderID, CustomerID, OrderTotal)
VALUES
(1, 101, 150.00),
(2, 102, 25.50),
(3, 101, 500.00),
(4, 103, 10.00);

-- Declare a cursor to iterate through the Orders table
DECLARE OrderCursor CURSOR FOR
SELECT OrderID, CustomerID, OrderTotal
FROM Orders
WHERE OrderTotal > 100;

-- Declare a variable to hold the OrderID
DECLARE @OrderID INT;

-- Declare a variable to hold the CustomerID
DECLARE @CustomerID INT;

-- Declare a variable to hold the OrderTotal
DECLARE @OrderTotal DECIMAL(10, 2);

-- Open the cursor
OPEN OrderCursor;

-- Fetch the first row
FETCH NEXT FROM OrderCursor INTO @OrderID, @CustomerID, @OrderTotal;

-- Loop through the rows
WHILE @@FETCH_STATUS = 0
BEGIN
    -- Check if the order total is greater than 100
    IF @OrderTotal > 200
    BEGIN
        -- Update the customer's address (replace with your actual update)
        UPDATE Customers
        SET Address = 'New Address'
        WHERE CustomerID = @CustomerID;
        PRINT 'Updated customer ' + CAST(@CustomerID AS VARCHAR(10)) + ' with new address.';
    END
    -- Fetch the next row
    FETCH NEXT FROM OrderCursor INTO @OrderID, @CustomerID, @OrderTotal;
END;

-- Close the cursor
CLOSE OrderCursor;

-- Deallocate the cursor
DEALLOCATE OrderCursor;

Common Mistakes

Want to learn about other SQL terms?