Built-In Scalar SQL Functions

The core functions shown below are available by default. Date & Time functions, aggregate functions, window functions, math functions, and JSON functions are documented separately. An application may define additional functions written in C and added to the database engine using the sqlite3_create_function() API.

- abs(X)
- changes()
- char(X1,X2,...,XN)
- coalesce(X,Y,...)
- format(FORMAT,...)
- glob(X,Y)
- hex(X)
- ifnull(X,Y)
- iif(X,Y,Z)
- instr(X,Y)
- last_insert_rowid()
- length(X)
- like(X,Y)
- like(X,Y,Z)
- likelihood(X,Y)
- likely(X)
- load_extension(X)
- load_extension(X,Y)
- lower(X)
- ltrim(X)
- ltrim(X,Y)
- max(X,Y,...)
- min(X,Y,...)
- nullif(X,Y)
- printf(FORMAT,...)
- quote(X)
- random()
- randomblob(N)
- replace(X,Y,Z)
- round(X)
- round(X,Y)
- rtrim(X)
- rtrim(X,Y)
- sign(X)
- soundex(X)
- sqlite_compileoption_get(N)
- sqlite_compileoption_used(X)
- sqlite_offset(X)
- sqlite_source_id()
- sqlite_version()
- substr(X,Y)
- substr(X,Y,Z)
- substring(X,Y)
- substring(X,Y,Z)
- total_changes()
- trim(X)
- trim(X,Y)
- typeof(X)
- unhex(X)
- unhex(X,Y)
- unicode(X)
- unlikely(X)
- upper(X)
- zeroblob(N)

**abs(***X*)**R-23979-26855:[**The abs(X) function returns the absolute value of the numeric argument X.**]****R-37434-19929:[**Abs(X) returns NULL if X is NULL.**]****R-01992-00519:[**Abs(X) returns 0.0 if X is a string or blob that cannot be converted to a numeric value.**]****R-31676-45509:[**If X is the integer -9223372036854775808 then abs(X) throws an integer overflow error since there is no equivalent positive 64-bit two complement value.**]****changes()****R-58361-29089:[**The changes() function returns the number of database rows that were changed or inserted or deleted by the most recently completed INSERT, DELETE, or UPDATE statement, exclusive of statements in lower-level triggers.**]****R-32760-32347:[**The changes() SQL function is a wrapper around the sqlite3_changes64() C/C++ function and hence follows the same rules for counting changes.**]****char(***X1*,*X2*,...,*XN*)**R-55469-62130:[**The char(X1,X2,...,XN) function returns a string composed of characters having the unicode code point values of integers X1 through XN, respectively.**]****coalesce(***X*,*Y*,...)**R-22655-13879:[**The coalesce() function returns a copy of its first non-NULL argument, or NULL if all arguments are NULL.**]****R-48353-58021:[**Coalesce() must have at least 2 arguments.**]****format(***FORMAT*,...)**R-32560-14372:[**The format(FORMAT,...) SQL function works like the sqlite3_mprintf() C-language function and the printf() function from the standard C library.**]**The first argument is a format string that specifies how to construct the output string using values taken from subsequent arguments.**R-40086-60101:[**If the FORMAT argument is missing or NULL then the result is NULL.**]****R-02347-27622:[**The %n format is silently ignored and does not consume an argument.**]****R-56064-04001:[**The %p format is an alias for %X.**]****R-17002-27534:[**The %z format is interchangeable with %s.**]****R-29410-53018:[**If there are too few arguments in the argument list, missing arguments are assumed to have a NULL value, which is translated into 0 or 0.0 for numeric formats or an empty string for %s.**]**See the built-in printf() documentation for additional information.**glob(***X*,*Y*)**R-61376-43478:[**The glob(X,Y) function is equivalent to the expression "**Y GLOB X**".**]**Note that the X and Y arguments are reversed in the glob() function relative to the infix GLOB operator. Y is the string and X is the pattern. So, for example, the following expressions are equivalent:name GLOB '*helium*' glob('*helium*',name)

**R-45676-18204:[**If the sqlite3_create_function() interface is used to override the glob(X,Y) function with an alternative implementation then the GLOB operator will invoke the alternative implementation.**]****hex(***X*)**R-57089-20897:[**The hex() function interprets its argument as a BLOB and returns a string which is the upper-case hexadecimal rendering of the content of that blob.**]**If the argument

