TSQL returning the wrong calculation results

OHPRS Admin 211 Reputation points
2024-07-29T22:30:04.77+00:00

I'm very confused. I've been working on this for hours. I have a query:

set @interest = @balance * power(1.0 + @curIrate / 365.0,@days) - @balance

select @checkDate checkDate, @interest interest, @balance balance, @curIrate curIrate, @days days

The 'select' returns:

checkDate               interest              balance          curIrate     days        

2011-12-01 00:00:00.0   16.0088               11447.50000      0.0170030    30

which calculates a clearly wrong interest amount. Yet if I run this as a stand-alone query I get:

select 1147.50 * power(1.0 + 0.0170030 / 365.0, 30) - 1147.50

1.6047228

which is correct. Why is the TSQL query calculating 16.0088 and not 1.6047228?

Declares:

@interest money

@balance decimal(14,5)

@iRate decimal(10,7)

@days integer

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,603 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. OHPRS Admin 211 Reputation points
    2024-07-29T22:56:28.5366667+00:00

    Never mind! My bad. I had the wrong balance. Not SQL's fault.

    0 comments No comments