SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite LOG2() Function



The SQLite LOG2() function returns the base-2 logarithm of a given number. In special cases it returns the following:

  • If the number is less than or equal to 0, then NULL is returned.

Syntax

LOG2(number)

Parameters

number Required. Specify the number. Must be greater than 0.

Return Value

Returns the base-2 logarithm of a given number.

Example 1:

The example below shows the usage of LOG2() function.

SELECT LOG2(1);
Result: 0.0

SELECT LOG2(1.5);
Result: 0.584962500721156

SELECT LOG2(2);
Result: 1.0

SELECT LOG2(5);
Result: 2.32192809488736

SELECT LOG2(10);
Result: 3.32192809488736

SELECT LOG2(0);
Result: NULL

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 10.5
Data 21
Data 35
Data 410
Data 550

The statement given below can be used to calculate the base-2 logarithm of column x.

SELECT *, LOG2(x) AS LOG2_Value FROM Sample;

This will produce the result as shown below:

DataxLOG2_Value
Data 10.5-1.0
Data 210.0
Data 352.32192809488736
Data 4103.32192809488736
Data 5505.64385618977472

❮ SQLite Functions