SQL LN function
Returns the natural logarithm (base e) of a positive number: LN(1) is 0 and LN(EXP(5)) is about 5.
- Kategorie
- Math
- Returns
float / double- In this category
- 20 of 82
Syntax
LN(number)
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Different name or syntax | No LN; LOG(x) is the natural log on SQL Server |
| PostgreSQL | Ja | Supported under this name. |
| MySQL | Ja | Supported under this name. |
| SQLite | Ja | Requires SQLite 3.35+ with the math functions built in |
| Oracle | Ja | Supported under this name. |
Beispiele
PostgreSQL
SELECT LN(1) AS zero, LN(EXP(5)) AS five; -- 0, 5
SQL Server
SELECT LOG(10) AS natural_log; -- 2.302...
PostgreSQL
SELECT LN(growth_factor) AS log_growth FROM populations WHERE growth_factor > 0;
Gut zu wissen
- LN(0) and LN(negative) have no answer: MySQL and SQLite return NULL, PostgreSQL raises 'cannot take logarithm of zero', Oracle raises ORA-01428, SQL Server's LOG(0) raises 'An invalid floating point operation occurred'.
- SQL Server spells it LOG. LOG(x, 2) there is the base-2 log of x, not the natural log, and LN raises 'not a recognized built-in function name'.
- Filter the domain first: WHERE x > 0, or CASE WHEN x > 0 THEN LN(x) END for the rest.
Questions about LN
What is the difference between LN and LOG?
LN is always base e. LOG means base e on SQL Server, base 10 with one argument on PostgreSQL and SQLite, and takes the base first as LOG(base, n) on MySQL and Oracle. Check the engine before you swap them.
What does LN(0) return?
Nothing valid: MySQL and SQLite give NULL, while PostgreSQL, Oracle and SQL Server (LOG(0)) raise an error. Guard with WHERE x > 0.
Why is EXP(LN(10)) not exactly 10?
Both are floating point operations, so the round trip can drift in the last digit: SQL Server returns 10.000000000000002 for EXP(LOG(10)). Round the result if exact display matters.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.