*X*in "hex(*X*)" is an integer or floating point number, then "interprets its argument as a BLOB" means that the binary number is first converted into a UTF8 text representation, then that text is interpreted as a BLOB. Hence, "hex(12345678)" renders as "3132333435363738" not the binary representation of the integer value "0000000000BC614E".See also: unhex()

**ifnull(***X*,*Y*)**R-54671-04027:[**The ifnull() function returns a copy of its first non-NULL argument, or NULL if both arguments are NULL.**]****R-35443-03595:[**Ifnull() must have exactly 2 arguments.**]****R-47349-36088:[**The ifnull() function is equivalent to coalesce() with two arguments.**]****iif(***X*,*Y*,*Z*)**R-03172-04369:[**The iif(X,Y,Z) function returns the value Y if X is true, and Z otherwise.**]****R-56368-08028:[**The iif(X,Y,Z) function is logically equivalent to and generates the same bytecode as the CASE expression "CASE WHEN X THEN Y ELSE Z END".**]****instr(***X*,*Y*)**R-27549-59611:[**The instr(X,Y) function finds the first occurrence of string Y within string X and returns the number of prior characters plus 1, or 0 if Y is nowhere found within X.**]****R-46421-32541:[**Or, if X and Y are both BLOBs, then instr(X,Y) returns one more than the number bytes prior to the first occurrence of Y, or 0 if Y does not occur anywhere within X.**]****R-17329-35644:[**If both arguments X and Y to instr(X,Y) are non-NULL and are not BLOBs then both are interpreted as strings.**]****R-14708-27487:[**If either X or Y are NULL in instr(X,Y) then the result is NULL.**]****last_insert_rowid()****R-14875-52970:[**The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function.**]****R-51513-12026:[**The last_insert_rowid() SQL function is a wrapper around the sqlite3_last_insert_rowid() C/C++ interface function.**]****length(***X*)**R-19393-22085:[**For a string value X, the length(X) function returns the number of characters (not bytes) in X prior to the first NUL character.**]**Since SQLite strings do not normally contain NUL characters, the length(X) function will usually return the total number of characters in the string X.**R-29691-21352:[**For a blob value X, length(X) returns the number of bytes in the blob.**]****R-06121-44139:[**If X is NULL then length(X) is NULL.**]****R-35003-32738:[**If X is numeric then length(X) returns the length of a string representation of X.**]**Note that for strings, the length(X) function returns the

*character*length of the string, not the byte length. The character length is the number of characters in the string. The character length is always different from the byte length for UTF-16 strings, and can be different from the byte length for UTF-8 strings if the string contains multi-byte characters.For BLOB values, length(X) always returns the byte-length of the BLOB.

For string values, length(X) must read the entire string into memory in order to compute the character length. But for BLOB values, that is not necessary as SQLite knows how many bytes are in the BLOB. Hence, for multi-megabyte values, the length(X) function is usually much faster for BLOBs than for strings, since it does not need to load the value into memory.

**like(***X*,*Y*)

like(*X*,*Y*,*Z*)**R-47394-08393:[**The like() function is used to implement the "**Y LIKE X [ESCAPE Z]**" expression.**]****R-38973-52184:[**If the optional ESCAPE clause is present, then the like() function is invoked with three arguments.**]****R-24738-46982:[**Otherwise, it is invoked with two arguments only.**]**Note that the X and Y parameters are reversed in the like() function relative to the infix LIKE operator. X is the pattern and Y is the string to match against that pattern. Hence, the following expressions are equivalent:name LIKE '%neon%' like('%neon%',name)

