Positional SUBSTRING
In its simple, positional form (with FROM
), this function returns the substring starting at character position startpos (the first character being 1).Without the FOR
argument, it returns all the remaining characters in the string.With FOR
, it returns length characters or the remainder of the string, whichever is shorter.
When startpos is smaller than 1, substring behaves as if the string has 1 - startpos
extra positions before the actual first character at position 1
.The length is considered from this imaginary start of the string, so the resulting string could be shorter than the specified length, or even empty.
The function fully supports binary and text BLOB
s of any length, and with any character set.If str is a BLOB
, the result is also a BLOB
.For any other argument type, the result is a VARCHAR
.
For non-BLOB
arguments, the width of the result field is always equal to the length of str, regardless of startpos and length.So, substring('pinhead' from 4 for 2)
will return a VARCHAR(7)
containing the string 'he'
.
insert into AbbrNames(AbbrName)
select substring(LongName from 1 for 3) from LongNames;
select substring('abcdef' from 1 for 2) from rdb$database;
-- result: 'ab'
select substring('abcdef' from 2) from rdb$database;
-- result: 'bcdef'
select substring('abcdef' from 0 for 2) from rdb$database;
-- result: 'a'
-- and NOT 'ab', because there is "nothing" at position 0
select substring('abcdef' from -5 for 2) from rdb$database;
-- result: ''
-- length ends before the actual start of the string