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 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.8528444,"math_prob":0.97197837,"size":1696,"snap":"2020-34-2020-40","text_gpt3_token_len":433,"char_repetition_ratio":0.17080379,"word_repetition_ratio":0.0,"special_character_ratio":0.27712265,"punctuation_ratio":0.10091743,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9975087,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-12T04:43:33Z\",\"WARC-Record-ID\":\"<urn:uuid:dfc269fd-9304-4366-a615-36c0c0958168>\",\"Content-Length\":\"9958\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:69846d53-17c3-4c0a-a8be-27d6606a86dd>\",\"WARC-Concurrent-To\":\"<urn:uuid:06629bf9-9efd-44e6-88f6-cdb7b26d1061>\",\"WARC-IP-Address\":\"84.18.206.205\",\"WARC-Target-URI\":\"http://blackwasp.co.uk/SQLLogFunctions.aspx\",\"WARC-Payload-Digest\":\"sha1:O2ZSPXUX3L5BBOY7RBINED7ZRXVVN3F2\",\"WARC-Block-Digest\":\"sha1:LHFWTCE3MK2ZPIOWCZT5X5U43EJMKYEW\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738864.9_warc_CC-MAIN-20200812024530-20200812054530-00571.warc.gz\"}"}