SUBSTR
Syntax
SUBSTR(str, pos)
SUBSTR(str, pos, len)
SUBSTR(str FROM pos)
SUBSTR(str FROM pos FOR len)
Description
Returns a substring of str starting at position pos and with length len. If any of the parameters is NULL, the function returns NULL.
-
If
lenis not specified, the substring starts at positionposand continues 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, the function returns an empty string.
Examples
SELECT
SUBSTR('abcdefg', 3),
SUBSTR('abcdefg', 3, 2),
SUBSTR('abcdefg', -3),
SUBSTR('abcdefg', 3, -2),
SUBSTR('abcdefg' from -4 for 2)
\G
*************************** 1. row ***************************
SUBSTR('abcdefg', 3): cdefg
SUBSTR('abcdefg', 3, 2): cd
SUBSTR('abcdefg', -3): efg
SUBSTR('abcdefg', 3, -2):
SUBSTR('abcdefg' from -4 for 2): de
1 row in set (0.001 sec)