Understanding the SQL Decimal data type (2024)

This article aims to walk you through the SQL Decimal data type and its usage with various examples. We will also see how we can exercise this data type in SQL Server to help make SQL developer’s job easier.

Introduction

Organizations deal with decimals on a day-to-day basis, and these decimal values can be seen everywhere in different sectors, be it in banks, the medical industry, biometrics, gas stations, financial reports, sports, and whatnot. Using whole numbers (by rounding decimal numbers) definitely makes one’s job easier but it often leads to inaccurate outputs, especially when we are dealing with a large number of values and crucial data. In such scenarios, it is ideal to use Sql Decimal data type in SQL Server to deliver correct results with perfect precision.

It becomes very essential for SQL developers to choose the correct data types in the table structure while designing and modeling SQL databases. Let’s move forward and explore Decimal data type in SQL Server.

Pre-requisite

SQL Decimal data type is being used in SQL Server since forever. You can use any SQL Server version installed (starting 2000 or above) to understand this data type. We will be using SQL Server 2017 in this article for the demo purposes. If you don’t have any version installed on your system and wish to practice against the 2017 version, download it from here.

The Basic syntax of Decimal data type in SQL Server

Let’s take a look at the basic syntax of SQL Decimal Data type first. It is denoted as below:

  • decimal [(p [,s])]

Where,

  • p stands for Precision, the total number of digits in the value, i.e. on both sides of the decimal point
  • s stands for Scale, number of digits after the decimal point

The default value of p is 18 and s is 0 and for both these values, the minimum is 1 and the maximum is 38.

In short, by defining parameters in the SQL Decimal data type, we are estimating how many digits a column or a variable will have and also the number of digits to the right of the decimal point.

For instance, decimal (4,2) indicates that the number will have 2 digits before the decimal point and 2 digits after the decimal point, something like this has to be the number value- ##.##.

One important thing to note here is, – parameter s (Scale) can only be specified if p (Precision) is specified. The scale must always be less than or equal to the precision.

Defining SQL Decimal Data type

Let’s work with a very popular mathematical constant – π, aka, Pi that has a value equal to 3.14159 (22/7 in a fraction). Copy and paste the below query in a new query window and execute it.

1

2

3

4

DECLARE @PiWithNoDecimal DECIMAL(6,0) = 3.14159

DECLARE @Piupto5Decimal DECIMAL(6,5) = 3.14159

DECLARE @Piupto1Decimal DECIMAL(3,1) = 3.14159

SELECT @PiWithNoDecimal AS PiWithNoDecimal, @Piupto5Decimal AS Piupto5Decimal, @Piupto1Decimal AS Piupto1Decimal

The above result set shows how SQL Server treats each combination of precision and scale as a different data type. Like here, decimal (6, 0) behaves differently from data types decimal (6,5) and decimal (3,1) and are considered as three different types. This way we can tweak the parameters in the SQL Decimal type to achieve desired results.

Now that we know how to create this Decimal data type in SQL Server, let’s explore it with numerous examples.

Using SQL Decimal in the Tables

Let’s quickly create a new table, named Patients, that makes use of decimal data type for columns height and weight. We will insert a few rows using an INSERT clause as shown below for the demo purposes.

1

2

3

4

5

6

7

8

9

10

11

CREATE TABLE dbo.Patients

( Name varchar(10),

Gender varchar(2),

Height decimal (3,2),

Weight decimal (5,2)

)

INSERT INTO PATIENTS VALUES('John','M',6.1,80.4)

INSERT INTO PATIENTS VALUES('Bred','M',5.8,73.7)

INSERT INTO PATIENTS VALUES('Leslie','F',5.3,66.9)

INSERT INTO PATIENTS VALUES('Rebecca','F',5.7,50.2)

INSERT INTO PATIENTS VALUES('Shermas','M',6.5,190.6)

Once the data is populated in the table, we can query this data using SELECT statement as shown below. The decimal values can be seen in the height and weight attributes.

1

SELECT * FROM dbo.PATIENTS

