↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
历史版本。 PostgreSQL 8.4 已结束支持。 请参阅 当前版本手册.

9.4. 字符串函数和操作符 #

本节描述用于检查和操作字符串值的函数和操作符。这里的字符串包括 character、character varying 和 text 类型的值。除非另有说明,下面列出的所有函数都适用于这些类型,但使用 character 类型时,应注意自动填充空格可能带来的影响。一些函数也为位串类型提供了原生实现。

SQL定义了一些字符串函数,它们使用关键字而不是逗号来分隔参数。详情见表 9.5。PostgreSQL也提供了这些函数使用正常函数调用语法的版本(见表 9.6)。

注意

在 PostgreSQL 8.3 之前,由于某些非字符串数据类型到 text 的隐式强制转换,这些函数也会悄然接受那些类型的值。由于这些强制转换经常产生出人意料的行为,它们已被移除。不过,字符串串接操作符(||)仍接受非字符串输入,只要至少一个输入属于字符串类型,如表 9.5所示。对于其他情况,如果需要重现以前的行为,可添加到 text 的显式强制转换。

表 9.5. SQL字符串函数和操作符

函数返回类型描述示例结果
string || string text 字符串串接 'Post' || 'greSQL'PostgreSQL
string || non-string or non-string || string text 用一个非字符串输入进行的字符串连接 'Value: ' || 42Value: 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 placing string from int [for int])text替换子串overlay('Txxxxas' placing 'hom' from 2 for 4)Thomas
position(substring in string)int指定子串的位置position('om' in 'Thomas')3
substring(string [from int] [for int])text 提取子串 substring('Thomas' from 2 for 3)hom
substring(string from pattern)text提取匹配 POSIX 正则表达式的子字符串。有关模式匹配的更多信息,参见第 9.7 节。substring('Thomas' from '...$')mas
substring(string from pattern for escape)text 提取匹配 SQL 正则表达式的子串。模式匹配的更多信息参见第 9.7 节。 substring('Thomas' from '%#"o_a#"_' for '#')oma
trim([leading | trailing | both] [characters] from string) text 移除string开始端/结束端/两端(默认为两端)仅含characters(默认为空格)的最长字符串 trim(both 'x' from 'xTomxx')Tom
upper(string)text范围的上界upper('tom')TOM

还有其他字符串操作函数,列在表 9.6中。其中一些用于内部实现表 9.5中列出的 SQL 标准字符串函数。

表 9.6. 其他字符串函数

