# SQL SERVER – Writing Function for The Rule of 72 During a recent project, the Comprehensive Database Performance Health Check, I had a fascinating side conversation with a client that introduced me to a financial concept called the Rule of 72. Originating from the world of finance, it offers a simple and quick way to estimate the time it takes for an investment to double based on a fixed interest rate. In this blog post, we will explore the history of the Rule of 72, delve into some fun facts, and then demonstrate two useful in-line functions in SQL Server that can leverage this rule.

### History of the Rule of 72

The Rule of 72 is believed to have originated in the 15th century, although its exact origins are unclear. It is commonly attributed to Luca Pacioli, an Italian mathematician who wrote about the rule in his book “Summa de arithmetica, geometria, proportioni et proportionalità” in 1494. Over the centuries, this rule has been widely adopted and is now commonly used in various fields, including finance and investment analysis. Please note that this is just a belief.

### Fun Facts about the Rule of 72

• The Rule of 72 is an approximation rather than an exact calculation. It assumes compound interest, which means it may not be as accurate for investments with fluctuating interest rates.
• The Rule of 72 is versatile and can be used with any compounding period, such as annually, quarterly, or monthly, as long as the interest rate remains constant.
• The Rule of 72 can also be used to estimate the impact of inflation. By using the inflation rate as the interest rate, you can determine how long it will take for prices to double.

### In-line Function 1: Estimating Time to Double Money

Now let’s explore the first in-line function that utilizes the Rule of 72. This function takes a percentage interest rate as input and returns the estimated time for an investment to double.

```CREATE FUNCTION dbo.DoublingTime(@interest_rate DECIMAL(5, 2))
RETURNS DECIMAL(6, 2)
AS
BEGIN
RETURN (72 / @interest_rate)
END;```

This function takes an interest rate as a percentage and returns the estimated time (in years) for the investment to double. Here’s an example of how to use the function:

```SELECT dbo.DoublingTime(8) AS 'Doubling Time';
-- Returns 9.00 (years)```

### In-line Function 2: Calculating Required Interest Rate

The second-in-line function we’ll discuss uses the Rule of 72 in reverse. This function takes the desired number of years as input and returns the approximate percentage interest rate required to achieve that doubling time.

```CREATE FUNCTION dbo.RequiredInterestRate(@years DECIMAL(5, 2))
RETURNS DECIMAL(6, 2)
AS
BEGIN
RETURN (72 / @years)
END;```

This function takes the desired number of years to double the investment and returns the required interest rate (as a percentage). Here’s an example of how to use the function:

```SELECT dbo.RequiredInterestRate(12) AS 'Required Interest Rate';
-- Returns 6.00 (%)```

### Conclusion

The Rule of 72 is a simple yet powerful tool that provides quick estimations regarding doubling time and required interest rates. In this blog post, we explored the history and fun facts about the rule and demonstrated how to leverage it in SQL Server using two handy in-line functions. By incorporating these functions into your database toolkit, you can easily perform calculations related to investment and interest rate analysis. If you have any such idea, do post a comment.

You can always reach out to me on LinkedIn.

Reference: Pinal Dave (https://blog.sqlauthority.com)