The SUBSTRING function
You can use the SUBSTRING function
to return some portion of a character string. You specify the start
position and length (optional) to determine which portion
of the character string the function returns. You can specify a positive
or negative number for the start position. A start position of 1
specifies
that the SUBSTRING function begins from the first
position in the string. When the start position is zero (0) or a negative
number, the SUBSTRING function counts backward
from the beginning of the string.
The following query shows
an example of the SUBSTRING function, which returns
the first four characters for any sname column values that
the query returns. In this example, the SUBSTRING function
starts at the beginning of the string and returns four characters
counting forward from the start position.
In the following query, the SUBSTRING function
specifies a start position of
6
but does not specify
the length. The function returns a character string that extends from
the sixth position to the end of the string. In the following query, the SUBSTRING function
returns only the first character for any sname column value
that the query returns. For the SUBSTRING function,
a start position of
-2
counts backward three positions
(0, -1, -2
) from the start position of the string
(for a start position of 0
, the function counts backward
one position from the beginning of the string).