Understanding the SQL Decimal data type (2)

Let’s figure out what happens if we try to insert values that exceed the specified precision or scale values while defining the Height and Weight columns. For this demo, we will insert 2 more rows into this table (shown below).

  1. 1

    INSERT INTO PATIENTS VALUES('Largest','M', '10.9', 88.5)

  2. 1

    INSERT INTO PATIENTS VALUES('Hulk','M', '9.9', 1000.45)

It encounters the below error saying arithmetic overflow error and the SQL Server terminated the statements.

Understanding the SQL Decimal data type (3)

Let’s get to the root of this issue:

  • Height Decimal (3, 2) means the value can have 3 digits overall and 2 digits to the right of the decimal point. In the first line of code above, the value 10.9 (considered as 10.90 = 4 digits overall) exceeds the specified range (3, 2) and causes the overflow
  • Weight Decimal (5,2) means the total number of digits cannot exceed 5 and 2 digits can be placed to the right of the decimal. However, the value 1000.45 in the second line of code above exceeds the specified range of (5, 2) since it means 6 digits in total and throws an overflow error
  • Quick note – In the above error message, if you have noticed, “data type numeric” is stated instead of data type decimal, the reason is that the Decimal and the Numeric data type are exactly the same, both are fixed-precision data types and can be used interchangeably

Resolving the error

One of the easiest workarounds is to increase the precision level of the columns to store bigger numbers. We can alter the data type of the columns without dropping the table or column with the below code.

1

2

ALTER TABLE dbo.Patients ALTER COLUMN Height decimal(4,2)

ALTER TABLE dbo.Patients ALTER COLUMN Weight decimal (6,2)

Once altered, execute the Insert queries to insert these rows into the table.

Understanding the SQL Decimal data type (4)

We can see the rows being added to the table.

Understanding the SQL Decimal data type (5)

Storage considerations with Decimal Data Type in SQL Server

Data type SQL Decimal requires the following storage bytes for the specified precision as provided by Microsoft below:

Precision

Storage (Bytes)

1 – 9

5

10 – 19

9

20 – 28

13

29 – 38

17

The space consumption of SQL Decimal data type is based on the column definition and not on the size of the value being assigned to it. For e.g. Decimal (12, 4) with value of 888.888 takes 9 bytes on disk and Decimal (22, 2) value of 9999.99 consumes 13 bytes on disk. This is why this data type falls under fixed-length columns.

As a SQL developer myself, I always try to use SQL Decimal data type as decimal (9, 2) which consumes the least storage, 5 bytes on disk and offers better performance.

Conclusion

I hope this article provides a comprehensible approach on how to use SQL Decimal data type. Always ensure the precision of the decimal or numeric variable specified is enough to accommodate the values assigned to it. Additionally, we observed, how selecting the right kind of data type helps SQL developers to save disk storage.

In case of any questions, please feel free to ask in the comments section below.

To continue your journey with SQL Server and data types used in it, I would recommend going through the below links.

  • Spatial SQL data types in SQL Server
  • SQL Server Data Type Conversion Methods and performance comparison
  • Understanding the GUID data type in SQL Server
  • A step-by-step walkthrough of SQL Inner Join
  • Author
  • Recent Posts

Gauri Mahajan

Gauri is a SQL Server Professional and has 6+ years experience of working with global multinational consulting and technology organizations. She is very passionate about working on SQL Server topics like Azure SQL Database, SQL Server Reporting Services, R, Python, Power BI, Database engine, etc. She has years of experience in technical documentation and is fond of technology authoring.

She has a deep experience in designing data and analytics solutions and ensuring its stability, reliability, and performance. She is also certified in SQL Server and have passed certifications like 70-463: Implementing Data Warehouses with Microsoft SQL Server.

View all posts by Gauri Mahajan

Latest posts by Gauri Mahajan (see all)

  • Oracle Substring function overview with examples - June 19, 2024
  • Introduction to the SQL Standard Deviation function - April 21, 2023
  • A quick overview of MySQL foreign key with examples - February 7, 2023

