Question

How to create PPMT, IPMT, etc FInancial functions in MySQL

Asked by: biolgb

Hello there,
I wanted to create these financial functions in MySQL. Could you please provide me the code?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-06 at 18:01:29ID24879834
Tags

MySQL

Topics

MySQL Server

,

PHP and Databases

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Sockets & Finance
    Can anybody recommend a good tutorial for learning sockets and financial transactions? I'm interested in writting an app to keep tabs on credit card balances (my own, not the world's) via internet & would like to get an idea of how much a nightmare I'm looking at. I want ...
  2. Finance
    Hi, this one is for a “master of finance” Say you have an initial investment of $ 20000 and a monthly premium of say $ 500. What will the Future value be after 3 months (1 quarter)? Assume the interest rate is for that quarter is 14% annually compounded. You see, I want to ...
  3. Finance Charge Computation
    I am trying to create a statement to send to customers. I would like the statement to include a finance charge. The underlying recordset of my report returns a row for each outstanding invoice. The finance charge computation is like this for each line: BalanceDue * (Inv...
  4. Get the finance chart from finance yahoo
    I need to get the Finance chart from http://finance.yahoo.com/ I managed to get the Stock Last Trade now i need to get the chart and the News Headlines data. Thanks.
  5. Working with dates in a bond finance spreadsheet model
    zorvek - I hope your looking on this I am going take another stab at explaining municpal bond finance and try to get some help solving my problem. Thanks again for you efforts last time. This is somewhat complex so let me see if I can explain it so that it makes since what ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: mwvisa1Posted on 2009-11-06 at 19:06:24ID: 25764803

http://www.techonthenet.com/excel/formulas/pmt.php
http://www.techonthenet.com/excel/formulas/ipmt.php

http://www.techonthenet.com/excel/formulas/ppmt.php

The above explain the functionality of the financial functions you mentioned as well as the PMT function.  You could create a schema called Financials and then create user defined functions for each of these.  You could create PMT first and then use the same logic in the others as you will need to determine the periodic payment amount to subtract at each payment to find the next balance by which interest is calculated.

HTH

M-1

 

by: biolgbPosted on 2009-11-08 at 10:02:28ID: 25771213

Hi MWVISA1,
Thanks for this response. I already have PMT but could you help me derive PPMT and IPMT from the code below;

>>>> Working PMT Function in MySQL
DELIMITER $$

DROP FUNCTION IF EXISTS `C245037_InterAct`.`PMT` $$
CREATE DEFINER=`C245037_biolgb`@`%` FUNCTION `PMT`(
      mPv double,
      mFv double,
      mNPer int,
      mApr float,
      mMonths int
     ) RETURNS double
BEGIN

  Declare mVal double;


/*
In excel it should look like this:
=PMT(A1/12,B2,-C3, D4,1)
A1 = 7.40%
B2 = 36 (Month)
C3 = 106344 (Financing)
D4 = 75960 (Rest value)
In excel the result = 1404

set Pv=106344;
set Fv=75960;
set NPer=36;
set Apr=.074;
set mMonths=12;

*/

select (mPV-mFV)/mNPER + (mPV+mFV-(mPV-mFV)/mNPER)/2*(mAPR/mMonths) into mVal;


return mVal;

END $$

DELIMITER ;

 

by: mwvisa1Posted on 2009-11-08 at 16:13:28ID: 25772681

I am working on this by the way, just wanted to run through some tests before posting back to ensure I understand correctly the effects of payments at beginning of month versus end of month.

 

by: mwvisa1Posted on 2009-11-08 at 21:40:33ID: 25773599

Okay, here we go ...

http://en.wikipedia.org/wiki/Mortgage_calculator

Above is usually a reference I use to remind myself of the mortgage payment calculation formula.

rate / (1 - (1+rate)^-Nper) * pv

Where rate is monthly rate; hence, APR / 12.  Therefore, the rate passed in should already be divided by 12 just like in Excel.  Nper is the number of payments, so it is the term of the loan multiplied by 12 (or other number of payments per year value if different than monthly).

This is the PMT() function at its basic level; however, Excel allows for two other factors to the calculated payment: future value and type.

Future value establishes amount of loan left after making all payments specified by Nper.  

Type specifies if loan is paid at beginning of period versus end.

These are both spelled out in the definition of PMT I linked to earlier; however, pointing out again as they alter the *pv* used in above formula.  

So :

P = pv - fv / (1+rate)^Nper

If type = 1, then :
P = P * (1-rate)

And updated formula :
rate / (1 - (1+rate)^-Nper) * P

Guess you can adjust the formula to handle the negative input to truly mimic Excel; however, tend to think of this in the positive.

From the above, we can also derive a future value formula.

(1+rate)^per * pv - ((1+rate)^per - 1) / rate * pmt

Where rate is same as above; per(iod) is the number of the specific payment you want like finding the future value on 6th payment; pmt is the calculated payment like from PMT() function.

Adjusting this again for type in same way as above with if statement, we get the FV() function similar to Excel.

Using the two functions afore mentioned, the PPMT and IPMT functions are greatly simplified to these formulas :

PPMT > pmt -  (fv * rate)
IPMT > fv * rate

Both are achieved by using pv and per -- same definitions as above -- to get fv.

fv( rate, per-1, c, pv, type )

Not that per-1 one used as in both cases we want the future value the payment before, so that we can then calculate the monthly interest for next payment (IPMT) and subsequently the principal of next payment (PPMT).  Consequently, you can see that the PPMT formula can even be:

pmt - ipmt

So implementing this to MySQL just remember pow() or power() function for exponents shown (e.g., pow(1+rate, nper)).  

See code snippet for example of PMT().


M-1

DELIMITER $$ 
 
DROP FUNCTION IF EXISTS `PMT`$$
-- Pmt( interest_rate, number_payments, PV, FV, Type )
CREATE FUNCTION `PMT`(
   mApr double,
   mNPer int,   
   mPv double,
   mFv double,
   type int
) RETURNS double
BEGIN 
  Declare mVal double; 
/*
In excel it should look like this:
=PMT(A1/12,B2,-C3, D4,1)
A1 = 7.40%
B2 = 36 (Month)
C3 = 106344 (Financing)
D4 = 75960 (Rest value)
In excel the result = 1404 
set Pv=106344;
set Fv=75960;
set NPer=36;
set Apr=.074;
set mMonths=12; 
*/ 
 
select mApr / (1 - pow(1 + mApr, -mNPer)) * ((mPv - mFv / pow(1 + mApr, mNPer)) * (1-if(type=1, mApr, 0))) into mVal; 
 
return mVal; 
END$$ 
 
DELIMITER ;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:

Select allOpen in new window

 

by: mwvisa1Posted on 2009-11-08 at 21:44:20ID: 25773606

Have tested this against Excel extensively, but please test to ensure it holds up in all scenarios you are working with.  Then I would create the FV() function next using the formula above I showed.

My code snippet should have given you all the syntax you need.

Using both, the IPMT and PPMT will be much easier.

Give them a try yourself and post back as you will have a better understanding if you code on your own.

 

by: biolgbPosted on 2009-11-09 at 04:32:45ID: 25775280

This is very good my friend, let me try it out. This looks more like the solution I am looking for. I will let you know after I test it in the actual program. Thanks.

 

by: mwvisa1Posted on 2009-11-11 at 12:07:23ID: 25798507

Okay, not sure how things are going on your end, but did some more testing to see if can match up to the sign of Excel since I was inverse (i.e., returning positive values for positive pv) and refined formulas to this:


c = payment
rate = interest rate (monthly)
nper = number of payments / periods
pv = present value
fv = future value
per = period to check -- PPMT / IPMT


PMT:
pmt = rate / ((1 + rate)^nper - 1) * -(pv * (1 + rate)^nper + fv)

if type = 1, then pmt = pmt / (1+rate)

(so if type = 1 -- beginning of month payment -- divide first result by (1+rate))


FV:
if type = 1, then c = c * (1+rate)

(if beginning of the month payment, then multiply the passed in payment by (1+rate))

fv = -(((1 + rate)^nper - 1) / rate * c + pv * (1 + rate)^nper);



IPMT:
ipmt = fv(rate, per-1, pmt(rate, nper, pv, fv, type), pv, type) * rate

Use the fv() and pmt() functions.

if type = 1, then ipmt = ipmt / (1+rate)

(same as above, the result changes if type = 1)


PPMT:
pmt(rate, nper, pv, fv, type) - ipmt(rate, per, nper, pv, fv, type);

Use the pmt() and ipmt() functions.


Hope that helps.

 

by: mwvisa1Posted on 2009-11-11 at 12:11:48ID: 25798535

So what does this mean in code :

pmt = rate / ((1 + rate)^nper - 1) * -(pv * (1 + rate)^nper + fv)
if type = 1, then pmt = pmt / (1+rate)

select (mApr / (pow(1 + mApr, mNPer) + 1) * -(mPv * pow(1 + mApr, mNPer) + fv)) / if(type=1, 1+mApr, 1) into mVal;


Or the last piece can be written as "... / (1+if(type=1,mApr,0))".

Hope that makes sense.

 

by: mwvisa1Posted on 2009-11-15 at 06:39:33ID: 25824975

Your question inspired me to write an article on the subject as I see the mortgage application question in Java quite a bit where the formula for PMT() is just the first step and similarly here for you.  Therefore, I think this may be a good read as I added some deeper explanation around implementation in databases.


         A Guide to the PMT, FV, IPMT and PPMT Function
http://www.experts-exchange.com/A_1948.html

Best regards,

Kevin

 

by: biolgbPosted on 2010-01-02 at 05:03:35ID: 26161384

Great job Kevin. You are a man to rely on. Much thanks.

 

by: mwvisa1Posted on 2010-01-02 at 15:01:00ID: 26163399

Thanks and glad to help.
Best regards and happy coding,
Kevin

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...