SQLite 3 reference · Math Functions
SQLite LOG() Function
Return the base-10 logarithm of a number, or the logarithm in any base with log(B, X). log10() and log2() also exist.
SQLite 3 Since 3.35.0
Syntax
log(X) | log(B, X) | log10(X) | log2(X)
Parameters
| Parameter | Description |
|---|---|
B optional |
The base. |
X |
A number (text that looks like a number is converted). |
Return value
A floating-point number, or NULL.
Examples
SELECT log(1000) AS base10, log(2, 8) AS base2, log2(1024) AS log2_fn;
Output
base10 | base2 | log2_fn 3 | 3 | 10
Run this on your computer — see “Get Started” for setup.
SELECT name, price, CAST(log10(price) AS INTEGER) + 1 AS digits_before_point
FROM products
WHERE product_id IN (6, 8, 11);
Output
name | price | digits_before_point 4K Monitor | 329 | 3 Coffee Mug | 12 | 2 Rubber Duck | 5.5 | 1
Run this on your computer — see “Get Started” for setup.
This is one of SQLite's built-in math functions, available since 3.35.0 when SQLite is compiled with
SQLITE_ENABLE_MATH_FUNCTIONS (the default for the sqlite3 command-line tool and most language bindings). The
in-browser SQLite on this site is built without them, so the examples show output from standard SQLite instead.
Version history
| Version | Change |
|---|---|
| SQLite 3.35.0 | Added |
In other languages
LOG()· PostgreSQL
Related
LN()· Return the natural logarithm (base e) of a number. Zero or negative numbers give NULL.POWER()· Raise a number to a power, so power(2, 10) is 1024. pow() is another name for the same function.- SQL Aggregate Functions · tutorial