Related posts:

  1. Top SQL Server Books
  2. Term Extraction Transformation in SSIS
  3. Temporal tables – SQL Server
  4. SQL Server SET Options that Affect the Query Result – SET CONCAT_NULL_YIELDS_NULL, SET NUMERIC_ROUNDABORT, SET QUOTED_IDENTIFIER, SET NOCOUNT, SET XACT_ABORT
  5. Troubleshooting the CXPACKET wait type in SQL Server
Understanding the SQL Decimal data type (2024)

FAQs

Understanding the SQL Decimal data type? ›

At its core, the SQL decimal data type is designed to store exact numeric values, where the precision and scale can be specified by the user. Precision refers to the total number of digits in a number, while scale refers to the number of digits to the right of the decimal point.

What is the decimal data type in SQL? ›

DECIMAL provides an exact numeric in which the precision and scale can be arbitrarily sized. You can specify the precision (the total number of digits, both to the left and the right of the decimal point) and the scale (the number of digits of the fractional component).

What does decimal 10^2 mean in SQL? ›

DECIMAL(10,2) means you can have a decimal number with a maximal total precision of 10 digits. Two (2) of them after the decimal point and eight (8) before. It is not so important why this is used. It is important to know how it works.

What is decimal 15 2 in SQL? ›

In SQL Server, DECIMAL(15,2) means “store 15 digits total, and allow up to 2 of them to be to the right of the decimal point.” You could use this to store dollar amounts in the trillions. A number like 1,234,567,890,123.45 will fit in such a column or variable.

What is decimal 3 2 in SQL? ›

Height Decimal (3, 2) means the value can have 3 digits overall and 2 digits to the right of the decimal point. In the first line of code above, the value 10.9 (considered as 10.90 = 4 digits overall) exceeds the specified range (3, 2) and causes the overflow.

What is the difference between float and decimal data types in SQL? ›

DECIMAL: a fixed-precision data type that stores exact values. NUMERIC: an alias for DECIMAL, the two are the same thing in MySQL. FLOAT: a floating-point data type that stores approximate values. DOUBLE: a floating-point data type that stores larger and more precise values than FLOAT.

What is the data type of a decimal? ›

A decimal data type could be implemented as either a floating-point number or as a fixed-point number. In the fixed-point case, the denominator would be set to a fixed power of ten. In the floating-point case, a variable exponent would represent the power of ten to which the mantissa of the number is multiplied.

How do you handle decimals in SQL? ›

In standard SQL, the syntax DECIMAL( M ) is equivalent to DECIMAL( M ,0) . Similarly, the syntax DECIMAL is equivalent to DECIMAL( M ,0) , where the implementation is permitted to decide the value of M . MySQL supports both of these variant forms of DECIMAL syntax. The default value of M is 10.

What is decimal 18 2 in SQL? ›

1. @HahaTTpro The 18 in DECIMAL(18,2) means you can have a decimal number with a maximal total precision of 18 digits.

What is decimal 8 2 in SQL? ›

For example, DECIMAL(8,2) can hold the value 123456.78 (8 (p) = 6 digits on the left + 2 (s) digits of the right of the decimal point). When using DECIMAL(p) with a precision but no scale, you define a floating-point number with p significant digits. For example, DECIMAL(8) can store 12345678, as well as 0.12345678.

What is the scale of decimals in SQL? ›

Scale is the number of digits to the right of the decimal point in a number. For example, the number 123.45 has a precision of 5 and a scale of 2 . In SQL Server, the default maximum precision of numeric and decimal data types is 38.

What is the precision of decimals in SQL? ›

p (precision)

The maximum total number of decimal digits to be stored. This number includes both the left and the right sides of the decimal point. The precision must be a value from 1 through the maximum precision of 38.

How to get 4 decimal places in SQL? ›

SQL Server ROUND() Function

The ROUND() function rounds a number to a specified number of decimal places. Tip: Also look at the FLOOR() and CEILING() functions.

How to get 2 decimal places in SQL? ›

