Last active
June 1, 2022 13:46
-
-
Save Azsaturndx/09771766a33a0a587215cb2c3d39add7 to your computer and use it in GitHub Desktop.
FV Function (Future value) - SQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Future Value (FV) | |
* (SQL Server) | |
* AzsaturnDx (2020) - No copyright at all | |
* | |
* Calculates the future value of an investment based on a constant interest rate. You can use FV with either periodic, constant payments, or a single lump sum payment. | |
* | |
* @Rate FLOAT - Required. The interest rate for the loan. | |
* @Periods INT - Required. The total number of payments for the loan. | |
* @Payment FLOAT - Required. The payment made each period; it cannot change over the life of the annuity. | |
* @Value FLOAT - Optional. The present value, or the lump-sum amount that a series of future payments is worth right now. If pv is omitted, it is assumed to be 0 (zero), and you must include the pmt argument. | |
* @Type INT - Optional. The number 0 (zero) or 1 and indicates when payments are due. (0 = End of Period , 1 = Start of Period) | |
* @return FLOAT Future Value | |
* | |
* @Example | |
* SELECT dbo.FV(.20,12,1,2,0) | |
* | |
* More financial functions are available @: https://gist.github.com/Azsaturndx/8ce3e47e2d3210a65fc83b09ac32da5f | |
*/ | |
CREATE FUNCTION [dbo].[FV] | |
(@Rate FLOAT | |
,@Periods INT | |
,@Payment FLOAT | |
,@Value FLOAT | |
,@Type INT) | |
RETURNS FLOAT | |
BEGIN | |
SET @Type = ISNULL(@Type,0); | |
SET @Value = ISNULL(@Value,0); | |
DECLARE @Result AS FLOAT=0; | |
DECLARE @Term AS FLOAT = 0; | |
IF @Rate=0 | |
BEGIN | |
SET @Result=(@Value+@Payment)*@Periods; | |
END --IF @Rate=0 | |
ELSE | |
BEGIN --IF @Rate <> 0 | |
SET @term = POWER(1+@rate,@periods); | |
IF @Type=1 | |
BEGIN | |
SET @Result=@Value*@Term+@Payment*(1+@Rate)*(@Term-1.0)/@Rate | |
END | |
ELSE | |
BEGIN | |
SET @Result=@value*@term+@Payment*(@Term-1)/@Rate | |
END | |
END --IF @Rate <> 0 | |
RETURN -@Result | |
END; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment