SQLite Math Functions
SQLite offers numeric functions for rounding, truncation, powers, roots, and logarithms. Start with the operation you need rather than assuming similarly named functions behave the same way across SQL databases.
Choose the right rounding function
| Function | Behavior | Example |
|---|---|---|
round(X, Y) |
Round to Y decimal places (default 0) |
round(2.7) = 3.0 |
trunc(X) |
Remove the fractional part, toward zero | trunc(2.7) = 2.0 |
ceil(X) |
Round upward to an integer | ceil(2.7) = 3.0 |
floor(X) |
Round downward to an integer | floor(2.7) = 2.0 |
round(X, -1) does not round to tens in SQLite: negative precision is treated as zero. For fixed-width numeric text, use printf() instead of round().
Other math operations
Use abs() for absolute values, sqrt() for square roots, and pow() for powers. The SQLite math functions documentation explains other functions and their availability.
round() is a core SQLite function. Some other functions, including ceil(), floor(), and sqrt(), require SQLite to be built with SQLITE_ENABLE_MATH_FUNCTIONS. Check your application’s SQLite build before relying on them.
Try the SQL examples in SQLite Playground, which runs SQLite locally in your browser.
-
abs
The SQLiteabs()function returns the absolute value of the value specified by the parameter. -
acos
The SQLiteacos()function returns the arc cosine of the specified number. -
acosh
The SQLiteacosh()function returns the inverse hyperbolic cosine of a specified number. -
asin
The SQLiteasin()function returns the arc sine of the specified number. -
asinh
The SQLiteasinh()function returns the inverse hyperbolic sine of the specified number. -
atan
The SQLiteatan()function returns the arctangent of the specified number. -
atan2
The SQLiteatan2()function returns the arc tangent of the result of dividing the specified two numbers, in radians. -
atanh
The SQLiteatanh()function returns the inverse hyperbolic tangent of the specified number. -
ceil
The SQLiteceil()function returns the smallest integer value greater than or equal to the number specified by the parameter. -
ceiling
The SQLiteceiling()function returns the smallest integer value greater than or equal to the number specified by the parameter. -
cos
The SQLitecos()function returns the cosine of the specified radian. -
cosh
The SQLitecosh()function returns the hyperbolic cosine of the specified number. -
degrees
The SQLitedegrees()function converts the specified radian value to a degree value and returns the result. -
floor
The SQLitefloor()function returns the largest integer value less than or equal to the number specified by the parameter. -
ln
The SQLiteln()function returns the natural logarithm of the number specified by the parameter. -
log
The SQLitelog()function returns the logarithm of the base (10 default) specified by the parameter.parameter. -
log10
The SQLitelog10()function returns the base 10 logarithm for the number specified by the parameter. -
log2
The SQLitelog2()function returns the base 2 logarithm for the number specified by the parameter. -
max(x,y,...)
The SQLitemax(x,y,...)function returns the largest value in the parameter list. -
min(x,y,...)
The SQLitemin(x,y,...)function returns the smallest value in the parameter list. -
mod
The SQLitemod()function returns the remainder after dividing the specified two numbers. -
pi
The SQLitepi()function returns an approximation of π. -
pow
The SQLitepow()function returns a specified number raised to the specified power. -
power
The SQLitepower()function returns a specified number raised to the specified power. -
radians
The SQLiteradians()unction converts the specified value in degrees to radians and returns the result. -
random
The SQLiterandom()function returns a pseudorandom integer between -9223372036854775808 and +9223372036854775807. -
round
Learn SQLite round(X, Y) with tested SQL examples for decimal places, negative precision, NULL, halfway values, and numeric versus formatted text. -
sign
The SQLitesign()function returns the sign of the specified number, and returns1,-1, or0respectively if the parameter is positive, negative, or zero. -
sin
The SQLitesin()function returns the sine of the specified radian. -
sinh
The SQLitesinh()function returns the hyperbolic sine of the specified number. -
sqrt
The SQLitesqrt()function returns the square root of a given number. -
tan
The SQLitetan()function returns the tangent of the specified radian. -
tanh
The SQLitetanh()function returns the hyperbolic tangent of the specified number. -
trunc
The SQLitetrunc()function returns the integer part of the specified number.