Implicit conversion between decimals with different precisionsWhy does SQL Server require the datatype length to be the same when using UNPIVOT?Changes in Oracle Database implicit conversion rules for select statementUser defined data types causing implicit conversionsImplicit conversion warning despite persisted computed column vs. performance hit from UDF in persisted computed column definitionHow to avoid implicit conversion for an Integer columnIs the idea of 'pages' and 'extents' unique to SQL Server or shared by other database management systems?How SQL Server knows when to implicitly convert the valuesImplicit conversion causing error part of the timeNo Implicit Conversion Between UNIQUEIDENTIFIER and VARCHAR After Removing a Persisted Computed ColumnUsed a CAST function ,still IMPLICIT Conversion warning in Execution PlanImplicit Conversion of VARCHAR Column to NVARCHAR does not cause expected table scan
Is there a standard definition of the "stall" phenomena?
As a supervisor, what feedback would you expect from a PhD who quits?
How to get the speed of my spaceship?
What is the fundamental difference between catching whales and hunting other animals?
How to play a D major chord lower than the open E major chord on guitar?
How predictable is $RANDOM really?
Examples of fluid (including air) being used to transmit digital data?
I'm feeling like my character doesn't fit the campaign
How important is it for multiple POVs to run chronologically?
is this a question or an affirmation?
Does the sensor of a dslr count the number of photons that hits it?
Multi-user CRUD: Valid, Problem, or Error?
My professor has told me he will be the corresponding author. Will it hurt my future career?
Why do we need a bootloader separate from our application program in microcontrollers?
Can a USB hub be used to access a drive from two devices?
What is the shape of the upper boundary of water hitting a screen?
Why does mean tend be more stable in different samples than median?
How to factor a fourth degree polynomial
Wouldn't putting an electronic key inside a small Faraday cage render it completely useless?
How to delete multiple process id of a single process?
Did Stalin kill all Soviet officers involved in the Winter War?
Attach a visible light telescope to the outside of the ISS
How do I check that users don't write down their passwords?
How did the IEC decide to create kibibytes?
Implicit conversion between decimals with different precisions
Why does SQL Server require the datatype length to be the same when using UNPIVOT?Changes in Oracle Database implicit conversion rules for select statementUser defined data types causing implicit conversionsImplicit conversion warning despite persisted computed column vs. performance hit from UDF in persisted computed column definitionHow to avoid implicit conversion for an Integer columnIs the idea of 'pages' and 'extents' unique to SQL Server or shared by other database management systems?How SQL Server knows when to implicitly convert the valuesImplicit conversion causing error part of the timeNo Implicit Conversion Between UNIQUEIDENTIFIER and VARCHAR After Removing a Persisted Computed ColumnUsed a CAST function ,still IMPLICIT Conversion warning in Execution PlanImplicit Conversion of VARCHAR Column to NVARCHAR does not cause expected table scan
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I know that when SQL Server handles an implicit conversion between types it converts the lowest priority one to the highest one.
But what is the result datatype when I perform an operation between decimals with different precisions?
sql-server type-conversion
add a comment |
I know that when SQL Server handles an implicit conversion between types it converts the lowest priority one to the highest one.
But what is the result datatype when I perform an operation between decimals with different precisions?
sql-server type-conversion
add a comment |
I know that when SQL Server handles an implicit conversion between types it converts the lowest priority one to the highest one.
But what is the result datatype when I perform an operation between decimals with different precisions?
sql-server type-conversion
I know that when SQL Server handles an implicit conversion between types it converts the lowest priority one to the highest one.
But what is the result datatype when I perform an operation between decimals with different precisions?
sql-server type-conversion
sql-server type-conversion
edited 8 hours ago
Paul White♦
57.1k15 gold badges301 silver badges474 bronze badges
57.1k15 gold badges301 silver badges474 bronze badges
asked 8 hours ago
s.demuros.demuro
864 bronze badges
864 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The type of the result is determined by the rules set out in Precision, scale, and Length (Transact-SQL):
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 is not decimal is the precision and scale defined for the data type of the expression. The function max(a,b) means the following: take the greater value of "a" or "b". Similarly, min(a,b) indicates to take the smaller value of "a" or "b".
* 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 won't be 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 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 integral part. Resulting scale isMIN(precision, 38) - max(p1 - s1, p2 - s2)
, so the fractional part might be rounded to fit into the resulting scale.
A convenient quick way to see the resulting type is to use SQL_VARIANT_PROPERTY
:
DECLARE @V1 decimal (9, 6) = 123.456789;
DECLARE @V2 decimal (7, 2) = 12345.67;
SELECT
[BaseType] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'BaseType'),
[Precision] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Precision'),
[Scale] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Scale');
Output:
╔══════════╦═══════════╦═══════╗
║ BaseType ║ Precision ║ Scale ║
╠══════════╬═══════════╬═══════╣
║ decimal ║ 12 ║ 6 ║
╚══════════╩═══════════╩═══════╝
db<>fiddle demo
add a comment |
The question is a bit vague but I believe the below is what you're after
It works the same way it does in mathematics:
DECLARE @a DECIMAL (5,2) = 100.02
DECLARE @b DECIMAL (8,3) = 10125.020
SELECT @a
SELECT @b
SELECT @a + @b
will add the number
one hundred point zero two
to
ten thousand, one hundred and twenty five point zero two zero
which will result in
ten thousand, two hundred and twenty five point zero four zero
When running the code above, SQL Server outputs 10225.040
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f242097%2fimplicit-conversion-between-decimals-with-different-precisions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The type of the result is determined by the rules set out in Precision, scale, and Length (Transact-SQL):
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 is not decimal is the precision and scale defined for the data type of the expression. The function max(a,b) means the following: take the greater value of "a" or "b". Similarly, min(a,b) indicates to take the smaller value of "a" or "b".
* 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 won't be 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 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 integral part. Resulting scale isMIN(precision, 38) - max(p1 - s1, p2 - s2)
, so the fractional part might be rounded to fit into the resulting scale.
A convenient quick way to see the resulting type is to use SQL_VARIANT_PROPERTY
:
DECLARE @V1 decimal (9, 6) = 123.456789;
DECLARE @V2 decimal (7, 2) = 12345.67;
SELECT
[BaseType] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'BaseType'),
[Precision] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Precision'),
[Scale] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Scale');
Output:
╔══════════╦═══════════╦═══════╗
║ BaseType ║ Precision ║ Scale ║
╠══════════╬═══════════╬═══════╣
║ decimal ║ 12 ║ 6 ║
╚══════════╩═══════════╩═══════╝
db<>fiddle demo
add a comment |
The type of the result is determined by the rules set out in Precision, scale, and Length (Transact-SQL):
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 is not decimal is the precision and scale defined for the data type of the expression. The function max(a,b) means the following: take the greater value of "a" or "b". Similarly, min(a,b) indicates to take the smaller value of "a" or "b".
* 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 won't be 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 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 integral part. Resulting scale isMIN(precision, 38) - max(p1 - s1, p2 - s2)
, so the fractional part might be rounded to fit into the resulting scale.
A convenient quick way to see the resulting type is to use SQL_VARIANT_PROPERTY
:
DECLARE @V1 decimal (9, 6) = 123.456789;
DECLARE @V2 decimal (7, 2) = 12345.67;
SELECT
[BaseType] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'BaseType'),
[Precision] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Precision'),
[Scale] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Scale');
Output:
╔══════════╦═══════════╦═══════╗
║ BaseType ║ Precision ║ Scale ║
╠══════════╬═══════════╬═══════╣
║ decimal ║ 12 ║ 6 ║
╚══════════╩═══════════╩═══════╝
db<>fiddle demo
add a comment |
The type of the result is determined by the rules set out in Precision, scale, and Length (Transact-SQL):
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 is not decimal is the precision and scale defined for the data type of the expression. The function max(a,b) means the following: take the greater value of "a" or "b". Similarly, min(a,b) indicates to take the smaller value of "a" or "b".
* 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 won't be 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 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 integral part. Resulting scale isMIN(precision, 38) - max(p1 - s1, p2 - s2)
, so the fractional part might be rounded to fit into the resulting scale.
A convenient quick way to see the resulting type is to use SQL_VARIANT_PROPERTY
:
DECLARE @V1 decimal (9, 6) = 123.456789;
DECLARE @V2 decimal (7, 2) = 12345.67;
SELECT
[BaseType] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'BaseType'),
[Precision] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Precision'),
[Scale] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Scale');
Output:
╔══════════╦═══════════╦═══════╗
║ BaseType ║ Precision ║ Scale ║
╠══════════╬═══════════╬═══════╣
║ decimal ║ 12 ║ 6 ║
╚══════════╩═══════════╩═══════╝
db<>fiddle demo
The type of the result is determined by the rules set out in Precision, scale, and Length (Transact-SQL):
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 is not decimal is the precision and scale defined for the data type of the expression. The function max(a,b) means the following: take the greater value of "a" or "b". Similarly, min(a,b) indicates to take the smaller value of "a" or "b".
* 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 won't be 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 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 integral part. Resulting scale isMIN(precision, 38) - max(p1 - s1, p2 - s2)
, so the fractional part might be rounded to fit into the resulting scale.
A convenient quick way to see the resulting type is to use SQL_VARIANT_PROPERTY
:
DECLARE @V1 decimal (9, 6) = 123.456789;
DECLARE @V2 decimal (7, 2) = 12345.67;
SELECT
[BaseType] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'BaseType'),
[Precision] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Precision'),
[Scale] = SQL_VARIANT_PROPERTY(@V1 + @V2, 'Scale');
Output:
╔══════════╦═══════════╦═══════╗
║ BaseType ║ Precision ║ Scale ║
╠══════════╬═══════════╬═══════╣
║ decimal ║ 12 ║ 6 ║
╚══════════╩═══════════╩═══════╝
db<>fiddle demo
edited 6 hours ago
answered 8 hours ago
Paul White♦Paul White
57.1k15 gold badges301 silver badges474 bronze badges
57.1k15 gold badges301 silver badges474 bronze badges
add a comment |
add a comment |
The question is a bit vague but I believe the below is what you're after
It works the same way it does in mathematics:
DECLARE @a DECIMAL (5,2) = 100.02
DECLARE @b DECIMAL (8,3) = 10125.020
SELECT @a
SELECT @b
SELECT @a + @b
will add the number
one hundred point zero two
to
ten thousand, one hundred and twenty five point zero two zero
which will result in
ten thousand, two hundred and twenty five point zero four zero
When running the code above, SQL Server outputs 10225.040
add a comment |
The question is a bit vague but I believe the below is what you're after
It works the same way it does in mathematics:
DECLARE @a DECIMAL (5,2) = 100.02
DECLARE @b DECIMAL (8,3) = 10125.020
SELECT @a
SELECT @b
SELECT @a + @b
will add the number
one hundred point zero two
to
ten thousand, one hundred and twenty five point zero two zero
which will result in
ten thousand, two hundred and twenty five point zero four zero
When running the code above, SQL Server outputs 10225.040
add a comment |
The question is a bit vague but I believe the below is what you're after
It works the same way it does in mathematics:
DECLARE @a DECIMAL (5,2) = 100.02
DECLARE @b DECIMAL (8,3) = 10125.020
SELECT @a
SELECT @b
SELECT @a + @b
will add the number
one hundred point zero two
to
ten thousand, one hundred and twenty five point zero two zero
which will result in
ten thousand, two hundred and twenty five point zero four zero
When running the code above, SQL Server outputs 10225.040
The question is a bit vague but I believe the below is what you're after
It works the same way it does in mathematics:
DECLARE @a DECIMAL (5,2) = 100.02
DECLARE @b DECIMAL (8,3) = 10125.020
SELECT @a
SELECT @b
SELECT @a + @b
will add the number
one hundred point zero two
to
ten thousand, one hundred and twenty five point zero two zero
which will result in
ten thousand, two hundred and twenty five point zero four zero
When running the code above, SQL Server outputs 10225.040
answered 8 hours ago
SEarle1986SEarle1986
6784 silver badges19 bronze badges
6784 silver badges19 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f242097%2fimplicit-conversion-between-decimals-with-different-precisions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown