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