Use 30.0
in your division to force non-integer arithmetic
DECLARE @Tenor Decimal(18,6)
SET @Tenor = ROUND(DATEDIFF(D,'2014-04-14','2014-05-07') / 30.0, 0)
SELECT @Tenor -- 1.000000
The behaviour you are seeing is as a result of implicit type conversion in SQL Server. Both the return type of DATEDIFF(D,'2014-04-14','2014-05-07')
and 30
are int
, so integer division will occur when dividing the two (effectively removing the fractional part of the result, i.e. 0
).
In your second attempt the dividend (@dd
) is defined as decimal
which results an implicit cast of the divisor (30
) to decimal
, giving the result you'd expect.