SUBSTRING
Declaration
SUBSTRING(str, pos)
SUBSTRING(str, pos, len)
SUBSTRING(str FROM pos)
SUBSTRING(str FROM pos FOR len)
Description
Returns a substring of str starting at position pos and having a length of len. If any parameter is NULL, the function returns NULL. This function is an alias for SUBSTR().
-
If
lenis not specified, the substring starts atposand extends to the end ofstr. -
If
posis a negative number, the starting position is determined by counting backward from the end ofstr. -
If
lenis less than or equal to 0, or if the starting position specified byposis invalid, an empty string is returned.
Examples
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.001 sec)