Skip to main content

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) and chr(X)

char 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). chr is a single-argument alias provided for PostgreSQL and SQL-standard compatibility. It is identical to char(X).

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) and strpos(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. strpos is a PostgreSQL-compatible alias with the same argument order.

length(X), char_length(X), character_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. char_length and character_length are SQL-standard aliases for length, accepted for compatibility with PostgreSQL and other engines. 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), btrim(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. btrim is a PostgreSQL/Oracle-compatible alias for trim (both the one- and two-argument forms).

max(X, Y, …) and min(X, Y, …)

The multi-argument forms of max 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.

lpad(X, N) and lpad(X, N, F)

Left-pads the string X with the fill string F until the result has exactly N characters. F defaults to a single space. If X is already at least N characters long, it is truncated from the right to length N. F is cycled when it is more than one character long. Returns NULL if any argument is NULL. If F is an empty string, the input is returned unchanged. Lengths are counted in Unicode characters, not bytes, so multi-byte input is padded correctly.

rpad(X, N) and rpad(X, N, F)

Right-pads the string X with the fill string F until the result has exactly N characters. F defaults to a single space. Truncation, fill cycling, NULL propagation, and empty-fill handling are identical to lpad (see the section above).

repeat(X, N)

Returns the string X concatenated with itself N times. Returns an empty string when N <= 0. Returns NULL if either argument is NULL. Non-text inputs are converted to their text representation first.

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.

reverse(X) and string_reverse(X)

Returns string X with its characters in reverse order. Reversal is done on Unicode characters, so multi-byte characters are preserved intact. Returns NULL if X is NULL. Non-text inputs are converted to their text representation first. reverse is the PostgreSQL (since version 17), MySQL, and Oracle spelling; string_reverse is the canonical Turso name. The two are interchangeable.

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.

turso_version()

Returns the version string of the Turso engine.

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