Can someone explain what happens in the following SQL code?
declare @dividend numeric (38,22)declare @divisor numeric (38,22)declare @otherDivisor intset @dividend = 1set @divisor = 3set @otherDivisor = 3select cast (@ dividend / @ divisor as numeric (38.22)), @ dividend / @ otherDivisor
declare @dividend numeric (38,22)
declare @divisor numeric (38,22)
declare @otherDivisor int
set @dividend = 1
set @divisor = 3
set @otherDivisor = 3
select cast (@ dividend / @ divisor as numeric (38.22)), @ dividend / @ otherDivisor
Return result
0.33333330000000000000000 0.3333333333333333333333333
I would expect the same result for both calculations.
decimal (38.22) / decimal (38.22) ends with decimal (x, 6) after these rules
So you have 0.33333 before you go back to decimal (38.22)
@otherDivisor (38, 0) (x, 22)
.
- . , SQL , , .
select 1.00000000000000000000/3.0 select 1.0/3.0
Try the following:
select cast(@dividend as numeric(38,22)) / @divisor, @dividend / @otherDivisor
You are casting after splitting.