Syntax
SUBSTRING(str, pos)
SUBSTRING(str, pos, len)
SUBSTRING(str FROM pos)
SUBSTRING(str FROM pos FOR len)
Purpose
You can call this function to return a substring of str. The start position is pos, and the length of the substring is len characters. If any argument is NULL, the function returns NULL. The function is the alias for SUBSTR().
If
lenis not specified, the returned substring starts from the positionpostill the end ofstr.If the value of
posis negative, the start position is reversely determined from the end ofstr.If
lenis less than or equal to 0, or the start position specified byposis invalid, an empty string is returned.
Examples
obclient> SELECT
SUBSTRING('abcdefg', 3),
SUBSTRING('abcdefg', 3, 2),
SUBSTRING('abcdefg', -3),
SUBSTRING('abcdefg', 3, -2),
SUBSTRING('abcdefg' from -4 for 2)
\G
*************************** 1. row ***************************
SUBSTRING('abcdefg', 3): cdefg
SUBSTRING('abcdefg', 3, 2): cd
SUBSTRING('abcdefg', -3): efg
SUBSTRING('abcdefg', 3, -2):
SUBSTRING('abcdefg' from -4 for 2): de
1 row in set (0.01 sec)