The solution is to use the ROUND() function. This function takes two arguments: the number you want to round and the number of decimal places you want to round to. For example, if you wanted to round the number 3.14159 to two decimal places, you would use the following query: SELECT ROUND(3.14159, 2);

What is decimal 6 2 in SQL? ›

Assuming you mean that the database type is decimal(6, 2), then this means that your column is set up to store 6 places (scale), with 2 to the right of the decimal (precision). You should treat this as a decimal CLR type. A sample would be 1234.56 .

What is decimal type in SQL? ›

decimal(p,s) Fixed precision and scale numbers. Allows numbers from -10^38 +1 to 10^38 –1. The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1 to 38.

What is the decimal symbol in SQL? ›

Decimal points in SQL statements are represented with either periods or commas. Two values control the representation: The value of field DECIMAL POINT IS on installation panel DSNTIPF, which can be a comma (,) or period (.) COMMA or PERIOD, which are mutually exclusive SQL processing options for COBOL.

How to get decimals in SQL? ›

The CAST() function converts one or both operands into decimal or floating-point data types (aka not whole number integers). By casting the operands, you explicitly instruct SQL to consider the division with a different data type, resulting in the desired output.

What is the data type for numbers in SQL? ›

SQL numeric data types are used to store numeric values in a database. The most commonly used numeric data types are INT and FLOAT . The INT data type is used to store whole numbers, while the FLOAT data type is used to store decimal numbers. The INT data type can store values ranging from -2^31 to 2^31-1.

What is the decimal data type for price in SQL? ›

The best type for price column should be DECIMAL. The type DECIMAL stores the value precisely. For Example - DECIMAL(10,2) can be used to store price value. It means the total digit will be 10 and two digits will be after decimal point.

Top Articles
Top Forex brokers in Italy: trading Forex, commodities, CFDs and Cryptocurrencies - TopForex.Trade
The ultimate beginner guide to Crypto: how to trade digital assets - TopForex.Trade
What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
Skigebiet Portillo - Skiurlaub - Skifahren - Testberichte
Craigslist Myrtle Beach Motorcycles For Sale By Owner
Jordanbush Only Fans
Kreme Delite Menu
122242843 Routing Number BANK OF THE WEST CA - Wise
Pga Scores Cbs
Our History | Lilly Grove Missionary Baptist Church - Houston, TX
Savage X Fenty Wiki
Helloid Worthington Login
A Guide to Common New England Home Styles
Best Suv In 2010
Steamy Afternoon With Handsome Fernando
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Blackwolf Run Pro Shop
Inside the life of 17-year-old Charli D'Amelio, the most popular TikTok star in the world who now has her own TV show and clothing line
Costco Gas Foster City
Milspec Mojo Bio
Rugged Gentleman Barber Shop Martinsburg Wv
Webcentral Cuny
Evil Dead Rise Showtimes Near Regal Sawgrass & Imax
Azur Lane High Efficiency Combat Logistics Plan
Scheuren maar: Ford Sierra Cosworth naar de veiling
Encyclopaedia Metallum - WikiMili, The Best Wikipedia Reader
January 8 Jesus Calling
Usa Massage Reviews
Craigslist Northern Minnesota
Yayo - RimWorld Wiki
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
91 Octane Gas Prices Near Me
Learn4Good Job Posting
Babbychula
Devotion Showtimes Near The Grand 16 - Pier Park
Merkantilismus – Staatslexikon
Craigslist Putnam Valley Ny
More News, Rumors and Opinions Tuesday PM 7-9-2024 — Dinar Recaps
Great Clips Virginia Center Commons
Blow Dry Bar Boynton Beach
Craigslist St Helens
Online College Scholarships | Strayer University
Rheumatoid Arthritis Statpearls
Mail2World Sign Up
French Linen krijtverf van Annie Sloan
Adams County 911 Live Incident
Land of Samurai: One Piece’s Wano Kuni Arc Explained
Anthony Weary Obituary Erie Pa
Emmi-Sellers
Dr Seuss Star Bellied Sneetches Pdf
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6456

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.