跳到主要内容

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)