Solved

VB pmt function in SQL

Posted on 2014-03-12
3
688 Views
Last Modified: 2014-03-24
I have some vb that works out loan repayments but i need to code into a stored procedure.

I've found that there is no pmt function in sql to do this. Can anyone help me write the following in SQL?


Math.Round(Pmt(0.119 / 12, 300, -17030), 2)

Open in new window


Thanks
0
Comment
Question by:edjones1
3 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39925179
0
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 39946877
Looks like somebody may have already written a function for you that does this exact calculation (from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55819):

create function dbo.PMT
	(
	-- Datetype for @rate changed to float
	@rate float,
	@periods smallint,
	@principal numeric(20,2)
	)
returns numeric (38,9)
as
begin
declare @pmt numeric (38,9)

declare @WK_periods float,
	@WK_principal float,
	@wk_One float,
	@WK_power float

-- Convert data for calculation
select  @WK_periods = @periods,
	@WK_principal = @principal,
	@WK_One = 1

select  @pmt =
round(
-- Do all multiplication before division
( @WK_principal * (@rate*power(@WK_One+@rate,@WK_periods)))
/ (power(@WK_One+@rate,@WK_periods)-@WK_One)
,9)

return @pmt

end
go

Open in new window


And it would be a direct swap in your example above:

= CONVERT(NUMERIC(20,2), CONVERT(INT, dbo.PMT(0.119 / 12, 300, -17030) * 100))/100

Open in new window

0
 

Author Closing Comment

by:edjones1
ID: 39949913
perfect thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question