| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 4 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 |
<link href="sqlite.css" rel="stylesheet"> |
| 6 |
<title>Built-In Mathematical SQL Functions</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.svg" alt="SQLite" border="0"> |
| 13 |
</a> |
| 14 |
<div><!-- IE hack to prevent disappearing logo --></div> |
| 15 |
<div class="tagline desktoponly"> |
| 16 |
Small. Fast. Reliable.<br>Choose any three. |
| 17 |
</div> |
| 18 |
<div class="menu mainmenu"> |
| 19 |
<ul> |
| 20 |
<li><a href="index.html">Home</a> |
| 21 |
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a> |
| 22 |
<li class='wideonly'><a href='about.html'>About</a> |
| 23 |
<li class='desktoponly'><a href="docs.html">Documentation</a> |
| 24 |
<li class='desktoponly'><a href="download.html">Download</a> |
| 25 |
<li class='wideonly'><a href='copyright.html'>License</a> |
| 26 |
<li class='desktoponly'><a href="support.html">Support</a> |
| 27 |
<li class='desktoponly'><a href="prosupport.html">Purchase</a> |
| 28 |
<li class='search' id='search_menubutton'> |
| 29 |
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a> |
| 30 |
</ul> |
| 31 |
</div> |
| 32 |
<div class="menu submenu" id="submenu"> |
| 33 |
<ul> |
| 34 |
<li><a href='about.html'>About</a> |
| 35 |
<li><a href='docs.html'>Documentation</a> |
| 36 |
<li><a href='download.html'>Download</a> |
| 37 |
<li><a href='support.html'>Support</a> |
| 38 |
<li><a href='prosupport.html'>Purchase</a> |
| 39 |
</ul> |
| 40 |
</div> |
| 41 |
<div class="searchmenu" id="searchmenu"> |
| 42 |
<form method="GET" action="search"> |
| 43 |
<select name="s" id="searchtype"> |
| 44 |
<option value="d">Search Documentation</option> |
| 45 |
<option value="c">Search Changelog</option> |
| 46 |
</select> |
| 47 |
<input type="text" name="q" id="searchbox" value=""> |
| 48 |
<input type="submit" value="Go"> |
| 49 |
</form> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<script> |
| 53 |
function toggle_div(nm) { |
| 54 |
var w = document.getElementById(nm); |
| 55 |
if( w.style.display=="block" ){ |
| 56 |
w.style.display = "none"; |
| 57 |
}else{ |
| 58 |
w.style.display = "block"; |
| 59 |
} |
| 60 |
} |
| 61 |
function toggle_search() { |
| 62 |
var w = document.getElementById("searchmenu"); |
| 63 |
if( w.style.display=="block" ){ |
| 64 |
w.style.display = "none"; |
| 65 |
} else { |
| 66 |
w.style.display = "block"; |
| 67 |
setTimeout(function(){ |
| 68 |
document.getElementById("searchbox").focus() |
| 69 |
}, 30); |
| 70 |
} |
| 71 |
} |
| 72 |
function div_off(nm){document.getElementById(nm).style.display="none";} |
| 73 |
window.onbeforeunload = function(e){div_off("submenu");} |
| 74 |
/* Disable the Search feature if we are not operating from CGI, since */ |
| 75 |
/* Search is accomplished using CGI and will not work without it. */ |
| 76 |
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){ |
| 77 |
document.getElementById("search_menubutton").style.display = "none"; |
| 78 |
} |
| 79 |
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */ |
| 80 |
function hideorshow(btn,obj){ |
| 81 |
var x = document.getElementById(obj); |
| 82 |
var b = document.getElementById(btn); |
| 83 |
if( x.style.display!='none' ){ |
| 84 |
x.style.display = 'none'; |
| 85 |
b.innerHTML='show'; |
| 86 |
}else{ |
| 87 |
x.style.display = ''; |
| 88 |
b.innerHTML='hide'; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
var antiRobot = 0; |
| 93 |
function antiRobotGo(){ |
| 94 |
if( antiRobot!=3 ) return; |
| 95 |
antiRobot = 7; |
| 96 |
var j = document.getElementById("mtimelink"); |
| 97 |
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 98 |
} |
| 99 |
function antiRobotDefense(){ |
| 100 |
document.body.onmousedown=function(){ |
| 101 |
antiRobot |= 2; |
| 102 |
antiRobotGo(); |
| 103 |
document.body.onmousedown=null; |
| 104 |
} |
| 105 |
document.body.onmousemove=function(){ |
| 106 |
antiRobot |= 2; |
| 107 |
antiRobotGo(); |
| 108 |
document.body.onmousemove=null; |
| 109 |
} |
| 110 |
setTimeout(function(){ |
| 111 |
antiRobot |= 1; |
| 112 |
antiRobotGo(); |
| 113 |
}, 100) |
| 114 |
antiRobotGo(); |
| 115 |
} |
| 116 |
antiRobotDefense(); |
| 117 |
</script> |
| 118 |
<div class=fancy> |
| 119 |
<div class=nosearch> |
| 120 |
<div class="fancy_title"> |
| 121 |
Built-In Mathematical SQL Functions |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
<h1 id="overview"><span>1. </span>Overview</h1> |
| 130 |
|
| 131 |
<p>The math functions shown below are a subgroup of |
| 132 |
<a href="lang_corefunc.html">scalar functions</a> that are built into the |
| 133 |
<a href="amalgamation.html">SQLite amalgamation source file</a> but are only active |
| 134 |
if the amalgamation is compiled using the |
| 135 |
<a href="compile.html#enable_math_functions">-DSQLITE_ENABLE_MATH_FUNCTIONS</a> compile-time option. |
| 136 |
|
| 137 |
</p><p>The arguments to math functions can be integers, floating-point numbers, |
| 138 |
or strings that look like integers or real numbers. If any argument |
| 139 |
is NULL or a blob or a string that is not readily converted into a number, |
| 140 |
then the function will return NULL. |
| 141 |
These functions also return NULL for domain errors, such as trying to |
| 142 |
take the square root of a negative number, or compute the arccosine of a |
| 143 |
value greater than 1.0 or less than -1.0. |
| 144 |
|
| 145 |
</p><p>The values returned by these functions are often approximations. |
| 146 |
For example, the <a href="lang_mathfunc.html#pi">pi()</a> function returns |
| 147 |
3.141592653589793115997963468544185161590576171875 which |
| 148 |
is about 1.22465e-16 too small, but it is the closest approximation available |
| 149 |
for IEEE754 doubles. |
| 150 |
|
| 151 |
<p> |
| 152 |
<div class='columns' style='columns: 15em auto;'> |
| 153 |
<ul style='padding-top:0;'> |
| 154 |
<li><a href='lang_mathfunc.html#acos'>acos(X)</a></li> |
| 155 |
<li><a href='lang_mathfunc.html#acosh'>acosh(X)</a></li> |
| 156 |
<li><a href='lang_mathfunc.html#asin'>asin(X)</a></li> |
| 157 |
<li><a href='lang_mathfunc.html#asinh'>asinh(X)</a></li> |
| 158 |
<li><a href='lang_mathfunc.html#atan'>atan(X)</a></li> |
| 159 |
<li><a href='lang_mathfunc.html#atan2'>atan2(Y,X)</a></li> |
| 160 |
<li><a href='lang_mathfunc.html#atanh'>atanh(X)</a></li> |
| 161 |
<li><a href='lang_mathfunc.html#ceil'>ceil(X)</a></li> |
| 162 |
<li><a href='lang_mathfunc.html#ceil'>ceiling(X)</a></li> |
| 163 |
<li><a href='lang_mathfunc.html#cos'>cos(X)</a></li> |
| 164 |
<li><a href='lang_mathfunc.html#cosh'>cosh(X)</a></li> |
| 165 |
<li><a href='lang_mathfunc.html#degrees'>degrees(X)</a></li> |
| 166 |
<li><a href='lang_mathfunc.html#exp'>exp(X)</a></li> |
| 167 |
<li><a href='lang_mathfunc.html#floor'>floor(X)</a></li> |
| 168 |
<li><a href='lang_mathfunc.html#ln'>ln(X)</a></li> |
| 169 |
<li><a href='lang_mathfunc.html#log'>log(B,X)</a></li> |
| 170 |
<li><a href='lang_mathfunc.html#log'>log(X)</a></li> |
| 171 |
<li><a href='lang_mathfunc.html#log'>log10(X)</a></li> |
| 172 |
<li><a href='lang_mathfunc.html#log2'>log2(X)</a></li> |
| 173 |
<li><a href='lang_mathfunc.html#mod'>mod(X,Y)</a></li> |
| 174 |
<li><a href='lang_mathfunc.html#pi'>pi()</a></li> |
| 175 |
<li><a href='lang_mathfunc.html#pow'>pow(X,Y)</a></li> |
| 176 |
<li><a href='lang_mathfunc.html#pow'>power(X,Y)</a></li> |
| 177 |
<li><a href='lang_mathfunc.html#radians'>radians(X)</a></li> |
| 178 |
<li><a href='lang_mathfunc.html#sin'>sin(X)</a></li> |
| 179 |
<li><a href='lang_mathfunc.html#sinh'>sinh(X)</a></li> |
| 180 |
<li><a href='lang_mathfunc.html#sqrt'>sqrt(X)</a></li> |
| 181 |
<li><a href='lang_mathfunc.html#tan'>tan(X)</a></li> |
| 182 |
<li><a href='lang_mathfunc.html#tanh'>tanh(X)</a></li> |
| 183 |
<li><a href='lang_mathfunc.html#trunc'>trunc(X)</a></li> |
| 184 |
</ul> |
| 185 |
</div> |
| 186 |
|
| 187 |
|
| 188 |
</p><h1 id="descriptions_of_built_in_scalar_sql_math_functions"><span>2. </span>Descriptions of built-in scalar SQL math functions</h1> |
| 189 |
<dl> |
| 190 |
|
| 191 |
<a name="acos"></a> |
| 192 |
<dt><p><b>acos(<i>X</i>)</b></dt><dd><p> |
| 193 |
Return the arccosine of X. The result is in radians. |
| 194 |
</dd> |
| 195 |
<a name="acosh"></a> |
| 196 |
<dt><p><b>acosh(<i>X</i>)</b></dt><dd><p> |
| 197 |
Return the hyperbolic arccosine of X. |
| 198 |
</dd> |
| 199 |
<a name="asin"></a> |
| 200 |
<dt><p><b>asin(<i>X</i>)</b></dt><dd><p> |
| 201 |
Return the arcsine of X. The result is in radians. |
| 202 |
</dd> |
| 203 |
<a name="asinh"></a> |
| 204 |
<dt><p><b>asinh(<i>X</i>)</b></dt><dd><p> |
| 205 |
Return the hyperbolic arcsine of X. |
| 206 |
</dd> |
| 207 |
<a name="atan"></a> |
| 208 |
<dt><p><b>atan(<i>X</i>)</b></dt><dd><p> |
| 209 |
Return the arctangent of X. The result is in radians. |
| 210 |
</dd> |
| 211 |
<a name="atan2"></a> |
| 212 |
<dt><p><b>atan2(<i>Y</i>,<i>X</i>)</b></dt><dd><p> |
| 213 |
Return the arctangent of Y/X. The result is in radians. The |
| 214 |
result is placed into correct quadrant depending on the signs |
| 215 |
of X and Y. |
| 216 |
</dd> |
| 217 |
<a name="atanh"></a> |
| 218 |
<dt><p><b>atanh(<i>X</i>)</b></dt><dd><p> |
| 219 |
Return the hyperbolic arctangent of X. |
| 220 |
</dd> |
| 221 |
<a name="ceil"></a> |
| 222 |
<dt><p><b>ceil(<i>X</i>)<br />ceiling(<i>X</i>)</b></dt><dd><p> |
| 223 |
Return the first representable integer value greater than or equal to X. |
| 224 |
For positive values of X, this routine rounds away from zero. |
| 225 |
For negative values of X, this routine rounds toward zero. |
| 226 |
</dd> |
| 227 |
<a name="cos"></a> |
| 228 |
<dt><p><b>cos(<i>X</i>)</b></dt><dd><p> |
| 229 |
Return the cosine of X. X is in radians. |
| 230 |
</dd> |
| 231 |
<a name="cosh"></a> |
| 232 |
<dt><p><b>cosh(<i>X</i>)</b></dt><dd><p> |
| 233 |
Return the hyperbolic cosine of X. |
| 234 |
</dd> |
| 235 |
<a name="degrees"></a> |
| 236 |
<dt><p><b>degrees(<i>X</i>)</b></dt><dd><p> |
| 237 |
Convert value X from radians into degrees. |
| 238 |
</dd> |
| 239 |
<a name="exp"></a> |
| 240 |
<dt><p><b>exp(<i>X</i>)</b></dt><dd><p> |
| 241 |
Compute <i>e</i> (Euler's number, approximately 2.71828182845905) raised |
| 242 |
to the power X. |
| 243 |
</dd> |
| 244 |
<a name="floor"></a> |
| 245 |
<dt><p><b>floor(<i>X</i>)</b></dt><dd><p> |
| 246 |
Return the first representable integer value less than or equal to X. |
| 247 |
For positive numbers, this function rounds toward zero. |
| 248 |
For negative numbers, this function rounds away from zero. |
| 249 |
</dd> |
| 250 |
<a name="ln"></a> |
| 251 |
<dt><p><b>ln(<i>X</i>)</b></dt><dd><p> |
| 252 |
Return the natural logarithm of X. |
| 253 |
</dd> |
| 254 |
<a name="log"></a> |
| 255 |
<dt><p><b>log(<i>X</i>)<br />log10(<i>X</i>)<br />log(<i>B</i>,<i>X</i>)</b></dt><dd><p> |
| 256 |
Return the base-10 logarithm for X. Or, for the two-argument version, |
| 257 |
return the base-B logarithm of X. |
| 258 |
<p> |
| 259 |
Compatibility note: SQLite works like PostgreSQL in that the log() function |
| 260 |
computes a base-10 logarithm. Most other SQL database engines compute a |
| 261 |
natural logarithm for log(). In the two-argument version of log(B,X), the |
| 262 |
first argument is the base and the second argument is the operand. This is |
| 263 |
the same as in PostgreSQL and MySQL, but is reversed from SQL Server which |
| 264 |
uses the second argument as the base and the first argument as the operand. |
| 265 |
</dd> |
| 266 |
<a name="log2"></a> |
| 267 |
<dt><p><b>log2(<i>X</i>)</b></dt><dd><p> |
| 268 |
Return the logarithm base-2 for the number X. |
| 269 |
</dd> |
| 270 |
<a name="mod"></a> |
| 271 |
<dt><p><b>mod(<i>X</i>,<i>Y</i>)</b></dt><dd><p> |
| 272 |
Return the remainder after dividing X by Y. This is similar to the '%' |
| 273 |
operator, except that it works for non-integer arguments. |
| 274 |
</dd> |
| 275 |
<a name="pi"></a> |
| 276 |
<dt><p><b>pi()</b></dt><dd><p> |
| 277 |
Return an approximation for π. |
| 278 |
</dd> |
| 279 |
<a name="pow"></a> |
| 280 |
<dt><p><b>pow(<i>X</i>,<i>Y</i>)<br />power(<i>X</i>,<i>Y</i>)</b></dt><dd><p> |
| 281 |
Compute X raised to the power Y. |
| 282 |
</dd> |
| 283 |
<a name="radians"></a> |
| 284 |
<dt><p><b>radians(<i>X</i>)</b></dt><dd><p> |
| 285 |
Convert X from degrees into radians. |
| 286 |
</dd> |
| 287 |
<a name="sin"></a> |
| 288 |
<dt><p><b>sin(<i>X</i>)</b></dt><dd><p> |
| 289 |
Return the sine of X. X is in radians. |
| 290 |
</dd> |
| 291 |
<a name="sinh"></a> |
| 292 |
<dt><p><b>sinh(<i>X</i>)</b></dt><dd><p> |
| 293 |
Return the hyperbolic sine of X. |
| 294 |
</dd> |
| 295 |
<a name="sqrt"></a> |
| 296 |
<dt><p><b>sqrt(<i>X</i>)</b></dt><dd><p> |
| 297 |
Return the square root of X. NULL is returned if X is negative. |
| 298 |
</dd> |
| 299 |
<a name="tan"></a> |
| 300 |
<dt><p><b>tan(<i>X</i>)</b></dt><dd><p> |
| 301 |
Return the tangent of X. X is in radians. |
| 302 |
</dd> |
| 303 |
<a name="tanh"></a> |
| 304 |
<dt><p><b>tanh(<i>X</i>)</b></dt><dd><p> |
| 305 |
Return the hyperbolic tangent of X. |
| 306 |
</dd> |
| 307 |
<a name="trunc"></a> |
| 308 |
<dt><p><b>trunc(<i>X</i>)</b></dt><dd><p> |
| 309 |
Return the representable integer in between X and 0 (inclusive) |
| 310 |
that is furthest away from zero. Or, in other words, return the |
| 311 |
integer part of X, rounding toward zero. |
| 312 |
The trunc() function is similar to <a href="lang_mathfunc.html#ceil">ceiling(X)</a> and <a href="lang_mathfunc.html#floor">floor(X)</a> except |
| 313 |
that it always rounds toward zero whereas ceiling(X) and floor(X) round |
| 314 |
up and down, respectively. |
| 315 |
</dd> |
| 316 |
|
| 317 |
</dl> |
| 318 |
<p align="center"><small><i>This page was last updated on 2026-02-02 11:47:13Z </small></i></p> |
| 319 |
|