|
|
|
|
|
|
|
|
Re: Duplicating Excel COUPNCD function in PLSQL. [message #671535 is a reply to message #671533] |
Tue, 04 September 2018 03:46   |
John Watson
Messages: 8840 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
You (not "u". Abbreviations like that are seriously bad form on a professional forum. Save them for your twittering) have to explain the algorithm that COUPNCD implements. The mathematics behind it: the input, the output, and how to calculate one from the other.
|
|
|
Re: Duplicating Excel COUPNCD function in PLSQL. [message #672418 is a reply to message #671528] |
Sun, 14 October 2018 12:48  |
 |
Barbara Boehmer
Messages: 9005 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
The following works for actual/actual day count basis (1), as demonstrated using the example in the previously provided link.
SCOTT@orcl_12.1.0.2.0> CREATE OR REPLACE FUNCTION couponcd
2 (p_settlement IN DATE,
3 p_maturity IN DATE,
4 p_frequency IN INTEGER)
5 RETURN DATE
6 AS
7 BEGIN
8 -- check for input errors:
9 IF p_frequency NOT IN (1, 2, 4) THEN
10 RAISE_APPLICATION_ERROR (-20001, 'Frequency must be 1 or 2 or 4!');
11 END IF;
12 IF TRUNC (p_settlement) >= TRUNC (p_maturity) THEN
13 RAISE_APPLICATION_ERROR (-20002, 'Settlement cannot be >= maturity!');
14 END IF;
15 -- calculate and return next coupon date:
16 RETURN ADD_MONTHS
17 (TRUNC (p_maturity),
18 ((12 / p_frequency) * TRUNC (MONTHS_BETWEEN (p_settlement, p_maturity) / (12 / p_frequency))));
19 END couponcd;
20 /
Function created.
SCOTT@orcl_12.1.0.2.0> SHOW ERRORS
No errors.
SCOTT@orcl_12.1.0.2.0> SELECT couponcd
2 (TO_DATE ('01-01-2011', 'DD-MM-YYYY'),
3 TO_DATE ('15-11-2011', 'DD-MM-YYYY'),
4 2)
5 FROM DUAL
6 /
COUPONCD(TO
-----------
15-MAY-2011
1 row selected.
|
|
|