# using to the nth power in server 2008

how would you write this formula in sql
This is what i have
@FinalAmount = (@P * (1 + dbo.[IntRate](@N))^@N)

This is the error
Msg 402, Level 16, State 1, Procedure FinalAmount, Line 7
The data types decimal and int are incompatible in the '^' operator.

What I am trying to do is calculate the compound interest
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
DEcLARE @x INT
SET @FinalAmount = (@P * (1 + dbo.[IntRate](@N))
SET CAST(@FinalAmount AS INT) ^ @N
0
Author Commented:
that does not work
here is my full code

Function [dbo].[FinalAmount](@P int, @N int, @DepositType char(6))
RETURNS int
AS
BEGIN
DECLARE @FinalAmount int
IF @DepositType = 'saving' set @FinalAmount = (@P+(@N * @P * dbo.[IntRate](@N)))
ELSE set @FinalAmount = (@P * (1 + dbo.[IntRate](@N)^@N * @N))
RETURN @FinalAmount
END

this is the error
Msg 402, Level 16, State 1, Procedure FinalAmount, Line 7
The data types decimal and int are incompatible in the '^' operator.

The ^ is an XOR operator and we are trying to do an exponential operator

0
Commented:
crap...I knew that.  One second.
0
Commented:
The function is POWER(number, exponent)
0
Commented:
so, for your first example it would be:

@FinalAmount = POWER (@P * (1 + dbo.[IntRate](@N)), @N)
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
when i go to call it in this select statement if gives me an incorrect statement
and it give the scientific notation as answer
4.48634164428711E+18

i need it to give me the final amount of money

0
Commented:
Ok, so give me the value from this statement:  @P * (1 + dbo.[IntRate](@N))

and the @N Value, and I'll see if I can get it to work.  What value do you expect?
0
Author Commented:
p = 5000
rate = 7.5%
years = 5

final amount should equal 7178.15
0
Commented:
hmmm....what is the mathematical formula again (not in TSQL terms, just math notation)
0
Author Commented:
A = P(1+R)^N
a = total amount
p = principle
r = rate
n = years
0
Commented:
so....

5000(1.075)^5 is supposed to equate to 7178.15?
0
Author Commented:
yes
0
Commented:
5000 * 1.075 = 5375.000

5375.000 ^ 5 = 4486341644287109600.000
0
Author Commented:
that is wrong!
the first one is correct

this is to calculate compound interest
0
Author Commented:
here is the correct formula!
@p*(POWER((1+dbo.[IntRate](@N)),@N))
0
Author Commented:
@p*(POWER((1+dbo.[IntRate](@N)),@N))
0
Commented:
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.