函数返回类型描述示例结果
ascii(string)int 参数第一个字符的ASCII码。对于UTF8返回该字符的 Unicode 码点。对于其他多字节编码,参数必须是一个ASCII字符。 ascii('x')120
btrim(string text [, characters text])text 移除string开始端和结束端仅由characters中的字符(默认为空格)组成的最长字符串 btrim('xyxtrimyyx', 'xy')trim
chr(int)text 具有给定代码的字符。对于UTF8,参数被当作 Unicode 码点。对于其他多字节编码,参数必须指定一个ASCII字符。NULL(0)字符不被允许,因为 text 数据类型无法存储这样的字节。 chr(65)A
convert(string bytea, src_encoding name, dest_encoding name) bytea 把字符串转换为dest_encoding编码。原始编码由src_encoding指定。string在该编码下必须有效。可以用CREATE CONVERSION定义转换,也有一些预定义的转换。可用转换见表 9.7。 convert('text_in_utf8', 'UTF8', 'LATIN1')以 Latin-1(ISO 8859-1)编码表示的text_in_utf8
convert_from(string bytea, src_encoding name) text 把字符串转换为数据库编码。原始编码由src_encoding指定。string在该编码下必须有效。 convert_from('text_in_utf8', 'UTF8')以当前数据库编码表示的 text_in_utf8
convert_to(string text, dest_encoding name) bytea 把字符串转换为dest_encoding编码。 convert_to('some text', 'UTF8')以 UTF8 编码表示的 some text
decode(string text, format text) bytea 从string中的文本表示解码二进制数据。format的选项与encode中的相同。 decode('MTIzAAE=', 'base64')x3132330001
encode(data bytea, format text) text 把二进制数据编码为文本表示。支持的格式有:base64、hex、escape。escape将零字节和高位置位的字节转换为八进制序列(\nnn),并将反斜线双写。 encode(E'123\\000\\001', 'base64')MTIzAAE=
initcap(string)text 把每个词的第一个字母转换为大写,其余字母转换为小写。词是由字母数字 characters separated by non-alphanumeric characters. initcap('hi THOMAS')Hi Thomas
length(string)intstring中的字符数length('jose')4
length(stringbytea, encoding name )int采用给定 encoding 的 string 中的字符数。string 必须是该编码下的有效字符串。length('jose', 'UTF8')4
lpad(string text, length int [, fill text]) text 通过在前面重复字符fill(默认为空格)把string填充到长度length。如果string已经比length长,则将其截断(在右侧)。 lpad('hi', 5, 'xy')xyxhi
ltrim(string text [, characters text]) text 移除string开始端仅含characters(默认为空格)的最长字符串 ltrim('zzzytrim', 'xyz')trim
md5(string)text 计算string的 MD5 哈希,以十六进制返回结果 md5('abc')900150983cd24fb0 d6963f7d28e17f72
pg_client_encoding()name当前客户端编码名称pg_client_encoding()SQL_ASCII
quote_ident(string text)text 返回给定字符串的适当加引号形式,使其可以在SQL语句字符串中用作标识符。只在必要时才加引号(即,当字符串包含非标识符字符或会被大小写折叠时)。内嵌的引号会被适当地双写。另见例 38.1。 quote_ident('Foo bar')"Foo bar"
quote_literal(string text)text 返回给定字符串的适当加引号形式,使其可以在SQL语句字符串中用作字符串字面量。内嵌的单引号和反斜线会被适当地双写。注意quote_literal对空值输入返回 null;如果参数可能为 null,quote_nullable通常更合适。另见例 38.1。 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。内嵌的单引号和反斜线会被适当地双写。另见例 38.1。 quote_nullable(NULL)NULL
quote_nullable(value anyelement)text将给定值强制转换为文本,然后作为字面量加引号;如果参数为 null,则返回 NULL。内嵌单引号和反斜杠会被适当地双写。quote_nullable(42.5)'42.5'
regexp_matches(string text, pattern text [, flags text])setof text[] 返回把 POSIX 正则表达式与string匹配所得到的全部捕获子串。更多信息见第 9.7.3 节。 regexp_matches('foobarbequebaz', '(bar)(beque)'){bar,beque}
regexp_replace(string text, pattern text, replacement text [, flags text])text 替换匹配 POSIX 正则表达式的子串。更多信息参见第 9.7.3 节。 regexp_replace('Thomas', '.[mN]a.', 'M')ThM
regexp_split_to_array(string text, pattern text [, flags text ])text[] 使用 POSIX 正则表达式作为分隔符拆分 string。更多信息参见第 9.7.3 节。 regexp_split_to_array('hello world', E'\\s+'){hello,world}
regexp_split_to_table(string text, pattern text [, flags text])setof text 使用 POSIX 正则表达式作为分隔符拆分 string。更多信息参见第 9.7.3 节。 regexp_split_to_table('hello world', E'\\s+')hello

world

(2 rows)
repeat(string text, number int)text将string重复指定次数(number)repeat('Pg', 4)PgPgPgPg
replace(string text, from text, to text)text将 string 中所有出现的子串 from 替换为子串 to replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
rpad(string text, length int [, fill text]) text 把string填充到长度 length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. rpad('hi', 5, 'xy')hixyx
rtrim(string text [, characters text]) text 移除string结束端仅含characters(默认为空格)的最长字符串 rtrim('trimxxxx', 'x')trim
split_part(string text, delimiter text, field int)text按delimiter分隔string,并返回给定字段(从 1 开始计数) split_part('abc~@~def~@~ghi', '~@~', 2)def
strpos(string, substring)int 指定子串的位置(与 position(substring in string) 相同,但注意参数顺序相反) strpos('high', 'ig')2
substr(string, from [, count])text 提取子串(与 substring(string from from for count) 相同) substr('alphabet', 3, 2)ph
to_ascii(string text [, encoding text])text 将 string 从另一种编码转换为 ASCII(只支持从 LATIN1、LATIN2、LATIN9 和 WIN1250 编码转换) to_ascii('Karel')Karel
to_hex(number int or bigint)text将 number 转换为其等价的十六进制表示 to_hex(2147483647)7fffffff
translate(string text, from text, to text) text string中任何与from集合中字符匹配的字符,都会被替换成to集合中的相应字符 translate('12345', '14', 'ax')a23x5

