SUBSTRING function
Returns the substring of an expression that starts at a position and is of a certain length.
Syntax
SUBSTRING(expression, position, length)
Arguments
expression: The string.position: The 1-based index from which the substring starts.length: The length of the substring. If omitted, it will go until the end of the expression.
Returns
A VARCHAR.
Examples
> SELECT SUBSTRING('abc', 1);
'abc'
> SELECT SUBSTRING('abc', 1, 1);
'a'
> SELECT SUBSTRING('abc', 5);
''
Notes
Negative position behavior
Negative integer literals: These are not supported in any environment.
Functional negative positions: It is possible to have a negative position when using a function.
Vertica: These start from the imaginary places before the actual start of the string.
Example:
abcwith a position of-1and length of3returnsa.
Spark-based engine: These start counting backwards from the end of the string.
Example:
abcwith a position of-2and length of2returnsbc.
Negative length behavior
Negative integer literals: These are not supported.
Functional negative lengths: It is possible to have a negative length when using a function.
Vertica: These will throw an exception.
Spark-based engine: These will return an empty string.