Skip to main content

Functions

ScramDB provides 72 scalar functions, 10 aggregate functions, and 16 window functions.

Scalar Functions

String Functions (22)

FunctionSyntaxDescription
LENGTHLENGTH(str)Number of characters
UPPERUPPER(str)Convert to uppercase
LOWERLOWER(str)Convert to lowercase
SUBSTRINGSUBSTRING(str FROM start FOR len)Extract substring
TRIMTRIM(str)Remove leading/trailing whitespace
LTRIMLTRIM(str)Remove leading whitespace
RTRIMRTRIM(str)Remove trailing whitespace
BTRIMBTRIM(str, chars)Remove specified characters from both ends
CONCATCONCAT(str1, str2, ...)Concatenate strings
CONCAT_WSCONCAT_WS(sep, str1, str2, ...)Concatenate with separator
REPLACEREPLACE(str, from, to)Replace all occurrences
POSITIONPOSITION(substr IN str)Find substring position (1-based)
LEFTLEFT(str, n)First n characters
RIGHTRIGHT(str, n)Last n characters
LPADLPAD(str, len, fill)Pad left to length
RPADRPAD(str, len, fill)Pad right to length
REPEATREPEAT(str, n)Repeat string n times
REVERSEREVERSE(str)Reverse string
INITCAPINITCAP(str)Capitalize first letter of each word
SPLIT_PARTSPLIT_PART(str, delim, n)Extract nth field
STARTS_WITHSTARTS_WITH(str, prefix)Check prefix
ENDS_WITHENDS_WITH(str, suffix)Check suffix
ASCIIASCII(str)ASCII code of first character
CHRCHR(code)Character from ASCII code
MD5MD5(str)MD5 hash as hex string

Math Functions (27)

FunctionSyntaxDescription
ABSABS(x)Absolute value
CEIL / CEILINGCEIL(x)Round up to nearest integer
FLOORFLOOR(x)Round down to nearest integer
ROUNDROUND(x, d)Round to d decimal places
TRUNCTRUNC(x, d)Truncate to d decimal places
SQRTSQRT(x)Square root
CBRTCBRT(x)Cube root
POWERPOWER(x, y)x raised to power y
EXPEXP(x)e^x
LNLN(x)Natural logarithm
LOGLOG(base, x)Logarithm with base
LOG10LOG10(x)Base-10 logarithm
MODMOD(x, y)Modulo (remainder)
SIGNSIGN(x)Sign (-1, 0, or 1)
PIPI()π constant
DEGREESDEGREES(radians)Radians to degrees
RADIANSRADIANS(degrees)Degrees to radians
GCDGCD(a, b)Greatest common divisor
LCMLCM(a, b)Least common multiple
SINSIN(x)Sine
COSCOS(x)Cosine
TANTAN(x)Tangent
ASINASIN(x)Arc sine
ACOSACOS(x)Arc cosine
ATANATAN(x)Arc tangent
ATAN2ATAN2(y, x)Two-argument arc tangent
GREATESTGREATEST(a, b, ...)Largest value
LEASTLEAST(a, b, ...)Smallest value

Date/Time Functions (16)

FunctionSyntaxDescription
EXTRACTEXTRACT(field FROM source)Extract date/time field
DATE_PARTDATE_PART('field', source)Same as EXTRACT (function form)
DATE_TRUNCDATE_TRUNC('field', source)Truncate to specified precision
NOWNOW()Current timestamp
CURRENT_DATECURRENT_DATECurrent date
CURRENT_TIMESTAMPCURRENT_TIMESTAMPCurrent timestamp
AGEAGE(ts1, ts2)Interval between timestamps
TO_CHARTO_CHAR(value, format)Format to string
TO_DATETO_DATE(str, format)Parse date from string
TO_TIMESTAMPTO_TIMESTAMP(str, format)Parse timestamp from string
MAKE_DATEMAKE_DATE(year, month, day)Construct date
MAKE_TIMESTAMPMAKE_TIMESTAMP(y,m,d,h,m,s)Construct timestamp

EXTRACT fields: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, EPOCH, DOW (day of week), DOY (day of year), QUARTER, WEEK


Aggregate Functions

FunctionSyntaxDescription
COUNTCOUNT(*) or COUNT(expr)Count rows
COUNT(DISTINCT)COUNT(DISTINCT expr)Count distinct values
SUMSUM(expr)Sum of values
AVGAVG(expr)Average
MINMIN(expr)Minimum value
MAXMAX(expr)Maximum value
STDDEV / STDDEV_SAMPSTDDEV(expr)Sample standard deviation
STDDEV_POPSTDDEV_POP(expr)Population standard deviation
VARIANCE / VAR_SAMPVARIANCE(expr)Sample variance
VAR_POPVAR_POP(expr)Population variance
STRING_AGGSTRING_AGG(expr, delimiter)Concatenate with delimiter
BOOL_AND / EVERYBOOL_AND(expr)True if all true
BOOL_ORBOOL_OR(expr)True if any true

All aggregate functions support FILTER (WHERE ...) clause:

SELECT
COUNT(*) AS total,
COUNT(*) FILTER (WHERE status = 'active') AS active_count
FROM users;

Window Functions

Window functions operate over a partition of rows defined by OVER().

Ranking Functions

FunctionDescription
ROW_NUMBER()Unique sequential number per partition
RANK()Rank with gaps for ties
DENSE_RANK()Rank without gaps for ties
NTILE(n)Distribute rows into n buckets
PERCENT_RANK()Relative rank (0 to 1)
CUME_DIST()Cumulative distribution

Value Functions

FunctionDescription
LAG(expr, offset, default)Value from preceding row
LEAD(expr, offset, default)Value from following row
FIRST_VALUE(expr)First value in window frame
LAST_VALUE(expr)Last value in window frame
NTH_VALUE(expr, n)Nth value in window frame

Aggregate Windows

All aggregate functions (SUM, COUNT, AVG, MIN, MAX) can be used as window functions:

SELECT
date,
amount,
SUM(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS week_total,
AVG(amount) OVER (PARTITION BY category ORDER BY date) AS running_avg
FROM sales;

Frame Clauses

-- Supported frame specifications
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW -- default
ROWS BETWEEN N PRECEDING AND N FOLLOWING
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING