SQL LOG Function

The SQL Server LOG function calculates the natural logarithmic value of a given float value. The LOG () function helps to find the natural logarithm value and the base (n) logarithmic values.

The syntax of teh LOG() function is shown below.

SELECT LOG(Float_Expression, base)
FROM [Source]

float_expression: It is an expression of float data type to find its logarithmic value.

Base: This is an optional argument. If you omit this argument, the logarithm function will consider the default e as the logarithm base. However, you can change the base value as per your requirement. The value of e is approximately equal to 2.71828.

SQL LOG Function Example

This Function is useful to calculate the logarithmic value of a given number with the specified base value. In this example, We use the LOG function on different data and display the output

Within this example query, first, we are calculating the logarithmic value of @i base e (It means LOG(10, e)). In the three SQL Server select statements below, we used this function directly on the int and float values 1, 96, and 5. Please refer to the list of SQL Math functions from our SQL Server tutorial page.

In the following SQL SELECT statement, we used the SQL Server LOG function with two arguments, 10 and 4. And they are the float and base values. It means the logarithmic value of 10 bases 4.

DECLARE @i float
SET @i = 10

SELECT LOG(@i)AS [LOG Result 1]

-- Calculating directly
SELECT LOG(1) AS [LOG Result 2]

SELECT LOG(96) AS [LOG Result 3]

SELECT LOG(10.90 + 15.10 - 22.50 + 1.50) AS [LOG Result 4]

-- Calculating with two arguments
SELECT LOG(10, 4) AS [LOG Result 5]

SELECT LOG((10.90 + 15.10 - 22.50 + 1.50), 8) AS [LOG Result 6]
SQL LOG Function 1

Calculating the logarithm of the exponent of a number

Both LOG() and EXP() are inverse functions. The following example calculates the LOG of the exponent number. First, EXP() finds the exponent of 10, and then LOG() finds its logarithm. Refer to the SQL EXP.

SELECT LOG(EXP(50)) AS result
result
50

Use the LOG function to calculate compound interest

This query uses the LOG() function to calculate the compound interest. This example finds how many years are required to get the final amount of 250,000 at an interest rate of 5%.

DECLARE @principal decimal = 100000, 
@rate decimal = 5,
@final decimal = 250000;
SELECT LOG(@final / @principal) / LOG(1.0 + @rate /100 )AS result
result
18.7802346532834

Logarithmic Function on table columns

In this SELECT statement, we will calculate the logarithmic value of all records present in the [StandardCost] and [Tax Amount] using the LOG Function.

Please refer to the SQL LOG10 (base 10 logarithm) and SQL SQRT functions from the list of available Mathematical functions.

SELECT [EnglishProductName]
      ,[Color]
      ,[StandardCost]
      ,LOG([StandardCost], 8) AS [Cost_Log result]
      ,[ListPrice]
      ,[DealerPrice]
      ,[SalesAmount]
      ,[TaxAmt]
      ,LOG([TaxAmt]) AS [Tax_Log result]
  FROM [Prod Sales]
LOG Function 2
Categories SQL