Precision, scale, and length (Transact-SQL) - SQL Server (2024)

  • Article

Applies to: Precision, scale, and length (Transact-SQL) - SQL Server (1) SQL Server Precision, scale, and length (Transact-SQL) - SQL Server (2) Azure SQL Database Precision, scale, and length (Transact-SQL) - SQL Server (3) Azure SQL Managed Instance Precision, scale, and length (Transact-SQL) - SQL Server (4) Azure Synapse Analytics Precision, scale, and length (Transact-SQL) - SQL Server (5) Analytics Platform System (PDW) Precision, scale, and length (Transact-SQL) - SQL Server (6) SQL analytics endpoint in Microsoft Fabric Precision, scale, and length (Transact-SQL) - SQL Server (7) Warehouse in Microsoft Fabric

Precision is the number of digits in a number. 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.

Length for a numeric data type is the number of bytes that are used to store the number. For varchar and char, the length of a character string is the number of bytes. For nvarchar and nchar, the length of the character string is the number of byte-pairs. The length for binary, varbinary, and image data types is the number of bytes. For example, an int data type can hold 10 digits, is stored in 4 bytes, and doesn't accept decimal points. The int data type has a precision of 10, a length of 4, and a scale of 0.

  • When you concatenate two char, varchar, binary, or varbinary expressions, the length of the resulting expression is the sum of the lengths of the two source expressions, up to 8,000 bytes.

  • When you concatenate two nchar or nvarchar expressions, the length of the resulting expression is the sum of the lengths of the two source expressions, up to 4,000 byte-pairs.

  • When you compare two expressions of the same data type but different lengths by using UNION, EXCEPT, or INTERSECT, the resulting length is the longer of the two expressions.

Remarks

The precision and scale of the numeric data types besides decimal are fixed. When an arithmetic operator has two expressions of the same type, the result has the same data type with the precision and scale defined for that type. If an operator has two expressions with different numeric data types, the rules of data type precedence define the data type of the result. The result has the precision and scale defined for its data type.

The following table defines how the precision and scale of the result are calculated when the result of an operation is of type decimal. The result is decimal when either:

  • Both expressions are decimal.
  • One expression is decimal and the other is a data type with a lower precedence than decimal.

The operand expressions are denoted as expression e1, with precision p1 and scale s1, and expression e2, with precision p2 and scale s2. The precision and scale for any expression that isn't decimal is the precision and scale defined for the data type of the expression. The function max(a, b) indicates to take the greater value of a or b. Similarly, min(a, b) indicates to take the smaller value of a or b.

OperationResult precisionResult scale 1
e1 + e2max(s1, s2) + max(p1 - s1, p2 - s2) + 1max(s1, s2)
e1 - e2max(s1, s2) + max(p1 - s1, p2 - s2) + 1max(s1, s2)
e1 * e2p1 + p2 + 1s1 + s2
e1 / e2p1 - s1 + s2 + max(6, s1 + p2 + 1)max(6, s1 + p2 + 1)
e1 { UNION | EXCEPT | INTERSECT } e2max(s1, s2) + max(p1 - s1, p2 - s2)max(s1, s2)
e1 % e2min(p1 - s1, p2 - s2) + max(s1, s2)max(s1, s2)

1 The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, it's reduced to 38, and the corresponding scale is reduced to try to prevent truncating the integral part of a result. In some cases such as multiplication or division, scale factor isn't reduced, to maintain decimal precision, although the overflow error can be raised.

In addition and subtraction operations, we need max(p1 - s1, p2 - s2) places to store the integral part of the decimal number. If there isn't enough space to store them (that is, max(p1 - s1, p2 - s2) < min(38, precision) - scale), the scale is reduced to provide enough space for the integral part. The resulting scale is min(precision, 38) - max(p1 - s1, p2 - s2), so the fractional part might be rounded to fit into the resulting scale.

In multiplication and division operations, we need precision - scale places to store the integral part of the result. The scale might be reduced using the following rules:

  1. The resulting scale is reduced to min(scale, 38 - (precision-scale)) if the integral part is less than 32, because it can't be greater than 38 - (precision-scale). The result might be rounded in this case.
  2. The scale isn't changed if it's less than 6 and if the integral part is greater than 32. In this case, an overflow error might be raised if it can't fit into decimal(38, scale).
  3. The scale is set to 6 if it's greater than 6 and if the integral part is greater than 32. In this case, both the integral part and scale would be reduced and resulting type is decimal(38, 6). The result might be rounded to 6 decimal places, or the overflow error is thrown if the integral part can't fit into 32 digits.

Examples

The following expression returns result 0.00000090000000000 without rounding, because the result can fit into decimal(38, 17):