**R-29639-49338:[**The sqlite3_create_function() interface can be used to override the like() function and thereby change the operation of the LIKE operator.**]**When overriding the like() function, it may be important to override both the two and three argument versions of the like() function. Otherwise, different code may be called to implement the LIKE operator depending on whether or not an ESCAPE clause was specified.**likelihood(***X*,*Y*)**R-41921-05214:[**The likelihood(X,Y) function returns argument X unchanged.**]****R-44133-61651:[**The value Y in likelihood(X,Y) must be a floating point constant between 0.0 and 1.0, inclusive.**]****R-28535-44631:[**The likelihood(X) function is a no-op that the code generator optimizes away so that it consumes no CPU cycles during run-time (that is, during calls to sqlite3_step()).**]****R-31509-40414:[**The purpose of the likelihood(X,Y) function is to provide a hint to the query planner that the argument X is a boolean that is true with a probability of approximately Y.**]****R-01283-11636:[**The unlikely(X) function is short-hand for likelihood(X,0.0625).**]****R-36850-34127:[**The likely(X) function is short-hand for likelihood(X,0.9375).**]****likely(***X*)**R-23735-03107:[**The likely(X) function returns the argument X unchanged.**]****R-43464-09689:[**The likely(X) function is a no-op that the code generator optimizes away so that it consumes no CPU cycles at run-time (that is, during calls to sqlite3_step()).**]****R-44492-58344:[**The purpose of the likely(X) function is to provide a hint to the query planner that the argument X is a boolean value that is usually true.**]****R-53436-40973:[**The likely(X) function is equivalent to likelihood(X,0.9375).**]**See also: unlikely(X).**load_extension(***X*)

load_extension(*X*,*Y*)**R-63519-36751:[**The load_extension(X,Y) function loads SQLite extensions out of the shared library file named X using the entry point Y.**]****R-01265-50598:[**The result of load_extension() is always a NULL.**]****R-09261-46339:[**If Y is omitted then the default entry point name is used.**]****R-26578-03501:[**The load_extension() function raises an exception if the extension fails to load or initialize correctly.**]****R-30224-53949:[**The load_extension() function will fail if the extension attempts to modify or delete an SQL function or collating sequence.**]****R-16057-55834:[**The extension can add new functions or collating sequences, but cannot modify or delete existing functions or collating sequences because those functions and/or collating sequences might be used elsewhere in the currently running SQL statement.**]**To load an extension that changes or deletes functions or collating sequences, use the sqlite3_load_extension() C-language API.For security reasons, extension loading is disabled by default and must be enabled by a prior call to sqlite3_enable_load_extension().

**lower(***X*)**R-31312-33516:[**The lower(X) function returns a copy of string X with all ASCII characters converted to lower case.**]****R-31442-26915:[**The default built-in lower() function works for ASCII characters only.**]**To do case conversions on non-ASCII characters, load the ICU extension.**ltrim(***X*)

ltrim(*X*,*Y*)**R-64249-42392:[**The ltrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the left side of X.**]****R-58421-05800:[**If the Y argument is omitted, ltrim(X) removes spaces from the left side of X.**]****max(***X*,*Y*,...)**R-60099-10331:[**The multi-argument max() function returns the argument with the maximum value, or return NULL if any argument is NULL.**]****R-41246-06467:[**The multi-argument max() function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons.**]****R-63138-42446:[**If none of the arguments to max() define a collating function, then the BINARY collating function is used.**]****R-01544-29257:[**Note that**max()**is a simple function when it has 2 or more arguments but operates as an aggregate function if given only a single argument.**]****min(***X*,*Y*,...)**R-02811-30532:[**The multi-argument min() function returns the argument with the minimum value.**]****R-17066-08509:[**The multi-argument min() function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons.**]****R-47421-29468:[**If none of the arguments to min() define a collating function, then the BINARY collating function is used.**]****R-46612-63606:[**Note that**min()**is a simple function when it has 2 or more arguments but operates as an aggregate function if given only a single argument.**]****nullif(***X*,*Y*)**R-29649-49812:[**The nullif(X,Y) function returns its first argument if the arguments are different and NULL if the arguments are the same.**]****R-64107-03133:[**The nullif(X,Y) function searches its arguments from left to right for an argument that defines a collating function and uses that collating function for all string comparisons.**]****R-29935-55502:[**If neither argument to nullif() defines a collating function then the BINARY collating function is used.**]****printf(***FORMAT*,...)**R-64900-53159:[**The printf() SQL function is an alias for the format() SQL function.**]**The format() SQL function was original named printf(). But the name was later changed to format() for compatibility with other database engines. The original printf() name is retained as an alias so as not to break any legacy code.**quote(***X*)**R-38536-04510:[**The quote(X) function returns the text of an SQL literal which is the value of its argument suitable for inclusion into an SQL statement.**]****R-48019-29352:[**Strings are surrounded by single-quotes with escapes on interior quotes as needed.**]****R-60735-46752:[**BLOBs are encoded as hexadecimal literals.**]****R-36754-20545:[**Strings with embedded NUL characters cannot be represented as string literals in SQL and hence the returned string literal is truncated prior to the first NUL.**]****random()****R-13846-35797:[**The random() function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.**]****randomblob(***N*)**R-45939-02717:[**The randomblob(N) function return an N-byte blob containing pseudo-random bytes.**]****R-11061-58747:[**If N is less than 1 then a 1-byte random blob is returned.**]**Hint: applications can generate globally unique identifiers using this function together with hex() and/or lower() like this:

