LOG10() Examples in SQL Server

InĀ SQL Server, the T-SQL LOG10() function is a mathematical function that returns the base-10 logarithm of the specified float expression.

You specify the float expression as an argument.

Syntax

The syntax goes like this:

LOG10 ( float_expression ) 

Where float_expression is an expression of type float or of a type that can be implicitly converted to float.

Example 1 – Basic Usage

Here’s a basic example to demonstrate how this function works.

SELECT LOG10(10) Result;

Result:

+----------+
| Result   |
|----------|
| 1        |
+----------+

And here’s another example using various values.

SELECT 
  LOG10(100) '100',
  LOG10(1000) '1000',
  LOG10(3000) '3000',
  LOG10(10000) '10000';

Result:

+-------+--------+------------------+---------+
| 100   | 1000   | 3000             | 10000   |
|-------+--------+------------------+---------|
| 2     | 3      | 3.47712125471966 | 4       |
+-------+--------+------------------+---------+

Example 2 – Negative Value

Here’s an example of passing in a negative value.

SELECT LOG10(-4) Result;

Result:

Msg 3623, Level 16, State 1, Line 1
An invalid floating point operation occurred.

This example returns an error message because the logarithm can only be returned for positive numbers that aren’t 1.

Example 3 – Zero

Here’s an example of passing in zero as the argument (we get the same result as the previous example).

SELECT LOG10(0) Result;

Result:

Msg 3623, Level 16, State 1, Line 1
An invalid floating point operation occurred.

Example 4 – Passing in 1

Passing in a value of 1 returns zero.

SELECT LOG10(1) Result;

Result:

+----------+
| Result   |
|----------|
| 0        |
+----------+

Example 5 – Expressions

You can also pass in expressions like this:

SELECT LOG10(5*2) Result;

Result:

+----------+
| Result   |
|----------|
| 1        |
+----------+

That’s effectively the same as doing this:

SELECT LOG10(10) Result;

Result:

+----------+
| Result   |
|----------|
| 1        |
+----------+

LOG()

Transact-SQL also has the LOG() function which returns the natural logarithm of a number.

In fact, you can use LOG() instead of LOG10() if you prefer. To do this, use 10 as the second argument to the function. Example below.

SELECT 
  LOG(100, 10) 'LOG',
  LOG10(100) 'LOG10';

Result:

+-------+---------+
| LOG   | LOG10   |
|-------+---------|
| 2     | 2       |
+-------+---------+