SELECT CAST(0.0000009000 AS DECIMAL(30, 20)) * CAST(1.0000000000 AS DECIMAL(30, 20)) [decimal(38, 17)];

In this case precision is 61, and scale is 40.

The integral part (precision-scale = 21) is less than 32, so this case is the first case in the multiplication rules, and scale is calculated as min(scale, 38 - (precision-scale)) = min(40, 38 - (61-40)) = 17. Result type is decimal(38, 17).

The following expression returns result 0.000001 to fit into decimal(38, 6):

SELECT CAST(0.0000009000 AS DECIMAL(30, 10)) * CAST(1.0000000000 AS DECIMAL(30, 10)) [decimal(38, 6)];

In this case precision is 61, and scale is 20.

Scale is greater than 6 and the integral part (precision-scale = 41) is greater than 32. This case is the third case in the multiplication rules, and the result type is decimal(38, 6).

See also

  • Expressions (Transact-SQL)
  • Data Types (Transact-SQL)
Precision, scale, and length (Transact-SQL) - SQL Server (2024)

FAQs

What is the maximum length and precision in SQL Server? ›

In SQL Server, the default maximum precision of numeric and decimal data types is 38. Length for a numeric data type is the number of bytes that are used to store the number. For varchar and char, the length of a character string is the number of bytes.

How to set precision and scale in SQL? ›

Bind and Substitution Variables

You should set the scale equal to the number of decimal places that you believe your variable is accurate to, and the precision equal to the scale plus the maximum number of whole number digits your variable could contain.

What is the precision scale of data length? ›

The precision is the maximum number of digits or characters that are displayed for the data in that column. For nonnumeric data, the precision typically refers to the defined length of the column. The scale refers to the maximum number of digits that are displayed to the right of the decimal point.

What is the precision of decimal scale INT SQL? ›

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. The default precision is 18.

What is maximum precision SQL? ›

The maximum precision is 38. Without any decimal storage specification, the precision defaults to 18 and the scale defaults to zero: DECIMAL in SQL SERVER = DECIMAL(18,0) in Informix.

What is the difference between precision and length? ›

Precision is used for decimal. And length is the character length. Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000.

How to get precision in SQL Server? ›

The Basic syntax of Decimal data type in SQL Server

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.

How do you use the set precision function? ›

By using the setprecision function, we can get the desired precise value of a floating-point or a double value by providing the exact number of decimal places. If an argument n is passed to the setprecision() function, then it will give n significant digits of the number without losing any information.

How to get data type length in SQL Server? ›

SQL Server DATALENGTH() Function

The DATALENGTH() function returns the number of bytes used to represent an expression. Note: The DATALENGTH() function counts both leading and trailing spaces when calculating the length of the expression.

How do you calculate the precision of data? ›

We can assess the precision of a set of measurements by calculating the average deviation of the measurements as follows:
  1. Calculate the average value of all the measurements: ...
  2. Calculate the deviation of each measurement, which is the absolute value of the difference between each measurement and the average value:
Nov 10, 2017

What are examples of precision measurements? ›

The closeness of two or more measurements to each other is known as the precision of a substance. If you weigh a given substance five times and get 3.2 kg each time, then your measurement is very precise but not necessarily accurate. Precision is independent of accuracy.

How do you find the precision of a scale? ›

To find the precision of a measurement, perform the measurement multiple times and then calculate the average value and the standard deviation of those measurements. The standard deviation provides a measure of the spread of the values around the average. A smaller standard deviation indicates higher precision.

What is the decimal length in SQL? ›

SQL Server Data Types

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. Default is 18.

What is datetime precision in SQL Server? ›

According to the documentation, the time precision of the DATETIME type is set to 0 at midnight and increments by 1 every 0.003333… seconds. Apart from DATETIME, some other data types, including DATETIME2, TIME, and DATE, alternatives to DATETIME, are internally represented a bit differently.

How many bytes is decimal precision in SQL Server? ›

Decimal Data Type
  • Storage size: Precision 1-9 5 Bytes. Precision 10-19 9 Bytes. Precision 20-28 13 Bytes. Precision 29-38 17 Bytes.
  • Functionally the same as numeric.
Sep 25, 2020

What is the maximum SQL length in SQL Server? ›

The maximum standard SQL query length is 1024.00K characters, including comments.

What is the maximum statement length in SQL Server? ›

The maximum standard SQL query length is 1024.00K characters, including comments and white space characters.

What is the maximum line length in SQL Server? ›

The row limit in bytes for Microsoft SQL Server is 8,060.

What is the limit length in SQL Server? ›

The maximum length of an SQL statement string is 65,000 characters. When the Microsoft Access driver is used, only SQL-92 string constants (with single quotation marks, not double quotation marks) are supported. The pipe character (|) can't be used in a string, whether the character is enclosed in back quotes or not.

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6413

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.