SQL Server LOG() Function

The LOG() function in SQL Server calculates the logarithm of a number. You can specify the base of the logarithm, providing flexibility for various mathematical calculations.



LOG(): Definition and Usage

Logarithms are fundamental mathematical operations used to solve exponential equations. The LOG() function in SQL Server allows you to compute both natural logarithms (base *e*) and logarithms to other bases. The natural logarithm (base *e*) is the default if you only provide a single number as input; otherwise you specify the base as the second argument.

Syntax

Syntax

-- For SQL Server 2012 and later:
LOG ( number, base )

-- For Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse:
LOG ( number ) --This will calculate the natural logarithm (base e)
      

Parameter Values

Parameter Description
number The number for which you want to compute the logarithm. This must be greater than 0. This is required.
base (Optional, SQL Server 2012 and later) The base of the logarithm (must be greater than 1). If omitted, the natural logarithm (base *e*) is calculated.

Examples

Natural Logarithm (Base e)

This calculates the natural logarithm (base *e*) of 2.

Syntax

SELECT LOG(2);
      
Output

0.693147180559945 (The exact value might vary slightly depending on your SQL Server version)
      

Logarithm with a Specified Base

This calculates the logarithm of 2 with base 4.

Syntax

SELECT LOG(2, 4);
      
Output

0.5