Log2 Function In Sql in SQL
LOG2 Function in SQL
The LOG2 function returns the base-2 logarithm of a given number.
? Syntax:
LOG2(number)number: A positive numeric value.
? Example:
SELECT LOG2(8) AS base2_log;Result:
| base2_log |
|---|
| 3 |
Because 2³ = 8
? Notes:
The input must be greater than 0.
Returns
NULLor an error if the input is 0 or negative.Supported in:
? MySQL
? SQL Server (not directly supported; use:
LOG(number) / LOG(2))? Oracle (use:
LOG(2, number)orLN(number)/LN(2))
?? SQL Server Equivalent:
SELECT LOG(8) / LOG(2) AS base2_log;?? Oracle Equivalent:
SELECT LOG(2, 8) AS base2_log FROM dual;-- orSELECT LN(8) / LN(2) AS base2_log FROM dual;Let me know if you'd like a comparison table of LOG, LOG2, LOG10, and LN across databases!