lang_corefunc.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: Core Functions</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>Core Functions</h1>
  47. <p>The core functions shown below are available by default.
  48. <a href="lang_datefunc.html">Date &amp; Time functions</a> and
  49. <a href="lang_aggfunc.html">aggregate functions</a> are documented separately. </p>
  50. <table border=0 cellpadding=10>
  51. <tr><td valign="top" align="right" width="120">abs(<i>X</i>)</td><td valign="top"><a name="abs"></a>
  52. Return the absolute value of the numeric
  53. argument <i>X</i>. Return NULL if <i>X</i> is NULL. Return 0.0 if
  54. <i>X</i> is not a numeric value.
  55. </td></tr><tr><td valign="top" align="right" width="120">changes()</td><td valign="top"><a name="changes"></a>
  56. Return the number of database rows that were changed or inserted or
  57. deleted by the most recently complete SQL.
  58. </td></tr><tr><td valign="top" align="right" width="120">coalesce(<i>X</i>,<i>Y</i>,...)</td><td valign="top"><a name="coalesce"></a>
  59. Return a copy of the first non-NULL argument. If
  60. all arguments are NULL then NULL is returned. There must be at least
  61. 2 arguments.
  62. </td></tr><tr><td valign="top" align="right" width="120">glob(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="glob"></a>
  63. This function is used to implement the
  64. "<b>Y GLOB X</b>" syntax of SQLite.
  65. Note that the X and Y arguments are reversed in the glob() function
  66. relative to the infix <a href="lang_expr.html#glob">GLOB</a> operator.
  67. </td>
  68. </td></tr><tr><td valign="top" align="right" width="120">ifnull(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="ifnull"></a>
  69. Return a copy of the first non-NULL argument. If
  70. both arguments are NULL then NULL is returned. The ifnull() functions and
  71. <a href="lang_corefunc.html#coalesce">coalesce()</a> with two arguments are interchangeable.</td>
  72. </td></tr><tr><td valign="top" align="right" width="120">hex(<i>X</i>)</td><td valign="top"><a name="hex"></a>
  73. The argument is interpreted as a BLOB. The result
  74. is a hexadecimal rendering of the content of that blob.
  75. </td></tr><tr><td valign="top" align="right" width="120">last_insert_rowid()</td><td valign="top"><a name="last_insert_rowid"></a>
  76. Return the <a href="lang_createtable.html#rowid">ROWID</a>
  77. of the last row insert from this
  78. connection to the database.
  79. This is the same value that would be returned
  80. from the lastInsertedId() method.
  81. </td></tr><tr><td valign="top" align="right" width="120">length(<i>X</i>)</td><td valign="top"><a name="length"></a>
  82. Return the string length of <i>X</i> in characters if <i>X</i> is a string,
  83. or in bytes if <i>X</i> is a blob.
  84. </td></tr><tr><td valign="top" align="right" width="120">like(<i>X</i>,<i>Y</i>)<br></br>like(<i>X</i>,<i>Y</i>,<i>Z</i>)</td><td valign="top"><a name="like"></a>
  85. This function is used to implement the "<b>Y LIKE X &#91;ESCAPE Z&#93;</b>"
  86. syntax of SQL. If the optional ESCAPE clause is present, then the
  87. user-function is invoked with three arguments. Otherwise, it is
  88. invoked with two arguments only. Note that the X and Y parameters are
  89. reversed in the like() function relative to the infix <a href="lang_expr.html#like">LIKE</a> operator.
  90. The sqlite3_create_function() interface can be used to override this
  91. function and thereby change the operation of the
  92. <a href="lang_expr.html#like">LIKE</a> operator. When doing this, it may be important
  93. to override both the two and three argument versions of the like()
  94. function. Otherwise, different code may be called to implement the
  95. <a href="lang_expr.html#like">LIKE</a> operator depending on whether or not an ESCAPE clause was
  96. specified.
  97. </td></tr><tr><td valign="top" align="right" width="120">load_extension(<i>X</i>)<br></br>load_extension(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="load_extension"></a>
  98. Load SQLite extensions out of the shared library
  99. file named <i>X</i> using the entry point <i>Y</i>. The result
  100. is a NULL. If <i>Y</i> is omitted then the default entry point
  101. of <b>sqlite3_extension_init</b> is used. This function raises
  102. an exception if the extension fails to load or initialize correctly.
  103. <p>This function will fail if the extension attempts to modify
  104. or delete a SQL function or collating sequence. The
  105. extension can add new functions or collating sequences, but cannot
  106. modify or delete existing functions or collating sequences because
  107. those functions and/or collating sequences might be used elsewhere
  108. in the currently running SQL statement. To load an extension that
  109. changes or deletes functions or collating sequences, use the
  110. sqlite3_load_extension() C-language API.</p>
  111. </td></tr><tr><td valign="top" align="right" width="120">lower(<i>X</i>)</td><td valign="top"><a name="lower"></a>
  112. Return a copy of string <i>X</i> with all ASCII characters
  113. converted to lower case. The default built-in lower() function works
  114. for ASCII characters only. To do case conversions on non-ASCII
  115. characters, load the ICU extension.
  116. </td></tr><tr><td valign="top" align="right" width="120">ltrim(<i>X</i>)<br></br>ltrim(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="ltrim"></a>
  117. Return a string formed by removing any and all
  118. characters that appear in <i>Y</i> from the left side of <i>X</i>.
  119. If the <i>Y</i> argument is omitted, spaces are removed.
  120. </td></tr><tr><td valign="top" align="right" width="120">max(<i>X</i>,<i>Y</i>,...)</td><td valign="top"><a name="maxCoreFunc"></a>
  121. Return the argument with the maximum value. Or return NULL if any argument
  122. is NULL.
  123. Note that <b>max()</b> is a simple function when
  124. it has 2 or more arguments but converts to an aggregate function if given
  125. only a single argument.
  126. </td></tr><tr><td valign="top" align="right" width="120">min(<i>X</i>,<i>Y</i>,...)</td><td valign="top"><a name="minCoreFunc"></a>
  127. Return the argument with the minimum value.
  128. Note that <b>min()</b> is a simple function when
  129. it has 2 or more arguments but converts to an aggregate function if given
  130. only a single argument.
  131. </td></tr><tr><td valign="top" align="right" width="120">nullif(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="nullif"></a>
  132. Return the first argument if the arguments are different,
  133. otherwise return NULL.
  134. </td></tr><tr><td valign="top" align="right" width="120">quote(<i>X</i>)</td><td valign="top"><a name="quote"></a>
  135. This routine returns a string which is the value of
  136. its argument suitable for inclusion into another SQL statement.
  137. Strings are surrounded by single-quotes with escapes on interior quotes
  138. as needed. BLOBs are encoded as hexadecimal literals.
  139. SQLite uses this function internally in its implementation of <a href="lang_vacuum.html">VACUUM</a>
  140. so if this function is overloading to provide incompatible behavior, the
  141. <a href="lang_vacuum.html">VACUUM</a> command will likely cease to work.
  142. </td></tr><tr><td valign="top" align="right" width="120">random()</td><td valign="top"><a name="random"></a>
  143. Return a pseudo-random integer
  144. between -9223372036854775808 and +9223372036854775807.
  145. </td></tr><tr><td valign="top" align="right" width="120">randomblob(<i>N</i>)</td><td valign="top"><a name="randomblob"></a>
  146. Return an <i>N</i>-byte blob containing pseudo-random bytes.
  147. <i>N</i> should be a postive integer.
  148. <p>Hint: applications can generate globally unique identifiers
  149. using this function together with <a href="lang_corefunc.html#hex">hex()</a> and/or
  150. <a href="lang_corefunc.html#lower">lower()</a> like this:</p>
  151. <blockquote>
  152. hex(randomblob(16))<br></br>
  153. lower(hex(randomblob(16)))
  154. </blockquote>
  155. </td></tr><tr><td valign="top" align="right" width="120">replace(<i>X</i>,<i>Y</i>,<i>Z</i>)</td><td valign="top"><a name="replace"></a>
  156. Return a string formed by substituting string <i>Z</i> for
  157. every occurrance of string <i>Y</i> in string <i>X</i>. The <a href="datatype3.html#collation">BINARY</a>
  158. collating sequence is used for comparisons. If <i>Y</i> is an empty
  159. string then return <i>X</i> unchanged.
  160. </td></tr><tr><td valign="top" align="right" width="120">round(<i>X</i>)<br></br>round(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="round"></a>
  161. Round off the number <i>X</i> to <i>Y</i> digits to the
  162. right of the decimal point. If the <i>Y</i> argument is omitted, 0 is
  163. assumed.
  164. </td></tr><tr><td valign="top" align="right" width="120">rtrim(<i>X</i>)<br></br>rtrim(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="rtrim"></a>
  165. Return a string formed by removing any and all
  166. characters that appear in <i>Y</i> from the right side of <i>X</i>.
  167. If the <i>Y</i> argument is omitted, spaces are removed.
  168. </td></tr><tr><td valign="top" align="right" width="120">soundex(<i>X</i>)</td><td valign="top"><a name="soundex"></a>
  169. Compute the soundex encoding of the string <i>X</i>.
  170. The string "?000" is returned if the argument is NULL.
  171. This function is omitted from SQLite by default.
  172. It is only available the -DSQLITE_SOUNDEX=1 compiler option
  173. is used when SQLite is built.
  174. </td></tr><tr><td valign="top" align="right" width="120">sqlite_source_id()</td><td valign="top"><a name="sqlite_source_id"></a>
  175. Return a string that identifies the specific version of the source code
  176. that was used to build the SQLite library. The return string begins with
  177. the date and time that the source code was checked in and is follows by
  178. an SHA1 hash that uniquely identifies the source tree.
  179. </td></tr><tr><td valign="top" align="right" width="120">sqlite_version()</td><td valign="top"><a name="sqlite_version"></a>
  180. Return the version string for the SQLite library
  181. that is running. Example: "3.5.9".
  182. </td></tr><tr><td valign="top" align="right" width="120">substr(<i>X</i>,<i>Y</i>,<i>Z</i>)<br></br>substr(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="substr"></a>
  183. Return a substring of input string <i>X</i> that begins
  184. with the <i>Y</i>-th character and which is <i>Z</i> characters long.
  185. If <i>Z</i> is omitted then all character through the end of the string
  186. are returned.
  187. The left-most character of <i>X</i> is number 1. If <i>Y</i> is negative
  188. the the first character of the substring is found by counting from the
  189. right rather than the left. If <i>X</i> is string
  190. then characters indices refer to actual UTF-8 characters. If
  191. <i>X</i> is a BLOB then the indices refer to bytes.
  192. </td></tr><tr><td valign="top" align="right" width="120">total_changes()</td><td valign="top"><a name="total_changes"></a>
  193. Return the number of row changes caused by INSERT, UPDATE or DELETE
  194. statements since the current database connection was opened.
  195. </td></tr><tr><td valign="top" align="right" width="120">trim(<i>X</i>)<br></br>trim(<i>X</i>,<i>Y</i>)</td><td valign="top"><a name="trim"></a>
  196. Return a string formed by removing any and all
  197. characters that appear in <i>Y</i> from both ends of <i>X</i>.
  198. If the <i>Y</i> argument is omitted, spaces are removed.
  199. </td></tr><tr><td valign="top" align="right" width="120">typeof(<i>X</i>)</td><td valign="top"><a name="typeof"></a>
  200. Return the <a href="datatype3.html">datatype</a> of the expression <i>X</i>. The only
  201. return values are "null", "integer", "real", "text", and "blob".
  202. </td></tr><tr><td valign="top" align="right" width="120">upper(<i>X</i>)</td><td valign="top"><a name="upper"></a>
  203. Return a copy of input string <i>X</i> converted to all
  204. upper-case letters. The implementation of this function uses the C library
  205. routine <b>toupper()</b> which means it may not work correctly on
  206. non-ASCII UTF-8 strings.
  207. </td></tr><tr><td valign="top" align="right" width="120">zeroblob(<i>N</i>)</td><td valign="top"><a name="zeroblob"></a>
  208. Return a BLOB consisting of N bytes of 0x00. SQLite
  209. manages these zeroblobs very efficiently. Zeroblobs can be used to
  210. reserve space for a BLOB that is later written using
  211. incremental BLOB I/O.
  212. </td></tr>
  213. </table>