MySQL LOG2 Function

MySQL LOG2 function is one of the Mathematical functions used to return the logarithmic value of a given number with base 2. Let us see how to find base 2 logarithmic values using Command Prompt and Workbench with examples.

The basic syntax of the LOG2 is as shown below:

SELECT LOG2(x) FROM Source

MySQL LOG2 Function Example

The LOG2 Function returns the logarithmic value with base 2 of any numeric integer. The following query shows multiple ways to use this one.

SELECT LOG2(10);

SELECT LOG2(623250);

SELECT LOG2(250), LOG2(-150);
Example 1

This Mathematical method also allows you to find the base 2 logarithmic value of a column of data. In this MySQL example, we are going to find the base 2 logarithmic values for all the records present in the Standard Cost, Sales Amount, and Tax Amount columns.

SELECT EnglishProductName, 
       Color,
       StandardCost,
       LOG2( StandardCost) AS LogStdCost,
       SalesAmount,
       LOG2( SalesAmount) AS LogSalesAmount,
       TaxAmt,
       LOG2( TaxAmt) AS TaxAmt
  FROM `mathemetical functions`;
MySQL LOG2 Function 2