SQLite substr() function

The SQLite substr function returns a substring from a string starting at a specified position with a predefined length.

Syntax

substr( string, start, length )Code language: SQL (Structured Query Language) (sql)

The starting position of the substring is determined by the start argument and its length is determined by the length argument.

Arguments

The substr() function accepts three arguments.

string

The string to be used to extract the substring.

start

The start argument is an integer that specifies the starting position of the returned substring. The start argument can be a positive or negative integer

  • If start is a positive integer, the  substr() function returns a substring starting from the beginning of the string. The first character has an index of 1.
  • If the start is a negative integer, the returned substring consists of the length number of characters starting from the end of the string. The last character has an index of -1.

See the following picture of a sample string with the positive and negative start