Oracle LOG Function: A Simple Guide

The LOG function in Oracle SQL is a mathematical function that returns the logarithm of a number to a specific base.

While the LN function is always base e, the LOG function lets you choose any base you want, such as 10 (for the common logarithm) or 2.

What is the LOG Function in Oracle?

The LOG(base, number) function answers the question: "To what power must base be raised to get number?"

  • number (n1): Must be a positive number.
  • base (n2): Must be a positive number other than 0 or 1.
  • LOG(10, 100) returns 2 (because 10^2 = 100).
  • LOG(2, 8) returns 3 (because 2^3 = 8).

LOG Function Syntax

The syntax for LOG requires two arguments:

LOG(n2, n1)

Let's break that down:

  • n2 (the base): The base of the logarithm.
  • n1 (the number): The number for which you want to find the logarithm.

Oracle LOG Function Examples

Here are two practical examples of how to use LOG.

Example 1: Finding the Log Base 10 (Common Log)

This example calculates the common logarithm of 1000 (i.e., "what power of 10 gives 1000?").

Query:

SELECT 
  LOG(10, 1000) AS "Log_Base_10_of_1000"
FROM DUAL;

Result:

Log_Base_10_of_1000
-------------------
                  3

Example 2: Finding the Log Base 2

This example is common in computing and calculates the log base 2 of 64 (i.e., "what power of 2 gives 64?").

Query:

SELECT 
  LOG(2, 64) AS "Log_Base_2_of_64"
FROM DUAL;

Result:

Log_Base_2_of_64
----------------
               6
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments