LOG() Function in SQL Server


The LOG() function in SQL Server is used to return the natural logarithm of a specified number. The natural logarithm is the logarithm to the base e, where e is an irrational constant approximately equal to 2.71828.

Syntax:

LOG(numeric_expression)

Where numeric_expression is the number for which the natural logarithm is to be calculated.

Example:

SELECT LOG(10) AS NaturalLogarithm;

Output:

NaturalLogarithm
----------------
2.30258509299405

In the above example, the natural logarithm of 10 is calculated using the LOG() function and returned as a result.

It is also possible to calculate the logarithm to a different base by using the following formula:

LOG(base, numeric_expression)

Where base is the base of the logarithm and numeric_expression is the number for which the logarithm is to be calculated.

Example:

SELECT LOG(2, 8) AS LogarithmBase2;

Output:

LogarithmBase2
--------------
3

In the above example, the logarithm of 8 to the base 2 is calculated using the LOG() function and returned as a result.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.