Postgresql常用函数整理
一、字符串函数
1、函数:string || string(string || non-string)
说明:字符串(或与非字符串)连接
示例:
2、函数:char_length(string)
说明:计算字符串中字符个数
示例:
3、函数:overlay(string placing string from int [for int])
说明:替换字符串中任意长度的子字串为新字符串
示例:
注:from 3 to 4 表示从第3个字节起开始,向后再算4个字节
4、函数:position(substring in string)
说明:子串在一字符串中的位置
示例:
5、函数:substring(string [from int] [for int])
说明:截取任意长度的子字符串
示例:
注:from 2 to 5 表示从第2个字节起开始,向后再算5个字节
6、函数:substring(string from pattern)
说明:利用正则表达式对一字符串进行任意长度的字串的截取
示例:
注:从后向前截取,一个点表示一位,如果字段内容小于截取长度,值为NULL
7、函数:trim([leading| trailing |both][ characters ]from string )
说明:从字符串 string 的开头/结尾/两边删除只包含characters 中字符 (缺省是空白)的最长的字符串
示例:
8、函数:lower( string )
说明:把字符串转化为小写
示例:
9、函数:upper( string )
说明:把字符串转化为大写
示例:
10、函数:bit_length(string)
说明:计算字符串位数(bits)
示例:
二、聚合函数
1、函数:string_agg( expression ,delimiter )
或者 array_to_string(array_agg(字段),',')
说明:输入值连接成为一个字符串,用分隔符分开
示例:
2、函数:json_agg( expression )
说明:聚合值作为JSON数组
示例:
3.split_part(str,',',1) 字符串分割符
select split_part('A3332-22222222','-',1); - > A3332
select split_part('A3332-22222222','-',2); - > 22222222
4.行转列
regexp_split_to_table
(string, pattern [, flags ])
select regexp_split_to_table(cmd,',')
from 【table】 c
-------------------------
a01#221 a02 a03