SUBSTR

fun SUBSTR(column: KQLiteColumn<*>, start: Int, length: Int? = null): KQLiteColumn<String?>

Extracts a substring from a string.

This function corresponds to the substr(X, Y, Z) function in SQLite. It returns a substring of the input string from the column, starting at a specified position, with an optional length.

  • The start index is 1-based. A positive value indicates the starting position from the beginning of the string.

  • A negative start value indicates the starting position from the end of the string. For example, -1 is the last character, -2 is the second to last, and so on.

  • If length is provided, it specifies the number of characters to extract. A negative length is not meaningful and may produce undefined results.

  • If length is omitted, all characters from the start position to the end of the string are returned.

This function is an alias for SUBSTRING.

Example

// SELECT substr('KQLite is fun', 1, 6) -> 'KQLite'
select(SUBSTR(StringLiteral("KQLite is fun"), 1, 6))

// Get the last 3 characters
// SELECT substr('KQLite is fun', -3) -> 'fun'
select(SUBSTR(StringLiteral("KQLite is fun"), -3))

// Using a column to get a prefix
val userPrefix = select(SUBSTR(Users.username, 1, 3)).from(Users)

Return

A KQLiteColumn of type String containing the extracted substring.

Author

MOHAMMAD AZIM ANSARI

Parameters

column

The source column or string value from which to extract the substring.

start

The 1-based starting position for the substring. Negative values count from the end.

length

The optional number of characters to extract. If omitted, extracts all characters to the end of the string.

See also