SUBSTR
声明
SUBSTR(str, pos)
SUBSTR(str, pos, len)
SUBSTR(str FROM pos)
SUBSTR(str FROM pos FOR len)
说明
返回 str 的子字符串,起始位置为 pos,长度为 len。参数中包含 NULL 时,返回 NULL。
-
不指定
len时,返回的子字符串从pos位置开始到str结尾。 -
pos值为负数时,从str尾部向头部倒序确定起始位置。 -
len小于等于 0,或pos指定的起始位置非法时,返回空字符串。
示例
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)