Reputation: 16981
Is there a way to check what is the resulting data type of implicit conversion? Or do I simply have to know it based on data types joining the arithmetic operation?
declare @i as int
set @i = 3
select @i / 9.0 as returning_numeric
Upvotes: 3
Views: 145
Reputation: 452967
The result of the expression is numeric (17,6)
. To see this
DECLARE @i INT, @v SQL_VARIANT
SET @i = 3
SET @v = @i / 9.0
SELECT
CAST(SQL_VARIANT_PROPERTY(@v, 'BaseType') AS VARCHAR(30)) AS BaseType,
CAST(SQL_VARIANT_PROPERTY(@v, 'Precision') AS INT) AS Precision,
CAST(SQL_VARIANT_PROPERTY(@v, 'Scale') AS INT) AS Scale
Returns
BaseType Precision Scale
---------- ----------- -----------
numeric 17 6
SELECT SQL_VARIANT_PROPERTY(9.0, 'BaseType'),
SQL_VARIANT_PROPERTY(9.0, 'Precision'),
SQL_VARIANT_PROPERTY(9.0, 'Scale')
So the literal 9.0
is treated as numeric(2,1)
(Can be seen from the query above)
@i
is numeric(10,0)
(as per Mikael's answer)
The rules that govern why numeric(10,0)
/numeric(2,1)
gives numeric (17,6)
are covered here
Operation: e1 / e2
Result precision: p1 - s1 + s2 + max(6, s1 + p2 + 1)
Result scale: max(6, s1 + p2 + 1)
Substituting the relevant values in gives
10 - 0 + 1 + max(6, 0 + 2 + 1) = 17
max(6, 0 + 2 + 1) = 6
Upvotes: 3
Reputation: 138960
The implicit conversion done here is on @i
to numeric(10,0). You can modify your statement and look at the execution plan.
declare @i int
set @i = 3
select @i / 9.0
from (select 1) as x(x)
Extract from execution plan
<ScalarOperator ScalarString="CONVERT_IMPLICIT(numeric(10,0),[@i],0)/(9.0)">
However, the resulting data type is numeric(17,6)
as Martin showed in his answer and that is not shown in the execution plan.
Upvotes: 2