对字符串的函数和运算。在平时应用中超级有用!
Function |
Return Type |
Description |
Example |
Result |
string || string |
text |
字符串连接 |
'Post' || 'greSQL' |
PostgreSQL |
string || non-string or non-string || string |
text |
字符和非字符连接 |
'Value: ' || 42 |
Value: 42 |
bit_length(string) |
int |
字符转换为二进制后的长度 |
bit_length('jose') |
32 |
char_length(string) or character_length(string) |
int |
字符串长度 |
char_length('jose') |
4 |
lower(string) |
text |
将大写转换为小写 |
lower('TOM') |
tom |
octet_length(string) |
int |
字节长度 |
octet_length('jose') |
4 |
overlay(string placingstring from int [for int]) |
text |
替换 |
overlay('Txxxxas' placing 'hom' from 2 for 4) |
Thomas |
position(substring instring) |
int |
定位 |
position('om' in 'Thomas') |
3 |
substring(string [fromint] [for int]) |
text |
取出字符 |
substring('Thomas' from 2 for 3) |
hom |
substring(string frompattern) |
text |
取出字符,从头或尾 |
substring('Thomas' from '...$') |
mas |
substring(string frompattern for escape) |
text |
提取匹配SQL正则表达式的子字符串。 |
substring('Thomas' from '%#"o_a#"_' for '#') |
oma |
trim([leading | trailing | both] [characters] fromstring) |
text |
从字符串的开始、结束或两端(都是默认的),删除包含字符(默认为空格)的字符串。 |
trim(both 'xyz' from 'yxTomxx') |
Tom |
trim([leading | trailing | both] [from] string [,characters] ) |
text |
trim的另一种形式(非标准形式) |
trim(both from 'yxTomxx', 'xyz') |
Tom |
upper(string) |
text |
转换成大写 |
upper('tom') |
TOM |
一些不太常用的字符串函数。
Function |
Return Type |
Description |
Example |
Result |
ascii(string) |
int |
第一个字符的ASCII码 |
ascii('x') |
120 |
btrim(string text [, characterstext]) |
text |
从字符串的开始、结束或两端(都是默认的),删除包含字符(默认为空格)的字符串。 |
btrim('xyxtrimyyx', 'xyz') |
trim |
chr(int) |
text |
将给的数字通过ASCII码转换为字符 |
chr(65) |
A |
concat(str "any" [, str "any" [, ...] ]) |
text |
连接所有字符 |
concat('abcde', 2, NULL, 22) |
abcde222 |
concat_ws(sep text, str "any" [,str "any" [, ...] ]) |
text |
用第一个参数给定的分隔符,连接后边的字符串 |
concat_ws(',', 'abcde', 2, NULL, 22) |
abcde,2,22 |
convert(string bytea,src_encoding name, dest_encodingname) |
bytea |
转换字符编码 |
convert('text_in_utf8', 'UTF8', 'LATIN1') |
text_in_utf8 represented in Latin-1 encoding (ISO 8859-1) |
convert_from(string bytea,src_encoding name) |
text |
转换字符编码 |
convert_from('text_in_utf8', 'UTF8') |
text_in_utf8 represented in the current database encoding |
convert_to(string text,dest_encoding name) |
bytea |
转换字符编码 |
convert_to('some text', 'UTF8') |
some text represented in the UTF8 encoding |
decode(string text, format text) |
bytea |
将给定的字符解码成二进制数,格式的选项与编码中的选项相同。 |
decode('MTIzAAE=', 'base64') |
\x3132330001 |
encode(data bytea, format text) |
text |
编码 |
encode(E'123\\000\\001', 'base64') |
MTIzAAE= |
format(formatstr text [,formatarg "any" [, ...] ]) |
text |
将字符串按照参数返回, |
format('Hello %s, %1$s', 'World') |
Hello World, World |
initcap(string) |
text |
将每个词(空格或标点隔开)的首字母大写,其他字母小写 |
initcap('hi THOMAS') |
Hi Thomas |
left(str text, n int) |
text |
取左边的几个字符 |
left('abcde', 2) |
ab |
length(string) |
int |
字符串长度 |
length('jose') |
4 |
length(string bytea, encodingname ) |
int |
编码为字符集之后的长度 |
length('jose', 'UTF8') |
4 |
lpad(string text, length int [,fill text]) |
text |
截取字符串数值的长度,如果给定数值大于字符串本身长度,用第三个参数循环补充在左侧。 |
lpad('hi', 5, 'xy') |
xyxhi |
ltrim(string text [, characterstext]) |
text |
从字符串的左侧开始删除参数字符串中的字符。 |
ltrim('zzzytest', 'xyz') |
test |
md5(string) |
text |
返回md5加密值 |
md5('abc') |
900150983cd24fb0 d6963f7d28e17f72 |
parse_ident(qualified_identifiertext [, strictmode boolean DEFAULT true ] ) |
text[] |
标识符(“.”隔开)拆分为标识符数组,删除单个标识符的任何引用。默认情况下,最后一个标识符之后的额外字符被认为是错误;但是如果第二个参数为false,那么这些额外字符将被忽略。 |
parse_ident('"SomeSchema".someTable') |
{SomeSchema,sometable} |
pg_client_encoding() |
name |
客户端当前解码 |
pg_client_encoding() |
SQL_ASCII |
quote_ident(string text) |
text |
返回适当引用的给定字符串,作为一个标识符使用在SQL语句字符串中。(给字符串加双引号) |
quote_ident('Foo bar') |
"Foo bar" |
quote_literal(string text) |
text |
返回适当引用的给定字符串,以作为SQL语句字符串中的字符串文字。嵌入式单引号和反斜线被适当加倍。(给字符串加单引号) |
quote_literal(E'O\'Reilly') |
'O''Reilly' |
quote_literal(value anyelement) |
text |
将给定值强制为文本,然后将其引用为文字。嵌入式单引号和反斜线被适当加倍。 |
quote_literal(42.5) |
'42.5' |
quote_nullable(string text) |
text |
返回一个给定字符串,该字符串被适当引用为一个字符串SQL语句,或者,如果参数为NULL,则返回null。 |
quote_nullable(NULL) |
NULL |
quote_nullable(value anyelement) |
text |
将给定值强制为文本,然后将其引用为文字。嵌入式单引号和反斜线被适当加倍。 |
quote_nullable(42.5) |
'42.5' |
regexp_match(string text,pattern text [, flags text]) |
text[] |
根据正则表达式匹配字符串,分组, |
regexp_match('foobarbequebaz', '(bar)(beque)') |
{bar,beque} |
regexp_matches(string text,pattern text [, flags text]) |
setof text[] |
根据正则表达式匹配字符串,分别组成array |
regexp_matches('foobarbequebaz', 'ba.', 'g') |
{bar} {baz} (2 rows) |
regexp_replace(string text,pattern text, replacement text[, flags text]) |
text |
通过正则表达式匹配,替换 |
regexp_replace('Thomas', '.[mN]a.', 'M') |
ThM |
regexp_split_to_array(stringtext, pattern text [, flags text]) |
text[] |
通过正则表达式匹配风格,将字符串分割成数组返回 |
regexp_split_to_array('hello world', E'\\s+') |
{hello,world} |
regexp_split_to_table(stringtext, pattern text [, flagstext]) |
setof text |
通过正则表达式匹配分割,将字符串分成表返回 |
regexp_split_to_table('hello world', E'\\s+') |
hello world (2 rows) |
repeat(string text, number int) |
text |
将字符串重复几次 |
repeat('Pg', 4) |
PgPgPgPg |
replace(string text, from text,to text) |
text |
替换 |
replace('abcdefabcdef', 'cd', 'XX') |
abXXefabXXef |
reverse(str) |
text |
将字符反转 |
reverse('abcde') |
edcba |
right(str text, n int) |
text |
取从右边数的第几个字符之后。 |
right('abcde', 2) |
de |
rpad(string text, length int [,fill text]) |
text |
截取字符串数值的长度,如果给定数值大于字符串本身长度,用第三个参数循环补充在右侧。 |
rpad('hi', 5, 'xy') |
hixyx |
rtrim(string text [, characterstext]) |
text |
从字符串结尾删除匹配出所有子字符串中的任何字符 |
rtrim('testxxzx', 'xyz') |
test |
split_part(string text,delimiter text, field int) |
text |
使用分割符分开字符串后,取出第几个字符串 |
split_part('abc~@~def~@~ghi', '~@~', 2) |
def |
strpos(string, substring) |
int |
定位给出的子字符串在字符串中的开始位置 |
strpos('high', 'ig') |
2 |
substr(string, from [, count]) |
text |
取出从第from个字符开始的几个(count)字符(same as substring(stringfrom from forcount)) |
substr('alphabet', 3, 2) |
ph |
to_ascii(string text [, encodingtext]) |
text |
转换编码(仅支持转换from LATIN1,LATIN2, LATIN9, and WIN1250 encodings) |
to_ascii('Karel') |
Karel |
to_hex(number int or bigint) |
text |
转换为十六进制数 |
to_hex(2147483647) |
7fffffff |
translate(string text, fromtext, to text) |
text |
替换 |
translate('12345', '143', 'ax') |
a2x5 |