Feeds:
Posts
Comments

Archive for the ‘Mathematical Functions’ Category

LOG function is used to calculate the natural logarithm of a specified float expression and it is part of SQL Server since earlier versions of SQL Server. But in SQL Server 2012, this function is being modified and became more flexible. In the earlier versions of SQL Server, the base of log function was approximately 2.718281828 or EXP(1) and it is the default value (not editable). But in SQL Server 2012, you are allowed to set your own base value. Lets discuss its syntax and examples.

Syntax

--In SQL Server earlier versions
LOG ( float_expression )

--In SQL Server 2012
LOG ( float_expression [, base ] )

Let me explain it with simple examples :

Example 1 :
In the example 1, use LOG function with default base value.

DECLARE @variable float;
SET @variable = 10;
SELECT LOG(@variable) as [LOG with Default base value]
GO
--OUTPUT

LOG with Default base value
—————————
2.30258509299405

(1 row(s) affected)

Example 2 :
In example 2, lets provide an input base and see the results.

DECLARE @variable float;
SET @variable = 10;
SELECT LOG(@variable,EXP(1)) as [LOG with defined base value]
GO
--OUTPUT

LOG with defined base value
—————————
2.30258509299405

(1 row(s) affected)

If you observe examples 1 & 2, the result is same in both examples because by default the base value of LOG function is EXP(1).

Example 3 :
In example 3, lets pass any other value than the EXP(1) (Default value of base) and observe the result.

DECLARE @variable float;
SET @variable = 10;
SELECT LOG(@variable,EXP(2)) as [LOG with defined base value]
GO
--OUTPUT

LOG with defined base value
—————————
1.15129254649702

(1 row(s) affected

Conclusion:
In SQL Server 2012, LOG function is more flexible and gives you more control on base value but still base value is optional. So if you don’t pass base value it takes the default value (EXP(1) or approximately 2.718281828)

Advertisements

Read Full Post »