SUBSTR
functionsupportedCore SQL functionsReturns the portion of the string or binary value from base_expr, starting from the character/byte specified by start_expr, with optionally limited length. These functions are synonymous. See also: LEFT , RIGHT
Syntax
supportedpartialnot supportednot scored
SUBSTR( <base_expr>, <start_expr> [ , <length_expr> ] )
SUBSTRING( <base_expr>, <start_expr> [ , <length_expr> ] )
Parameters
| Name | Required | Description |
|---|---|---|
base_expr | required | An expression that evaluates to a VARCHAR or BINARY value. |
start_expr | required | An expression that evaluates to an integer. It specifies the offset from which the substring starts. The offset is measured in: The number of UTF-8 characters if the input is a VARCHAR value. The number of bytes if the input is a BINARY value. The start position is 1-based, not 0-based. For example, SUBSTR('abc', 1, 1) returns a, not b. |
length_expr | required | An expression that evaluates to an integer. It specifies: The number of UTF-8 characters to return if the input is VARCHAR. The number of bytes to return if the input is BINARY. Specify a length that is greater than or equal to zero. If the length is a negative number, the function returns an empty string. |
Returns
The data type of the returned value is the same as the data type of the base_expr (VARCHAR or BINARY). If any of the inputs are NULL, NULL is returned.
Usage notes
- If length_expr is specified, up to length_expr characters/bytes are returned. If length_expr isn’t specified, all the characters until the end of the string or binary value are returned.
- The values in start_expr start from 1: If 0 is specified, it is treated as 1.
- If a negative value is specified, the starting position is computed as the start_expr characters/bytes from the end of the string or binary value. If the position is outside of the range of a string or binary value, an empty value is returned.
Collation details
- Collation applies to VARCHAR inputs. Collation doesn’t apply if the input data type of the first parameter is BINARY. - No impact. Although collation is accepted syntactically, collations don’t affect processing. For example, two-character and three-character letters in languages (for example, “dzs” in Hungarian or “ch” in Czech) are still counted as two or three characters (not one character) for the length argument. - The collation of the result is the same as the collation of the input. This can be useful if the returned value is passed to another function as part of nested function calls.