forked from erikdarlingdata/DarlingData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureValue.sql
More file actions
63 lines (61 loc) · 1.37 KB
/
Copy pathFutureValue.sql
File metadata and controls
63 lines (61 loc) · 1.37 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
CREATE OR ALTER FUNCTION
dbo.FutureValue_Inline
(
@Rate float,
@Periods integer,
@Payment float,
@Value float,
@Type integer
)
RETURNS TABLE
AS
RETURN
/*
For support, head over to GitHub:
https://code.erikdarling.com
*/
WITH pre AS
(
SELECT
Type =
ISNULL(@Type, 0),
Value =
ISNULL(@Value, 0),
Term =
POWER(1 + @Rate, @Periods)
),
post AS
(
SELECT
FutureValue =
CASE
WHEN @Rate = 0
THEN (p.Value + @Payment) * @Periods
WHEN (@Rate <> 0
AND p.Type = 0)
THEN p.Value * p.Term + @Payment * (p.Term - 1) / @Rate
WHEN (@Rate <> 0
AND p.Type = 1)
THEN p.Value * p.Term + @Payment * (1 + @Rate) * (p.Term - 1.0) / @Rate
END
FROM pre AS p
)
SELECT
FutureValue =
CONVERT
(
float,
p.FutureValue * -1.
)
FROM post AS p;
GO