Scalar Functions
Scalar functions accept one or more arguments and return a single value. They can be used anywhere an expression is valid: SELECT columns, WHERE conditions, ORDER BY, GROUP BY, HAVING, CHECK constraints, and DEFAULT values.Function Reference
Math Functions
String Functions
Conditional Functions
Type Functions
Pattern Matching Functions
Optimizer Hints
Blob Functions
System Functions
Detailed Descriptions and Examples
abs(X)
Returns the absolute value of X. The return type matches the input: INTEGER for integer inputs, REAL for floating-point inputs. Returns NULL if X is NULL. Returns INTEGER for a text value that looks like an integer.char(X1, X2, …, XN)
Returns a string composed of characters having the Unicode code points X1 through XN. Arguments that are not valid code points are replaced with the Unicode replacement character (U+FFFD).coalesce(X, Y, …)
Returns the first argument that is not NULL. If all arguments are NULL, returns NULL. Requires at least two arguments.concat(X, …) and concat_ws(SEP, X, …)
concat joins all arguments as strings, skipping NULLs. concat_ws inserts the separator between non-NULL arguments.
format(FORMAT, …) and printf(FORMAT, …)
Returns a formatted string using printf-style format specifiers.printf is an alias for format.
glob(X, Y)
Returns 1 if string Y matches the glob pattern X, and 0 otherwise. Glob matching is case-sensitive and uses* for any sequence of characters, ? for any single character, and [...] for character classes.
The
glob(X, Y) function is the functional form of the Y GLOB X operator. Note the reversed argument order compared to the operator syntax.hex(X) and unhex(X)
hex returns the uppercase hexadecimal representation of its argument. For text, it returns the hex encoding of the UTF-8 bytes. For blobs, it encodes each byte. For integers, it returns the hex of the value.
unhex converts a hexadecimal string back to a blob. Returns NULL if the input contains non-hex characters, unless a second argument specifies characters to ignore.
iif(X, Y, Z) and if(X, Y, Z)
Returns Y if X is true (non-zero and non-NULL), otherwise returns Z.if is an alias for iif.
instr(X, Y)
Returns the 1-based position of the first occurrence of string Y in string X. Returns 0 if Y is not found in X. If either argument is NULL, returns NULL.length(X) and octet_length(X)
length returns the number of characters in a text value, or the number of bytes in a blob value. For NULL, returns NULL. For numeric values, returns the length of the text representation.
octet_length always returns the length in bytes, regardless of type.
like(X, Y) and like(X, Y, Z)
Returns 1 if string Y matches LIKE pattern X, and 0 otherwise.% matches any sequence of characters, _ matches any single character. Matching is case-insensitive for ASCII characters. The optional third argument Z specifies an escape character.
The
like(X, Y) function is the functional form of the Y LIKE X operator. Note the reversed argument order compared to the operator syntax.lower(X), upper(X)
lower returns a copy of string X with all ASCII characters converted to lowercase. upper converts to uppercase.
ltrim(X), rtrim(X), trim(X)
These functions remove characters from the ends of a string. Without a second argument, they remove whitespace. With a second argument Y, they remove any characters present in the string Y.max(X, Y, …) and min(X, Y, …)
The multi-argument forms ofmax and min return the largest or smallest argument, respectively. Arguments are compared using the standard SQLite comparison rules. If any argument is NULL, the result is NULL.
The multi-argument
max() and min() are scalar functions. When called with a single argument inside an aggregate query (e.g., SELECT max(salary) FROM employees), they act as aggregate functions.nullif(X, Y)
Returns NULL if X equals Y, otherwise returns X. This is useful for converting sentinel values to NULL.quote(X)
Returns the text of an SQL literal that represents the value X. Strings are enclosed in single quotes with escaping. BLOBs are encoded as hex literals. NULL returns the string'NULL'. Numbers are returned as-is.
random() and randomblob(N)
random returns a pseudo-random 64-bit signed integer. randomblob returns a blob of N pseudo-random bytes.
replace(X, Y, Z)
Returns a copy of string X with every occurrence of string Y replaced by string Z. If Y is empty, X is returned unchanged.round(X) and round(X, Y)
Rounds X to Y decimal places. If Y is omitted, it defaults to 0. The return type is always REAL.sign(X)
Returns -1 for negative values, 0 for zero, and 1 for positive values. Returns NULL if X is NULL.substr(X, Y) and substr(X, Y, Z)
Returns a substring of X starting at the Y-th character (1-based). If Z is provided, the substring is at most Z characters long. Negative Y counts from the end of the string.substring is an alias.
typeof(X)
Returns the storage class of X as a lowercase string:"null", "integer", "real", "text", or "blob".
unicode(X)
Returns the Unicode code point of the first character of string X. Returns NULL if X is NULL or an empty string.soundex(X)
Returns the Soundex encoding of string X as a four-character code. Soundex encodes a string based on how it sounds in English, which is useful for fuzzy name matching.zeroblob(N)
Returns a blob consisting of N zero bytes (0x00). Useful for pre-allocating blob storage.last_insert_rowid()
Returns the rowid of the most recent successful INSERT on the current database connection. Returns 0 if no INSERT has been performed.changes() and total_changes()
changes returns the number of rows modified by the most recent INSERT, UPDATE, or DELETE statement. total_changes returns the total number of rows modified since the database connection was opened.
sqlite_version()
Returns the SQLite-compatible version string.load_extension(X)
Loads a Turso-native extension from the shared library at path X.Extension loading must be enabled on the database connection. See the Extensions documentation for details on building and loading extensions.
See Also
- Aggregate Functions for functions that operate across groups of rows
- Expressions for operator syntax, CAST, CASE, and subqueries
- Data Types for storage classes and type affinity
- SELECT for using functions in queries