表 9.7. 内置转换

Conversion Name [a] 源编码目标编码
ascii_to_micSQL_ASCIIMULE_INTERNAL
ascii_to_utf8SQL_ASCIIUTF8
big5_to_euc_twBIG5EUC_TW
big5_to_micBIG5MULE_INTERNAL
big5_to_utf8BIG5UTF8
euc_cn_to_micEUC_CNMULE_INTERNAL
euc_cn_to_utf8EUC_CNUTF8
euc_jp_to_micEUC_JPMULE_INTERNAL
euc_jp_to_sjisEUC_JPSJIS
euc_jp_to_utf8EUC_JPUTF8
euc_kr_to_micEUC_KRMULE_INTERNAL
euc_kr_to_utf8EUC_KRUTF8
euc_tw_to_big5EUC_TWBIG5
euc_tw_to_micEUC_TWMULE_INTERNAL
euc_tw_to_utf8EUC_TWUTF8
gb18030_to_utf8GB18030UTF8
gbk_to_utf8GBKUTF8
iso_8859_10_to_utf8LATIN6UTF8
iso_8859_13_to_utf8LATIN7UTF8
iso_8859_14_to_utf8LATIN8UTF8
iso_8859_15_to_utf8LATIN9UTF8
iso_8859_16_to_utf8LATIN10UTF8
iso_8859_1_to_micLATIN1MULE_INTERNAL
iso_8859_1_to_utf8LATIN1UTF8
iso_8859_2_to_micLATIN2MULE_INTERNAL
iso_8859_2_to_utf8LATIN2UTF8
iso_8859_2_to_windows_1250LATIN2WIN1250
iso_8859_3_to_micLATIN3MULE_INTERNAL
iso_8859_3_to_utf8LATIN3UTF8
iso_8859_4_to_micLATIN4MULE_INTERNAL
iso_8859_4_to_utf8LATIN4UTF8
iso_8859_5_to_koi8_rISO_8859_5KOI8R
iso_8859_5_to_micISO_8859_5MULE_INTERNAL
iso_8859_5_to_utf8ISO_8859_5UTF8
iso_8859_5_to_windows_1251ISO_8859_5WIN1251
iso_8859_5_to_windows_866ISO_8859_5WIN866
iso_8859_6_to_utf8ISO_8859_6UTF8
iso_8859_7_to_utf8ISO_8859_7UTF8
iso_8859_8_to_utf8ISO_8859_8UTF8
iso_8859_9_to_utf8LATIN5UTF8
johab_to_utf8JOHABUTF8
koi8_r_to_iso_8859_5KOI8RISO_8859_5
koi8_r_to_micKOI8RMULE_INTERNAL
koi8_r_to_utf8KOI8RUTF8
koi8_r_to_windows_1251KOI8RWIN1251
koi8_r_to_windows_866KOI8RWIN866
koi8_u_to_utf8KOI8UUTF8
mic_to_asciiMULE_INTERNALSQL_ASCII
mic_to_big5MULE_INTERNALBIG5
mic_to_euc_cnMULE_INTERNALEUC_CN
mic_to_euc_jpMULE_INTERNALEUC_JP
mic_to_euc_krMULE_INTERNALEUC_KR
mic_to_euc_twMULE_INTERNALEUC_TW
mic_to_iso_8859_1MULE_INTERNALLATIN1
mic_to_iso_8859_2MULE_INTERNALLATIN2
mic_to_iso_8859_3MULE_INTERNALLATIN3
mic_to_iso_8859_4MULE_INTERNALLATIN4
mic_to_iso_8859_5MULE_INTERNALISO_8859_5
mic_to_koi8_rMULE_INTERNALKOI8R
mic_to_sjisMULE_INTERNALSJIS
mic_to_windows_1250MULE_INTERNALWIN1250
mic_to_windows_1251MULE_INTERNALWIN1251
mic_to_windows_866MULE_INTERNALWIN866
sjis_to_euc_jpSJISEUC_JP
sjis_to_micSJISMULE_INTERNAL
sjis_to_utf8SJISUTF8
tcvn_to_utf8WIN1258UTF8
uhc_to_utf8UHCUTF8
utf8_to_asciiUTF8SQL_ASCII
utf8_to_big5UTF8BIG5
utf8_to_euc_cnUTF8EUC_CN
utf8_to_euc_jpUTF8EUC_JP
utf8_to_euc_krUTF8EUC_KR
utf8_to_euc_twUTF8EUC_TW
utf8_to_gb18030UTF8GB18030
utf8_to_gbkUTF8GBK
utf8_to_iso_8859_1UTF8LATIN1
utf8_to_iso_8859_10UTF8LATIN6
utf8_to_iso_8859_13UTF8LATIN7
utf8_to_iso_8859_14UTF8LATIN8
utf8_to_iso_8859_15UTF8LATIN9
utf8_to_iso_8859_16UTF8LATIN10
utf8_to_iso_8859_2UTF8LATIN2
utf8_to_iso_8859_3UTF8LATIN3
utf8_to_iso_8859_4UTF8LATIN4
utf8_to_iso_8859_5UTF8ISO_8859_5
utf8_to_iso_8859_6UTF8ISO_8859_6
utf8_to_iso_8859_7UTF8ISO_8859_7
utf8_to_iso_8859_8UTF8ISO_8859_8
utf8_to_iso_8859_9UTF8LATIN5
utf8_to_johabUTF8JOHAB
utf8_to_koi8_rUTF8KOI8R
utf8_to_koi8_uUTF8KOI8U
utf8_to_sjisUTF8SJIS
utf8_to_tcvnUTF8WIN1258
utf8_to_uhcUTF8UHC
utf8_to_windows_1250UTF8WIN1250
utf8_to_windows_1251UTF8WIN1251
utf8_to_windows_1252UTF8WIN1252
utf8_to_windows_1253UTF8WIN1253
utf8_to_windows_1254UTF8WIN1254
utf8_to_windows_1255UTF8WIN1255
utf8_to_windows_1256UTF8WIN1256
utf8_to_windows_1257UTF8WIN1257
utf8_to_windows_866UTF8WIN866
utf8_to_windows_874UTF8WIN874
windows_1250_to_iso_8859_2WIN1250LATIN2
windows_1250_to_micWIN1250MULE_INTERNAL
windows_1250_to_utf8WIN1250UTF8
windows_1251_to_iso_8859_5WIN1251ISO_8859_5
windows_1251_to_koi8_rWIN1251KOI8R
windows_1251_to_micWIN1251MULE_INTERNAL
windows_1251_to_utf8WIN1251UTF8
windows_1251_to_windows_866WIN1251WIN866
windows_1252_to_utf8WIN1252UTF8
windows_1256_to_utf8WIN1256UTF8
windows_866_to_iso_8859_5WIN866ISO_8859_5
windows_866_to_koi8_rWIN866KOI8R
windows_866_to_micWIN866MULE_INTERNAL
windows_866_to_utf8WIN866UTF8
windows_866_to_windows_1251WIN866WIN
windows_874_to_utf8WIN874UTF8
euc_jis_2004_to_utf8EUC_JIS_2004UTF8
utf8_to_euc_jis_2004UTF8EUC_JIS_2004
shift_jis_2004_to_utf8SHIFT_JIS_2004UTF8
utf8_to_shift_jis_2004UTF8SHIFT_JIS_2004
euc_jis_2004_to_shift_jis_2004EUC_JIS_2004SHIFT_JIS_2004
shift_jis_2004_to_euc_jis_2004SHIFT_JIS_2004EUC_JIS_2004

[a] 转换名称遵循标准命名规则:将源编码正式名称中的所有非字母数字字符替换为下划线,再接上 _to_,然后接上按相同方式处理的目标编码名称。因此,这些名称可能与惯用的编码名称不同。


报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.