This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

SQL Server
SQL 2005+

SQL Server Logarithmic Functions

The forty-second part of the SQL Server Programming Fundamentals tutorial continues the examination of the mathematical Transact-SQL (T-SQL) functions. This article considers the logarithmic functions that are provided by Transact-SQL.

Logarithmic Functions

Logarithms are used primarily when working with large numbers. They are often used for scientific purposes and in engineering solutions. Logarithms are expressed with a base value. The logarithm of a number to a given base gives the value of the power that the base must be raised to in order to produce that number. For example, the base two logarithm of the number 256 is eight because two to the power of eight is 256.

Transact-SQL includes two functions that calculate logarithms. The first returns the base ten logarithm for a value. The second calculates the natural logarithm of a value. This is the logarithm that uses Euler's number, or e, as the base. Euler's number is approximately 2.71828.

Base 10 Logarithm

The base 10 logarithm of a value can be calculated in T-SQL using the Log10 function. The function requires a single argument containing the value to be processed. The following script shows some example calculations.

PRINT log10(1)      -- 0
PRINT log10(10)     -- 1
PRINT log10(100)    -- 2
PRINT log10(0.1)    -- -1
PRINT log10(0.01)   -- -2
PRINT log10(56)     -- 1.74819

Natural Logarithm

The natural logarithm of a value is returned by the Log function. As you can see in the following examples, Euler's number is not exactly 2.71828 so the log of this value is slightly below one.

PRINT log(2.71828)  -- 0.999999
PRINT log(10)       -- 2.30259

Exponential Value

The exponential function (Exp) is the inverse of the logarithm function. It returns e to the power of the value provided. The following examples demonstrate the function by performing the reverse operations to those in the previous script.

PRINT exp(0.999999) -- 2.71828
PRINT exp(2.30259)  -- 10
12 November 2009