📣
TiDB Cloud Premium 开放公测中。为企业级工作负载提供无限扩展、即时弹性伸缩和高级安全保障。此页面由 AI 自动翻译,英文原文请见此处。

SUBSTR



从给定字符串的特定位置提取包含指定数量字符的字符串。

  • 不带 len 参数的形式会从字符串 str 的位置 pos 开始返回一个子字符串。
  • 带 len 参数的形式会从字符串 str 的位置 pos 开始,返回一个长度为 len 个字符的子字符串。

pos 也可以使用负值。在这种情况下,子字符串的起始位置是从字符串末尾向前数 pos 个字符,而不是从开头开始计数。在此函数的任意形式中都可以对 pos 使用负值。pos 的值为 0 时返回空字符串。字符串中用于提取子字符串的第一个字符位置记为 1。

语法

SUBSTR(<str>, <pos>) SUBSTR(<str>, <pos>, <len>)

参数

参数描述
<str>要从中提取字符的主字符串
<pos>子字符串的起始位置(从 1 开始)。如果为负数,则从末尾开始计数
<len>要提取的子字符串的最大长度

别名

返回类型

VARCHAR

示例

SELECT SUBSTRING('Quadratically', 5), SUBSTR('Quadratically', 5), MID('Quadratically', 5); ┌─────────────────────────────────────────────────────────────────────────────────────────────────┐ │ substring('quadratically' from 5) │ substring('quadratically' from 5) │ mid('quadratically', 5) │ ├───────────────────────────────────┼───────────────────────────────────┼─────────────────────────┤ │ ratically │ ratically │ ratically │ └─────────────────────────────────────────────────────────────────────────────────────────────────┘ SELECT SUBSTRING('Quadratically', 5, 6), SUBSTR('Quadratically', 5, 6), MID('Quadratically', 5, 6); ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ substring('quadratically' from 5 for 6) │ substring('quadratically' from 5 for 6) │ mid('quadratically', 5, 6) │ ├─────────────────────────────────────────┼─────────────────────────────────────────┼────────────────────────────┤ │ ratica │ ratica │ ratica │ └────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

文档内容是否有帮助?