SQLite substring() Function
The SQLite substr()
function extracts and returns a substring from a specified string according to the specified starting position and length.
From SQLite 3.34, substring()
is added as an aliase for substr()
.
Syntax
This is the syntax of the SQLite substr()
function:
substr(string, start[, length])
Parameters
string
-
Required. The string to extract from.
start
-
Required. The starting position of the substring.
length
-
Optional. The length of the substring. The default is to the end of the string
string
.
Return value
The SQLite function substr()
extracts a substring of length length
starting at start
position from the string string
and returns it. If length
is not specified, substr()
extracts the substring from start
to the end of string string
.
If start + length
exceeds the length of string string
, return the substring from start
to the end of string string
.
If any parameter is NULL
, the function will return NULL
.
Examples
This example shows how to use the substring()
function to extract the string starting at position 7 in a string.
SELECT substring('hello world', 7);
substring('hello world', 7)
---------------------------
world
You can also specify the number of characters to extract (the length of the substring), for example:
SELECT substring('hello world', 7, 5);
substring('hello world', 7, 5)
------------------------------
world