pgsql支持substr吗
时间: 2023-07-28 19:41:20 浏览: 44
是的,PostgreSQL 支持内置的 SUBSTR 函数,该函数用于从一个字符串中提取一个子字符串。语法如下:
```
substr(string, start_position [, length])
```
其中,string 是要提取子字符串的字符串,start_position 是子字符串的起始位置(从 1 开始计数),length 是可选参数,表示要提取的子字符串的长度。如果省略 length 参数,则函数将从 start_position 位置开始提取到字符串的末尾。
例如,要从字符串 'hello world' 中提取从第 7 个字符开始的子字符串,可以使用以下查询:
```
SELECT substr('hello world', 7);
```
该查询将返回字符串 'world'。
如果要从同样的字符串中提取前 5 个字符,则可以使用以下查询:
```
SELECT substr('hello world', 1, 5);
```
该查询将返回字符串 'hello'。
需要注意的是,start_position 参数必须大于等于 1,否则函数将返回 NULL。如果 length 参数大于字符串的长度,函数将从 start_position 位置开始提取到字符串的末尾。
相关问题
pgsql的substr的用法
substr函数是PostgreSQL中用于截取字符串的函数,它的语法如下:
```
substr(string, start_position, [length])
```
其中,string为要截取的字符串,start_position为开始截取的位置,length为截取的长度。如果省略length,则截取从start_position开始到字符串末尾的所有字符。
例如,如果要从字符串'Hello, World!'中截取'World!',可以使用如下语句:
```
SELECT substr('Hello, World!', 8);
```
结果为:
```
World!
```
如果要从字符串'Hello, World!'中截取'World',可以使用如下语句:
```
SELECT substr('Hello, World!', 8, 5);
```
结果为:
```
World
```
注意,start_position从1开始计数,而不是从0开始计数。
pgsql两字段互换
引用提供了一种使用正则表达式的方法来实现字符串中两个字段的互换。具体代码如下:
```
SELECT regexp_substr('abc1,cbd2,db3,db5', '[^, ', 1, rownum) AS 字符串
FROM dual
CONNECT BY rownum <= length('abc1,cbd2,db3,db5') - length(replace('abc1,cbd2,db3,db5', ',', ''));
``<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [plsql 字符串转成数组(互换)](https://blog.csdn.net/qq_34745941/article/details/106348979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]