hex(randomblob(16))

lower(hex(randomblob(16)))**replace(***X*,*Y*,*Z*)**R-21182-58169:[**The replace(X,Y,Z) function returns a string formed by substituting string Z for every occurrence of string Y in string X.**]****R-24454-61742:[**The BINARY collating sequence is used for comparisons.**]****R-50678-57776:[**If Y is an empty string then return X unchanged.**]****R-42393-33810:[**If Z is not initially a string, it is cast to a UTF-8 string prior to processing.**]****round(***X*)

round(*X*,*Y*)**R-10715-11594:[**The round(X,Y) function returns a floating-point value X rounded to Y digits to the right of the decimal point.**]****R-03483-52769:[**If the Y argument is omitted or negative, it is taken to be 0.**]****rtrim(***X*)

rtrim(*X*,*Y*)**R-23317-43079:[**The rtrim(X,Y) function returns a string formed by removing any and all characters that appear in Y from the right side of X.**]****R-58312-28618:[**If the Y argument is omitted, rtrim(X) removes spaces from the right side of X.**]****sign(***X*)**R-03595-19994:[**The sign(X) function returns -1, 0, or +1 if the argument X is a numeric value that is negative, zero, or positive, respectively.**]****R-61228-42590:[**If the argument to sign(X) is NULL or is a string or blob that cannot be losslessly converted into a number, then sign(X) returns NULL.**]****soundex(***X*)**R-59782-00072:[**The soundex(X) function returns a string that is the soundex encoding of the string X.**]****R-64894-50321:[**The string "?000" is returned if the argument is NULL or contains no ASCII alphabetic characters.**]****R-25361-16150:[**This function is omitted from SQLite by default. It is only available if the SQLITE_SOUNDEX compile-time option is used when SQLite is built.**]****sqlite_compileoption_get(***N*)**R-04922-24076:[**The sqlite_compileoption_get() SQL function is a wrapper around the sqlite3_compileoption_get() C/C++ function.**]****R-43217-29904:[**This routine returns the N-th compile-time option used to build SQLite or NULL if N is out of range.**]**See also the compile_options pragma.**sqlite_compileoption_used(***X*)**R-39564-36305:[**The sqlite_compileoption_used() SQL function is a wrapper around the sqlite3_compileoption_used() C/C++ function.**]****R-19404-45868:[**When the argument X to sqlite_compileoption_used(X) is a string which is the name of a compile-time option, this routine returns true (1) or false (0) depending on whether or not that option was used during the build.**]****sqlite_offset(***X*)**R-62529-02872:[**The sqlite_offset(X) function returns the byte offset in the database file for the beginning of the record from which value would be read.**]**If X is not a column in an ordinary table, then sqlite_offset(X) returns NULL. The value returned by sqlite_offset(X) might reference either the original table or an index, depending on the query. If the value X would normally be extracted from an index, the sqlite_offset(X) returns the offset to the corresponding index record. If the value X would be extracted from the original table, then sqlite_offset(X) returns the offset to the table record.The sqlite_offset(X) SQL function is only available if SQLite is built using the -DSQLITE_ENABLE_OFFSET_SQL_FUNC compile-time option.

**sqlite_source_id()****R-01298-38284:[**The sqlite_source_id() function returns a string that identifies the specific version of the source code that was used to build the SQLite library.**]****R-22138-49689:[**The string returned by sqlite_source_id() is the date and time that the source code was checked in followed by the SHA3-256 hash for that check-in.**]****R-24470-31136:[**This function is an SQL wrapper around the sqlite3_sourceid() C interface.**]****sqlite_version()****R-37910-23168:[**The sqlite_version() function returns the version string for the SQLite library that is running.**]****R-48699-48617:[**This function is an SQL wrapper around the sqlite3_libversion() C-interface.**]****substr(***X*,*Y*,*Z*)

