Returns a substring of a string.
{ SUBSTRING | SUBSTR } ( string-expression, start
[, length ] )
string-expression The string from which a substring is to be returned.
start The start position of the substring to return, in characters.
length The length of the substring to return, in characters. If length is specified, the substring is restricted to that length.
In UltraLite, the database does not have an ansi_substring option. Nonetheless, the SUBSTR function behaves as if ansi_substring is set to on by default. In other words, the function's behavior corresponds to ANSI/ISO SQL/2003 behavior:
Start value The first character in the string is at position 1. A negative or zero start offset is treated as if the string were padded on the left with non-characters.
Length value A positive length specifies that the substring ends length characters to the right of the starting position.
A negative length returns an error.If string-expression is of binary data type, the SUBSTRING function behaves as BYTE_SUBSTR.
It is recommended that you avoid using non-positive start offsets or negative lengths with the SUBSTRING function. Where possible, use the LEFT or RIGHT functions instead.
Whenever possible, if the input string uses character length semantics the return value is described in terms of character length semantics.
SQL/2003 Core feature.
The following table shows the values returned by the SUBSTRING function when used in a SELECT statement, with the ansi_substring option set to On and Off.
Example | Result with ansi_substring set to On | Result with ansi_substring set to Off |
---|---|---|
SUBSTRING( 'front yard', 1, 4 ) | fron | fron |
SUBSTRING( 'back yard', 6, 4 ) | yard | yard |
SUBSTR( 'abcdefgh', 0, -2 ) | Returns an error | gh |
SUBSTR( 'abcdefgh', -2, 2 ) | Returns an empty string | gh |
SUBSTR( 'abcdefgh', 2, -2 ) | Returns an error | ab |
SUBSTR( 'abcdefgh', 2, -4 ) | Returns an error | ab |
SUBSTR( 'abcdefgh', 2, -1 ) | Returns an error | b |