PostgreSQL 提供了多种截取函数用于截取字符串,包括:

  1. substring(string from start for length)

该函数从字符串的开始位置 start 处截取长度为 length 的子串。

例如:

SELECT substring('hello world' from 2 for 5); -- 输出 "ello "
  1. substring(string from start)

该函数从字符串的开始位置 start 处截取到字符串末尾。

例如:

SELECT substring('hello world' from 2); -- 输出 "ello world"
  1. left(string, length)

该函数从字符串的开始位置截取长度为 length 的子串。

例如:

SELECT left('hello world', 5); -- 输出 "hello"
  1. right(string, length)

该函数从字符串的末尾位置截取长度为 length 的子串。

例如:

SELECT right('hello world', 5); -- 输出 "world"
  1. trim(string)

该函数删除字符串首尾的空格。

例如:

SELECT trim(' hello world '); -- 输出 "hello world"
  1. ltrim(string)

该函数删除字符串开头的空格。

例如:

SELECT ltrim(' hello world '); -- 输出 "hello world "
  1. rtrim(string)

该函数删除字符串末尾的空格。

例如:

SELECT rtrim(' hello world '); -- 输出 " hello world"
postgresql 截取函数

原文地址: https://www.cveoy.top/t/topic/yz7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录