Open In App

SQL Server DECIMAL Data Type

Last Updated : 12 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server, when you need numbers to be super precise, like in financial calculations, you use the DECIMAL data type. It’s like a special box for storing exact numbers and no rounding off!

In simpler terms, decimal is SQL Server’s way of saying, “I don’t mess around with numbers.” It’s the go-to choice when you need to count every penny in your financial database or measure the exact ingredients for your best recipe.

Prerequisites

Before understanding the DECIMAL datatype in SQL Server, it’s beneficial to have a basic understanding of the following prerequisites:

1. Basic SQL Knowledge:

  • Familiarize yourself with fundamental SQL Server concepts such as SELECT statements, INSERT, UPDATE, and DELETE Operations.
  • Understand how to create and manage database tables.

2. Data Types:

3. Database Basics:

Main Concept

Alright, let’s break down the main concept of the DECIMAL datatype in a super simple way!

Imagine you’re dealing with money. You want to be exact because every cent matters. Now, think of the DECIMAL datatype as a special money box. This money box has two parts:

  • Total Digits Box: This part is for all the digits your money can have, like dollars and cents combined.
  • After-the-Dot Box: This part is for how many cents you want to keep after the dot.

Example: Suppose we have a DECIMAL(5, 2) money box:

We can have a total of 5 digits (dollars and cents combined).

Out of those, 2 can be cents after the dot.

Let’s say we put $123.45 in this money box. It fits perfectly because we have 3 digits for dollars (123) and 2 for cents (.45).

DecData

Explanation of Decimal Datatype

But if you try to put $1234.567, it won’t fit! Why? Because you only have 5 digits in total, and you can only keep 2 digits after the dot. So, you might need a bigger money box, like DECIMAL(7, 2).

In simple terms, the DECIMAL datatype helps you keep your numbers exact, especially when you’re counting every penny. It’s like having a special money box that won’t let you lose anything!

Syntax:

It is declared using the “DECIMAL” Keyword only. Here is how it is declared.

DECIMAL(P, S)




  • P: P stands for Precision which means Total number of digits.
  • S: S stands for Scale which means the Number of digits to the right of the decimal point.

Example

Let’s imagine you’re building a database to keep track of transactions. Each transaction has an amount of money associated with it, and you want to make sure not a single cent gets lost in the process.

Creating Table

Let’s create a table called Transactions which includes TransactionID and Amount as columns.

CREATE TABLE Transactions
(
TransactionID INT PRIMARY KEY,
Amount DECIMAL(8, 3) -- Total 8 digits, 3 digits after the dot
);





Inserting Data

Let’s insert some data into the Transactions table.

INSERT INTO Transactions (TransactionID, Amount) VALUES (1, 123.451);
INSERT INTO Transactions (TransactionID, Amount) VALUES (2, 5678.9025);

SELECT * from Transactions




NOTE: 2nd Data that is entered has a scale part(digits after the dot) greater than the set limit lets see what happens if we enter the data more than the set limit in the case of decimal datatype

Output:

InsertDataTransaction

After Inserting Data

Explanation:

First of all the Data of the table Transaction is displayed. In the 2nd Row data is rounded off to the next integer to accommodate in the scale range.

IMPORTANT NOTE: If the data inserted entered has more scale(digits after the dot) than the set limit of the scale the n the data is automatically rounded off to the next integer.

Conclusion

In a nutshell, the DECIMAL datatype in SQL Server is your go-to tool when you need exact numbers, especially in financial contexts. It’s like having a special vault for your digits, ensuring that your calculations are as accurate as possible.

Remember, with DECIMAL, you’re telling SQL Server, “I want my numbers exact, no rounding off!” So, whether you’re dealing with dollars and cents or any other scenario where precision is crucial, embrace the power of the DECIMAL datatype for precise and accurate data storage in SQL Server.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads