BQL Functions Reference
Complete reference for all BQL functions.
Position/Amount Functions
NUMBER
Extract numeric value from an amount or position.
NUMBER(amount) → Decimal
NUMBER(position) → DecimalExamples:
SELECT NUMBER(units) -- 100.00
SELECT NUMBER(position) -- Units number from positionCURRENCY
Extract currency from an amount or position.
CURRENCY(amount) → String
CURRENCY(position) → StringExamples:
SELECT CURRENCY(units) -- "USD"UNITS
Extract units (amount without cost) from a position.
UNITS(position) → Amount
UNITS(amount) → Amount
UNITS(inventory) → StringFor inventories, returns a formatted string of all positions.
COST
Calculate total cost of a position or inventory.
COST(position) → Amount
COST(inventory) → AmountFormula: |units.number| × cost.number
Returns NULL if no cost basis.
WEIGHT
Calculate balancing weight of a position.
WEIGHT(position) → Amount
WEIGHT(amount) → AmountWeight calculation:
- Position with cost:
units × cost - Position without cost:
units - Amount with price: handled at posting level
VALUE
Convert to market value using price database.
VALUE(position) → Amount
VALUE(position, currency) → Amount
VALUE(inventory) → Amount
VALUE(inventory, currency) → AmountUses most recent price as of transaction date.
Examples:
SELECT VALUE(position, "USD") -- Convert to USD
SELECT VALUE(position) -- Use default operating currencyGETITEM / GET
Extract amount for specific currency from inventory.
GETITEM(inventory, currency) → Amount
GET(inventory, currency) → AmountReturns NULL if currency not in inventory.
GETPRICE
Look up price from price database.
GETPRICE(base, quote) → Decimal
GETPRICE(base, quote, date) → DecimalReturns NULL if no price found.
Date Functions
YEAR
Extract year from date.
YEAR(date) → IntegerExample:
SELECT YEAR(date) -- 2024MONTH
Extract month from date (1-12).
MONTH(date) → IntegerDAY
Extract day of month from date (1-31).
DAY(date) → IntegerQUARTER
Extract quarter from date.
QUARTER(date) → StringReturns format like "2024-Q1" (year and quarter).
WEEKDAY
Extract day of week as abbreviated name.
WEEKDAY(date) → StringReturns "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", or "Sun".
String Functions
LENGTH
Get string length.
LENGTH(string) → Integer
LENGTH(set) → IntegerFor sets (tags, links), returns number of elements.
UPPER
Convert to uppercase.
UPPER(string) → StringLOWER
Convert to lowercase.
LOWER(string) → StringSUBSTR / SUBSTRING
Extract substring.
SUBSTR(string, start) → String
SUBSTR(string, start, length) → String0-indexed start position.
Account Functions
PARENT
Get parent account (remove last component).
PARENT(account) → StringExample:
PARENT("Assets:Bank:Checking") -- "Assets:Bank"LEAF
Get last account component.
LEAF(account) → StringExample:
LEAF("Assets:Bank:Checking") -- "Checking"ROOT
Get first N components of account.
ROOT(account, n) → StringExample:
ROOT("Assets:Bank:Checking", 2) -- "Assets:Bank"Aggregate Functions
COUNT
Count rows or non-NULL values.
COUNT(*) → Integer
COUNT(expr) → IntegerSUM
Sum values (works with amounts, positions, inventories).
SUM(amount) → Amount
SUM(position) → Inventory
SUM(number) → DecimalExample:
SELECT account, SUM(position)
GROUP BY account;MIN / MAX
Minimum or maximum value.
MIN(expr) → same type
MAX(expr) → same typeWorks with numbers, dates, strings.
FIRST / LAST
First or last value in group (by sort order).
FIRST(expr) → same type
LAST(expr) → same typeUseful with ORDER BY:
SELECT account, FIRST(date), LAST(date)
GROUP BY account
ORDER BY date;Inventory Functions
EMPTY
Check if inventory is empty.
EMPTY(inventory) → BooleanReturns TRUE for NULL inventories.
FILTER_CURRENCY
Filter inventory to single currency.
FILTER_CURRENCY(inventory, currency) → InventoryExample:
FILTER_CURRENCY(balance, "USD") -- Only USD positionsPOSSIGN
Adjust sign based on account type (debit-normal vs credit-normal).
POSSIGN(amount, account) → AmountCredit-normal accounts (Liabilities, Equity, Income) have signs inverted.
Example:
SELECT POSSIGN(units, account) -- Positive for debit-normalType Conversion
COALESCE
Return first non-NULL argument.
COALESCE(expr1, expr2, ...) → same typeExample:
COALESCE(payee, "Unknown")ABS
Absolute value.
ABS(number) → Decimal
ABS(amount) → AmountMetadata Access
META
Access metadata value by key.
META(key) → MetaValueExample:
SELECT META("category")
WHERE META("reviewed") = TRUE;Special Functions
ENTRY_META
Access transaction-level metadata (from posting context).
ENTRY_META(key) → MetaValueANY_META
Check if any metadata key matches pattern.
ANY_META(pattern) → Boolean