SUBSTRING

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

Extracts a substring from a string. This is an alias for the SUBSTR function.

This function corresponds to the substring(X, Y, Z) or substr(X, Y, Z) function in SQLite. It returns a substring of the input string from the column. The portion of the string is defined by the start position and the 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.

  • If length is provided, it specifies the number of characters to extract.

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

Example

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

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

// Using a column
val userInitials = select(SUBSTRING(Users.firstName, 1, 1)).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