SQL Server LOG() Function

The LOG() function in SQL Server returns the natural logarithm of a given number. The natural logarithm is the logarithm to the base e (approximately 2.71828), represented as ln(x).

Syntax

LOG(number)

Parameters:

  • number: Required. The positive number for which the natural logarithm is to be returned.

Return Value:

  • The natural logarithm of number. Returns NULL if number is less than or equal to 0.

Usage

The LOG() function is commonly used in calculating exponential distributions in probability and statistical analysis.

Examples

Suppose we have a table sales containing sales data for a product, with a column revenue representing the sales amount. We want to calculate the natural logarithm of the sales amount.

SELECT revenue, LOG(revenue) as log_revenue
FROM sales

Result:

revenue log_revenue
100 4.605170
200 5.298317
300 5.703782

Another example, we want to calculate the investment return rate based on different interest rates.

SELECT investment, rate, LOG(investment) / LOG(1+rate) as return_rate
FROM investments

Result:

investment rate return_rate
1000 0.1 6.144564
2000 0.2 7.178977
3000 0.3 7.853654

Conclusion

The LOG() function is very useful in calculating exponential distributions and investment returns. It can calculate the natural logarithm of any positive number and combine it with other numerical values for more complex calculations.