Skip to content

Functions ​

Every function smaltdb accepts, in alphabetical order. Names are case-insensitive. The functions follow SQLite's behaviour; each page says where they differ.

PageContents
Scalar functionsNULL handling, conditionals, typeof, scalar min/max, blobs, planner hints, last-statement information
String functionsLength, case, substrings, trimming, concatenation, characters, hex, quote, printf
Math functionsabs, round, sign, random; sqrt, pow, exp, ln, log, ceil, floor, mod, trigonometric and hyperbolic functions
Date and time functionsdate, time, datetime, julianday, unixepoch, strftime, timediff, CURRENT_*; time values, modifiers and format codes
Aggregate functionscount, sum, total, avg, min, max, group_concat, string_agg; statistics: stddev, variance, corr, median, percentile, …; DISTINCT and FILTER
Window functionsRanking and navigation functions, aggregates over windows, OVER syntax
Table-valued functionsread_csv, read_json, read_parquet (from plugins), pragma_*

Operators (||, LIKE, GLOB, arithmetic, comparisons), CASE and CAST are not functions; see Expressions.

SELECT * FROM information_schema.routines lists the scalar and aggregate functions (not the window functions, table-valued functions or CURRENT_* keywords); see information_schema.

All functions ​

FunctionKindDescription
abs(X)scalarAbsolute value
acos(X)scalarArc cosine, in radians
acosh(X)scalarInverse hyperbolic cosine
asin(X)scalarArc sine, in radians
asinh(X)scalarInverse hyperbolic sine
atan(X)scalarArc tangent, in radians
atan2(Y, X)scalarAngle of the point (X, Y), in radians
atanh(X)scalarInverse hyperbolic tangent
avg(X)aggregateAverage of non-NULL values
ceil(X), ceiling(X)scalarRound up
changes()scalarRows changed by the last INSERT, UPDATE or DELETE
char(X, …)scalarString from Unicode code points
coalesce(X, Y, …)scalarFirst non-NULL argument
concat(X, …)scalarConcatenation, skipping NULLs
concat_ws(SEP, X, …)scalarConcatenation with a separator, skipping NULLs
corr(Y, X)aggregateCorrelation coefficient
cos(X)scalarCosine
cosh(X)scalarHyperbolic cosine
count(*), count(X)aggregateNumber of rows, or of non-NULL values
covar_pop(Y, X)aggregatePopulation covariance
covar_samp(Y, X)aggregateSample covariance
cume_dist()windowCumulative distribution within the partition
CURRENT_DATEkeywordCurrent UTC date, YYYY-MM-DD
CURRENT_TIMEkeywordCurrent UTC time, HH:MM:SS
CURRENT_TIMESTAMPkeywordCurrent UTC date and time, YYYY-MM-DD HH:MM:SS
date(TIME, MOD, …)scalarDate as YYYY-MM-DD
datetime(TIME, MOD, …)scalarDate and time as YYYY-MM-DD HH:MM:SS
degrees(X)scalarRadians to degrees
dense_rank()windowRank without gaps
exp(X)scalare to the power X
first_value(X)windowValue from the first row of the frame
floor(X)scalarRound down
format(FORMAT, …)scalarSame as printf
group_concat(X [, SEP])aggregateValues joined into text
hex(X)scalarUpper-case hexadecimal of the bytes
ifnull(X, Y)scalarX if not NULL, else Y
if(COND, X, …)scalarSame as iif
iif(COND, X, Y)scalarX if COND is true, else Y
instr(X, Y)scalarPosition of Y in X
julianday(TIME, MOD, …)scalarJulian day number
kurtosis(X)aggregateSample excess kurtosis
kurtosis_pop(X)aggregatePopulation excess kurtosis
lag(X [, N [, DEFAULT]])windowValue from an earlier row
last_insert_rowid()scalarRowid of the last inserted row
last_value(X)windowValue from the last row of the frame
lead(X [, N [, DEFAULT]])windowValue from a later row
length(X)scalarCharacters in text, bytes in a blob
likelihood(X, P)scalarReturns X (planner hint, ignored)
likely(X)scalarReturns X (planner hint, ignored)
ln(X)scalarNatural logarithm
log(X), log(B, X)scalarBase-10 logarithm, or to base B
log10(X)scalarBase-10 logarithm
log2(X)scalarBase-2 logarithm
lower(X)scalarASCII lower case
ltrim(X [, CHARS])scalarRemove characters from the start
max(X)aggregateLargest value
max(X, Y, …)scalarLargest argument
median(X)aggregateMedian
min(X)aggregateSmallest value
min(X, Y, …)scalarSmallest argument
mod(X, Y)scalarRemainder of X / Y, as a REAL
nth_value(X, N)windowValue from the N-th row of the frame
ntile(N)windowBucket number 1..N
nullif(X, Y)scalarNULL if X = Y, else X
octet_length(X)scalarBytes in the UTF-8 encoding
percent_rank()windowRelative rank, 0 to 1
percentile(X, P)aggregatePth percentile (0–100), interpolated
percentile_cont(X, F)aggregatePercentile at fraction F, interpolated
percentile_disc(X, F)aggregatePercentile at fraction F, one of the values
pi()scalarπ
pow(X, Y), power(X, Y)scalarX to the power Y
pragma_foreign_key_list(TABLE)table-valuedForeign keys of a table
pragma_index_info(INDEX)table-valuedKey columns of an index
pragma_index_list(TABLE)table-valuedIndexes of a table
pragma_index_xinfo(INDEX)table-valuedAll columns of an index
pragma_table_info(TABLE)table-valuedColumns of a table
pragma_table_xinfo(TABLE)table-valuedpragma_table_info plus hidden
printf(FORMAT, …)scalarC-style formatted text
quote(X)scalarValue as an SQL literal
radians(X)scalarDegrees to radians
random()scalarRandom 64-bit integer
randomblob(N)scalarN random bytes
rank()windowRank with gaps
read_csv(LOCATION, …)table-valuedRows of a CSV or TSV file (@smaltdb/io plugin)
read_json(LOCATION, …)table-valuedRows of a JSON or NDJSON file (@smaltdb/io plugin)
read_parquet(LOCATION, …)table-valuedRows of a Parquet file (@smaltdb/parquet plugin)
replace(X, Y, Z)scalarReplace every Y in X with Z
round(X [, DIGITS])scalarRound to DIGITS decimal places
row_number()windowRow number within the partition
rtrim(X [, CHARS])scalarRemove characters from the end
sign(X)scalar-1, 0 or 1
sin(X)scalarSine
sinh(X)scalarHyperbolic sine
skewness(X)aggregateSample skewness
skewness_pop(X)aggregatePopulation skewness
smaltdb_version()scalarThe smaltdb package version
sqrt(X)scalarSquare root
stddev(X), stddev_samp(X)aggregateSample standard deviation
stddev_pop(X)aggregatePopulation standard deviation
strftime(FORMAT, TIME, MOD, …)scalarFormatted date and time
string_agg(X, SEP)aggregateSame as group_concat(X, SEP)
substr(X, START [, LENGTH])scalarPart of a string or blob
substring(X, START [, LENGTH])scalarSame as substr
sum(X)aggregateSum; NULL when there are no values
tan(X)scalarTangent
tanh(X)scalarHyperbolic tangent
time(TIME, MOD, …)scalarTime as HH:MM:SS
timediff(A, B)scalarDifference between two times
total(X)aggregateSum; 0 when there are no values
total_changes()scalarRows changed since the connection opened
trim(X [, CHARS])scalarRemove characters from both ends
trunc(X)scalarRound toward zero
typeof(X)scalarStorage class: null, integer, real, text, blob
unicode(X)scalarCode point of the first character
unixepoch(TIME, MOD, …)scalarSeconds since 1970-01-01
unlikely(X)scalarReturns X (planner hint, ignored)
upper(X)scalarASCII upper case
var_pop(X)aggregatePopulation variance
var_samp(X), variance(X)aggregateSample variance
zeroblob(N)scalarN zero bytes

Every aggregate function can also be used as a window function with OVER; see Window functions.

Not available ​

These SQLite functions are not implemented. Each fails with SMALT_ERROR no such function: name (or no such table-valued function in FROM) when the statement is prepared.

GroupFunctions
JSONjson, jsonb, json_array, json_array_length, json_extract, json_insert, json_object, json_patch, json_quote, json_remove, json_replace, json_set, json_type, json_valid, json_group_array, json_group_object, and the table-valued json_each and json_tree. The -> and ->> operators fail with SMALT_UNSUPPORTED.
Textunhex, unistr, unistr_quote, and the function forms like(…) and glob(…) (the LIKE and GLOB operators work). REGEXP has no implementation.
SQLite-specificsqlite_version, sqlite_source_id, sqlite_compileoption_get, sqlite_compileoption_used, sqlite_offset, load_extension, subtype
Table-valuedgenerate_series (use a recursive CTE; see SELECT)
sql
SELECT json_extract('{"a": 1}', '$.a');
sql
SELECT '{"a": 1}' ->> '$.a';

There are no user-defined functions: functions cannot be added from JavaScript.

A sequence of numbers without generate_series:

sql
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 5)
SELECT group_concat(i) AS numbers FROM n;
numbers
1,2,3,4,5

MIT License