T-SQL LOG() Function
The T-SQL (Transact-SQL) LOG() function returns the natural logarithm or logarithm to the specified base of a given number. In special cases it returns the following:
- If the number is less than or equal to 0, then an error is returned.
- If the base is less than or equal to 0, then an error is returned.
- If the base is equal to 1, then an error is returned.
Syntax
/* natural logarithm of number */ LOG(number) /* logarithm of number to the specified base */ LOG(number, base)
Parameters
number |
Required. Specify the number. Must be greater than 0. |
base |
Specify the number. Must be greater than 0 and not equal to 1. |
Return Value
Returns the natural logarithm or logarithm to the specified base of a given number.
Example 1:
The example below shows the usage of LOG() function.
SELECT LOG(1); Result: 0 SELECT LOG(1.5); Result: 0.4054651081081644 SELECT LOG(5); Result: 1.6094379124341003 SELECT LOG(4, 3); Result: 1.2618595071429148 SELECT LOG(0.8, 0.7); Result: 0.6256216061886871 SELECT LOG(10, 0.5); Result: -3.3219280948873626 SELECT LOG(25, 5); Result: 2
Example 2:
Consider a database table called Sample with the following records:
Data | x |
---|---|
Data 1 | 0.5 |
Data 2 | 1 |
Data 3 | 5 |
Data 4 | 10 |
Data 5 | 50 |
The statement given below can be used to calculate the base-10 logarithm of column x.
SELECT *, LOG(x, 10) AS LOG_Value FROM Sample;
This will produce the result as shown below:
Data | x | LOG_Value |
---|---|---|
Data 1 | 0.5 | -0.30102999566398114 |
Data 2 | 1 | 0 |
Data 3 | 5 | 0.6989700043360187 |
Data 4 | 10 | 1 |
Data 5 | 50 | 1.6989700043360185 |
Example 3:
Consider a database table called Sample with the following records:
Data | x | y |
---|---|---|
Data 1 | 0.5 | 2 |
Data 2 | 1 | 2 |
Data 3 | 5 | 2 |
Data 4 | 10 | 10 |
Data 5 | 50 | 10 |
To calculate the logarithm of records of column x with base as records of column y, the following query can be used:
SELECT *, LOG(x, y) AS LOG_Value FROM Sample;
This will produce the result as shown below:
Data | x | y | LOG_Value |
---|---|---|---|
Data 1 | 0.5 | 2 | -1 |
Data 2 | 1 | 2 | 0 |
Data 3 | 5 | 2 | 2.321928094887362 |
Data 4 | 10 | 10 | 1 |
Data 5 | 50 | 10 | 1.6989700043360185 |
❮ T-SQL Functions