跳到主要内容

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)