跳到主要内容

SUBSTR

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

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

也可以使用负值作为 pos。在这种情况下,子字符串的起始位置是从字符串末尾开始计数的 pos 个字符,而不是从开头开始。负值可以用于此函数的任何形式中的 pospos 为 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 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
开始使用 Databend Cloud
低成本
快速分析
多种数据源
弹性扩展
注册