substr(*X*,*Y*)

substring(*X*,*Y*,*Z*)

substring(*X*,*Y*)**R-64130-24369:[**The substr(X,Y,Z) function returns a substring of input string X that begins with the Y-th character and which is Z characters long.**]****R-08267-32299:[**If Z is omitted then substr(X,Y) returns all characters through the end of the string X beginning with the Y-th.**]****R-64707-05329:[**The left-most character of X is number 1.**]****R-34009-51042:[**If Y is negative then the first character of the substring is found by counting from the right rather than the left.**]****R-13337-13961:[**If Z is negative then the abs(Z) characters preceding the Y-th character are returned.**]****R-16262-20798:[**If X is a string then characters indices refer to actual UTF-8 characters.**]****R-22795-48828:[**If X is a BLOB then the indices refer to bytes.**]**"substring()" is an alias for "substr()" beginning with SQLite version 3.34.

**total_changes()****R-38914-26427:[**The total_changes() function returns the number of row changes caused by INSERT, UPDATE or DELETE statements since the current database connection was opened.**]****R-11217-42568:[**This function is a wrapper around the sqlite3_total_changes64() C/C++ interface.**]****trim(***X*)

trim(*X*,*Y*)**R-50629-09283:[**The trim(X,Y) function returns a string formed by removing any and all characters that appear in Y from both ends of X.**]****R-42511-45809:[**If the Y argument is omitted, trim(X) removes spaces from both ends of X.**]****typeof(***X*)**R-62084-05956:[**The typeof(X) function returns a string that indicates the datatype of the expression X: "null", "integer", "real", "text", or "blob".**]****unhex(***X*)

unhex(*X*,*Y*)**R-40637-59981:[**The unhex(X,Y) function returns a BLOB value which is the decoding of the hexadecimal string X.**]****R-45050-32981:[**If X contains any characters that are not hexadecimal digits and which are not in Y, then unhex(X,Y) returns NULL.**]****R-51717-34902:[**If Y is omitted, it is understood to be an empty string and hence X must be a pure hexadecimal string.**]****R-11344-60106:[**All hexadecimal digits in X must occur in pairs, with both digits of each pair beginning immediately adjacent to one another, or else unhex(X,Y) returns NULL.**]****R-32258-04844:[**If either parameter X or Y is NULL, then unhex(X,Y) returns NULL.**]****R-45221-09189:[**The X input may contain an arbitrary mix of upper and lower case hexadecimal digits.**]****R-50173-64790:[**Hexadecimal digits in Y have no affect on the translation of X. Only characters in Y that are not hexadecimal digits are ignored in X.**]**See also: hex()

**unicode(***X*)**R-29701-50711:[**The unicode(X) function returns the numeric unicode code point corresponding to the first character of the string X.**]**If the argument to unicode(X) is not a string then the result is undefined.**unlikely(***X*)**R-11152-23456:[**The unlikely(X) function returns the argument X unchanged.**]****R-22887-63324:[**The unlikely(X) function is a no-op that the code generator optimizes away so that it consumes no CPU cycles at run-time (that is, during calls to sqlite3_step()).**]****R-43820-61705:[**The purpose of the unlikely(X) function is to provide a hint to the query planner that the argument X is a boolean value that is usually not true.**]****R-61304-29449:[**The unlikely(X) function is equivalent to likelihood(X, 0.0625).**]****upper(***X*)**R-35553-21226:[**The upper(X) function returns a copy of input string X in which all lower-case ASCII characters are converted to their upper-case equivalent.**]****zeroblob(***N*)**R-31842-04164:[**The zeroblob(N) function returns a BLOB consisting of N bytes of 0x00.**]**SQLite manages these zeroblobs very efficiently. Zeroblobs can be used to reserve space for a BLOB that is later written using incremental BLOB I/O.**R-00293-64994:[**This SQL function is implemented using the sqlite3_result_zeroblob() routine from the C/C++ interface.**]**

*This page last modified on 2023-01-27 13:42:47 UTC *