Friday, December 25, 2015

SQL - LOG() Mathematical Function


SQL LOG()
This function was introduced within the release of SQL Server 2005. This function is used to calculate the natural logarithm of a number in SQL. In a very simple manner, we can say that the LOG function computes the logarithm of an expression. It plays a very important role in the data analytics.

Syntax
We can use this function by using below syntax-
LOG (Float Expression [, Base ] )
Base
Optional integer argument that sets the base for the logarithm. The base by which to compute the logarithm. When you do not specify a value, the function computes the natural logarithm of the expression by using e for the base where e is equal to 2.718281828459.
Float Expression
A numeric expression which is greater than zero. When the value is equal to or less than zero, LOG returns an NA value.
Return Types: The important thing is that it always return float type output.
Remarks: By default, LOG() returns the natural logarithm.  User can change the base of the logarithm to another value using the optional base parameter.
The natural logarithm of the exponential of a number is the number itself: LOG( EXP( n ) ) = n. And the exponential of the natural logarithm of a number is the number itself:
EXP( LOG( n ) ) = n.
Calculating the logarithm
You can easily calculate the log for the specified float expression as given below:


--- declare float variable and set value
Declare @inpFloat float=10.24
--- declare int variable and set value
Declare @inpInt int=10
--- declare decimal variable and set value
Declare @inpDecimal int=10.24

---- select value from the log function
SELECT LOG(@inpFloat) as 'LOG Value from Float',
LOG(@inpInt) as 'LOG Value from Int',
LOG(@inpDecimal) as 'LOG Value from Decimal'

LOG Value from Float
LOG Value from Int
LOG Value from Decimal
2.32630162
2.302585093
2.302585093

--- declare base
Declare @base int=2

---- select value from the log function
SELECT LOG(@inpFloat, @base ) as 'LOG Value from Float',
LOG(@inpInt, @base) as 'LOG Value from Int',
LOG(@inpDecimal, @base) as 'LOG Value from Decimal'
LOG Value from Float
LOG Value from Int
LOG Value from Decimal
3.35614381
3.321928095
3.321928095


Now, we can see that log function returns two results. One is without base value and another one is with base value.

Conclusion
As we already know that this function plays important role in the data analytics calculations. Apart from this; logarithm is used by navigators, scientists, engineers, and others to perform computations more easily. In public-key cryptography application, discrete logarithm is used to generate keys.

1 comment:

  1. You have share a great information about SQL data base thanks for sharing this and keep writing content like this. Yellowstone Vest

    ReplyDelete