PGsql学习笔记:函数和操作符(二)

4、字符串函数和操作符

SQL字符串函数和操作符


函数/操作符
描述
例子


text || text → text
连接两个字符串。
‘Post’ || ‘greSQL’ → PostgreSQL


text || anynonarray → text
anynonarray || text → text
将非字符串输入转换为文本,然后将两个字符串串联在一起
'Value: ’ || 42 → Value: 42


text IS [NOT] [form] NORMALIZED → boolean
检查字符串是否在指定的 Unicode 规范化表单中。 可选的form关键词指定表单:NFC (默认的), NFD, NFKC, 或 NFKD。 只有在服务器编码为UTF8时,才能使用此表达式。 请注意,使用这个表达式检查规范化通常比规范化可能已经规范化的字符串要快。
U&‘\0061\0308bc’ IS NFD NORMALIZED → t


bit_length ( text ) → integer
返回字符串中的位数(8倍于octet_length)。
bit_length(‘jose’) → 32


char_length ( text ) → integer
character_length ( text ) → integer
返回字符串中的字符数。
char_length(‘josé’) → 4


lower ( text ) → text
根据数据库的语言环境规则,将字符串转换为全部小写。
lower(‘TOM’) → tom


normalize ( text [, form ] ) → text
将字符串转换为指定的Unicode规范化形式。 可选的form关键字指定了如下形式:NFC (the default),NFD, NFKC,或NFKD。 该函数只能在服务器编码为UTF8时使用。
normalize(U&‘\0061\0308bc’, NFC) → U&‘\00E4bc’


octet_length ( text ) → integer
返回字符串的字节数。
octet_length(‘josé’) → 5 (if server encoding is UTF8)


octet_length ( character ) → integer
返回字符串中的字节数。 由于此版本的函数直接接受character类型,它不会剥离尾随空格。
octet_length('abc '::character(4)) → 4


overlay ( string text PLACING newsubstring text FROM start integer [ FOR count integer ] ) → text
替换string从start字符开始的子串,并用newsubstring扩展到count字符。 如果省略了count,则默认为newsubstring的长度。
overlay(‘Txxxxas’ placing ‘hom’ from 2 for 4) → Thomas


position ( substring text IN string text ) → integer
返回指定的substring在string起始索引,如果不存在则返回零,。
position(‘om’ in ‘Thomas’) → 3


substring ( string text [ FROM start integer ] [ FOR count integer ] ) → text
如果已指定,提取string从start字符开始的子串, 并且在count字符后停止。如果已指定的话。 提供至少一个start和count中的至少一个。
substring(‘Thomas’ from 2 for 3) → hom
substring(‘Thomas’ from 3) → omas
substring(‘Thomas’ for 2) → Th


substring ( string text FROM pattern text ) → text
提取匹配POSIX正则表达式的子字符串
substring(‘Thomas’ from ‘…$’) → mas


substring ( string text FROM pattern text FOR escape text ) → text
提取匹配 SQL 正则表达式的字串
substring(‘Thomas’ from ‘%#“o_a#”_’ for ‘#’) → oma


trim ( [ LEADING | TRAILING | BOTH ] [ characters text ] FROM string text ) → text
从string的开始、末端或两端(默认为BOTH )移除仅包含characters(默认为空格)字符的最长字符串。
trim(both ‘xyz’ from ‘yxTomxx’) → Tom


trim ( [ LEADING | TRAILING | BOTH ] [ FROM ] string text [, characters text ] ) → text
这是一个非标准的trim()语法。
trim(both from ‘yxTomxx’, ‘xyz’) → Tom


upper ( text ) → text
根据数据库的定位规则,将字符串转换为所有大写。
upper(‘tom’) → TOM


其他字符串函数


函数
描述
例子


ascii ( text ) → integer
返回参数的第一个字符的数字代码。在UTF8编码中,返回该字符的Unicode代码点。 在其他多字节编码中,该参数必须是一个ASCII字符。
ascii(‘x’) → 120


btrim ( string text [, characters text ] ) → text
从string的开头或结尾删除最长的只包含characters(默认是一个空格)的字符串
btrim(‘xyxtrimyyx’, ‘xyz’) → trim


chr ( integer ) → text
返回给定代码的字符。在UTF8编码中该参数被视作一个Unicode代码点。 在其他多字节编码中该参数必须指定一个ASCII字符。 chr(0) 字符不被允许,因为文本数据类型不能存储这种字符。
chr(65) → A


concat ( val1 “any” [, val2 “any” [, …] ] ) → text
连接所有参数的文本表示。空参数被忽略。
concat(‘abcde’, 2, NULL, 22) → abcde222


concat_ws ( sep text, val1 “any” [, val2 “any” [, …] ] ) → text
用分隔符连接除第一个参数外的所有参数。第一个参数用作分隔符字符串,不应为NULL。其他NULL参数将被忽略。
concat_ws(‘,’, ‘abcde’, 2, NULL, 22) → abcde,2,22


format ( formatstr text [, formatarg “any” [, …] ] ) → text
根据格式字符串对参数进行格式化;这个函数类似于C函数 sprintf。
format(‘Hello %s, %1$s’, ‘World’) → Hello World, World


initcap ( text ) → text
将每个单词的第一个字母转换为大写,其余字母转换为小写。单词是由非字母数字字符分隔的字母数字字符序列。
initcap(‘hi THOMAS’) → Hi Thomas


left ( string text, n integer ) → text
以字符串返回第一个 n 字符,或在 n 为负时, 返回最后 |n| 个字符之外的全部字符。
left(‘abcde’, 2) → ab


length ( text ) → integer
返回字符串中的字符数。
length(‘jose’) → 4


lpad ( string text, length integer [, fill text ] ) → text
将string扩展为长度length,通过前置字符fill(默认空格)。 如果string已经超过length那么它将被截断(在右侧)。
lpad(‘hi’, 5, ‘xy’) → xyxhi


ltrim ( string text [, characters text ] ) → text
从string开始删除包含characters(默认空格)中仅包含字符的最长字符串。
ltrim(‘zzzytest’, ‘xyz’) → test


md5 ( text ) → text
计算参数的 MD5 hash ,结果以十六进制形式写入。
md5(‘abc’) → 900150983cd24fb0​d6963f7d28e17f72


parse_ident ( qualified_identifier text [, strict_mode boolean DEFAULT true ] ) → text[]
将qualified_identifier拆分为一个标识符数组,删除单个标识符的任何引用。 默认情况下,最后一个标识符之后的额外字符被视为错误;但是,如果第二个参数为false,则忽略这些额外的字符。 (这种行为对于解析类似函数的对象的名称有作用。) 请注意,此函数不会截断超长标识符。如果你想截断,你可以把结果给到name[]。
parse_ident(‘“SomeSchema”.someTable’) → {SomeSchema,sometable}


pg_client_encoding ( ) → name
返回当前客户端编码名称。
pg_client_encoding() → UTF8


quote_ident ( text ) → text
返回适合引用的给定字符串,作为SQL语句字符串中的标识符。 只有在必要的情况下才添加引号(例如,如果字符串包含非标识符字符或将被大小写折叠)。 嵌入的引号被适当地加双引号。
quote_ident(‘Foo bar’) → “Foo bar”


quote_literal ( text ) → text
返回在SQL语句字符串中适当引用的给定字符串,用作字符串文字使用。 嵌入式单引号和反斜线适当的翻倍(转双引号或双斜线)。 请注意,quote_literal返回无效输入;如果这个参数可能为空,quote_nullable通常更合适。
quote_literal(E’O’Reilly’) → ‘O’‘Reilly’


quote_literal ( anyelement ) → text
将给定的值转换为文本,然后将其作为字面量引用。 内嵌的单引号和反斜杠被适当地翻倍。
quote_literal(42.5) → ‘42.5’


quote_nullable ( text ) → text
返回在SQL语句字符串中适当引用的给定字符串文字;或者,如果参数为null,则返回NULL。 内嵌的单引号和反斜杠被适当地翻倍。
quote_nullable(NULL) → NULL


quote_nullable ( anyelement ) → text
将给定值转换为文本,然后将其作为字面量引用;或者,如果参数为null,则返回NULL。 内嵌的单引号和反斜杠被适当地翻倍。
quote_nullable(42.5) → ‘42.5’


regexp_match ( string text, pattern text [, flags text ] ) → text[]
返回从POSIX正则表达式到string的第一个匹配中捕获的子字符串。
regexp_match(‘foobarbequebaz’, ‘(bar)(beque)’) → {bar,beque}


regexp_matches ( string text, pattern text [, flags text ] ) → setof text[]
返回通过将POSIX正则表达式与string匹配而捕获的子字符串。
regexp_matches(‘foobarbequebaz’, ‘ba.’, ‘g’) →
{bar}
{baz}


regexp_replace ( string text, pattern text, replacement text [, flags text ] ) → text
替换匹配POSIX正则表达式的子字符串。
regexp_replace(‘Thomas’, ‘.[mN]a.’, ‘M’) → ThM


regexp_split_to_array ( string text, pattern text [, flags text ] ) → text[]
使用POSIX正则表达式作为分隔符拆分string。
regexp_split_to_array(‘hello world’, ‘\s+’) → {hello,world}


regexp_split_to_table ( string text, pattern text [, flags text ] ) → setof text
使用POSIX正则表达式作为分隔符拆分string。
regexp_split_to_table(‘hello world’, ‘\s+’) →

hello
world


repeat ( string text, number integer ) → text
重复string指定number的次数。
repeat(‘Pg’, 4) → PgPgPgPg


replace ( string text, from text, to text ) → text
将string 中当前的子串from替换为子串to。
replace(‘abcdefabcdef’, ‘cd’, ‘XX’) → abXXefabXXef


reverse ( text ) → text
颠倒字符串中字符的顺序。
reverse(‘abcde’) → edcba


right ( string text, n integer ) ) → text
返回字符串中的最后n个字符,或者在n>为负时,返回除了前面的|n|字符之外的所有字符。
right(‘abcde’, 2) → de


rpad ( string text, length integer [, fill text ] ) ) → text
扩展 string 到长度 length,通过追加fill 字符(默认为空格). 如果string 已经比 length 长,则截断它。
rpad(‘hi’, 5, ‘xy’) → hixyx


rtrim ( string text [, characters text ] ) → text
从string末尾删除包含characters(默认为空格)中仅包含字符的最长字符串。
rtrim(‘testxxzx’, ‘xyz’) → test


split_part ( string text, delimiter text, n integer ) → text
在delimiter出现时拆分string,并且返回第n个字段(从一计数)。
split_part(‘abc@def@ghi’, ‘@’, 2) → def


strpos ( string text, substring text ) → integer
返回在string中指定的substring的起始索引,如果不存在则为零。 (与(substring 在 string中的)位置相同,但是请注意反转的参数顺序)
strpos(‘high’, ‘ig’) → 2


substr ( string text, start integer [, count integer ] ) → text
提取string从start字符开始的子字符串,并扩展count字符,如果指定了的话。 (与 子字符串(string 从 start 开始计数 count)相同。)
substr(‘alphabet’, 3) → phabet
substr(‘alphabet’, 3, 2) → ph


starts_with ( string text, prefix text ) → boolean
如果 string 以 prefix开始就返回真。
starts_with(‘alphabet’, ‘alph’) → t


to_ascii ( string text ) → text
to_ascii ( string text, encoding name ) → text
to_ascii ( string text, encoding integer ) → text
将string从另一个编码中转换为ASCII,该编码可按名称或编号标识。 如果encoding被省略,则假定数据库编码(这在实践中是唯一有用的案例)。转换主要包括降音。 转换仅支持来自 LATIN1、LATIN2、LATIN9、 和 WIN1250 的编码. (其他请参见 unaccent 模块, 更灵活的解决方案。)
to_ascii(‘Karél’) → Karel
to_hex ( integer ) → text


to_hex ( bigint ) → text
将数字转换为其相应的十六进制表示形式。
to_hex(2147483647) → 7fffffff


translate ( string text, from text, to text ) → text
将string中与from集合中匹配的每个字符替换为to集合中相应的字符。 如果from长于to,from中出现的额外字符被删除。
translate(‘12345’, ‘143’, ‘ax’) → a2x5


5、 二进制串函数和操作符

二进制串函数和操作符


函数/操作符
描述
例子


bytea || bytea → bytea
连接两个二进制字符串。
‘\x123456’::bytea || ‘\x789a00bcde’::bytea → \x123456789a00bcde


bit_length ( bytea ) → integer
返回二进制字符串中的位数 (8 倍于 octet_length).
bit_length(‘\x123456’::bytea) → 24


octet_length ( bytea ) → integer
返回二进制字符串中的字节数。
octet_length(‘\x123456’::bytea) → 3


overlay ( bytes bytea PLACING newsubstring bytea FROM start integer [ FOR count integer ] ) → bytea
将bytes的子字符串替换为newsubstring,该子字符串从start字节开始,并以count字节扩展。 如果忽略了count,则默认为newsubstring的长度。
overlay(‘\x1234567890’::bytea placing ‘\002\003’::bytea from 2 for 3) → \x12020390


position ( substring bytea IN bytes bytea ) → integer
返回指定的substring在bytes内的起始索引,如果不存在,则为零。
position(‘\x5678’::bytea in ‘\x1234567890’::bytea) → 3


substring ( bytes bytea [ FROM start integer ] [ FOR count integer ] ) → bytea
提取bytes从start字节开始的子字符串,如果指定了,并且在count字节之后停止,如果指定了的话。 至少提供start和count中的一个。
substring(‘\x1234567890’::bytea from 3 for 2) → \x5678


trim ( [ BOTH ] bytesremoved bytea FROM bytes bytea ) → bytea
从bytes的开始和结束处删除bytesremoved中只包含字节的最长字符串。
trim(‘\x9012’::bytea from ‘\x1234567890’::bytea) → \x345678


trim ( [ BOTH ] [ FROM ] bytes bytea, bytesremoved bytea ) → bytea
这是trim()的非标准语法。
trim(both from ‘\x1234567890’::bytea, ‘\x9012’::bytea) → \x345678


其他二进制串函数


函数
描述
例子


btrim ( bytes bytea, bytesremoved bytea ) → bytea
从bytes的开始和结束处删除只包含bytesremoved中出现的字节的最长字符串
btrim(‘\x1234567890’::bytea, ‘\x9012’::bytea) → \x345678


get_bit ( bytes bytea, n bigint ) → integer
从二进制字符串中提取 n’th 位。
get_bit(‘\x1234567890’::bytea, 30) → 1


get_byte ( bytes bytea, n integer ) → integer
从二进制字符串中提取 n’th 字节。
get_byte(‘\x1234567890’::bytea, 4) → 144


length ( bytea ) → integer
返回二进制字符串中的字节数。
length(‘\x1234567890’::bytea) → 5


length ( bytes bytea, encoding name ) → integer
返回二进制字符串中的字符数,假设它是给定encoding中的文本。
length(‘jose’::bytea, ‘UTF8’) → 4


md5 ( bytea ) → text
计算二进制字符串的MD5 hash,结果以十六进制形式写入。
md5(‘Th\000omas’::bytea) → 8ab2d3c9689aaf18​b4958c334c82d8b1


set_bit ( bytes bytea, n bigint, newvalue integer ) → bytea
设置二进制字符串中的n’th位为newvalue。
set_bit(‘\x1234567890’::bytea, 30, 0) → \x1234563890


set_byte ( bytes bytea, n integer, newvalue integer ) → bytea
设置二进制字符串中的 n’th 字节到 newvalue。
set_byte(‘\x1234567890’::bytea, 4, 64) → \x1234567840


sha224 ( bytea ) → bytea
计算二进制字符串的 SHA-224 hash。
sha224(‘abc’::bytea) → \x23097d223405d8228642a477bda2​55b32aadbce4bda0b3f7e36c9da7


sha256 ( bytea ) → bytea
计算二进制字符串的 SHA-256 hash。
sha256(‘abc’::bytea) → \xba7816bf8f01cfea414140de5dae2223​b00361a396177a9cb410ff61f20015ad


sha384 ( bytea ) → bytea
计算二进制字符串的 SHA-384 hash。
sha384(‘abc’::bytea) → \xcb00753f45a35e8bb5a03d699ac65007​272c32ab0eded1631a8b605a43ff5bed​8086072ba1e7cc2358baeca134c825a7


sha512 ( bytea ) → bytea
计算二进制字符串的 SHA-512 hash。
sha512(‘abc’::bytea) → \xddaf35a193617abacc417349ae204131​12e6fa4e89a97ea20a9eeee64b55d39a​2192992a274fc1a836ba3c23a3feebbd​454d4423643ce80e2a9ac94fa54ca49f


substr ( bytes bytea, start integer [, count integer ] ) → bytea
从start字节开始提取bytes的子字符串,并扩展为count字节,如果这是指定的。 (与 substring(bytes 从 start 到 count) 相同.)
substr(‘\x1234567890’::bytea, 3, 2) → \x5678


Text/Binary String Conversion Functions


函数
描述
例子


convert ( bytes bytea, src_encoding name, dest_encoding name ) → bytea
将表示编码src_encoding的文本的二进制字符串转换为编码dest_encoding的二进制字符串
convert(‘text_in_utf8’, ‘UTF8’, ‘LATIN1’) → \x746578745f696e5f75746638


convert_from ( bytes bytea, src_encoding name ) → text
将表示编码src_encoding的文本的二进制字符串转换为数据库编码中的text。
convert_from(‘text_in_utf8’, ‘UTF8’) → text_in_utf8


convert_to ( string text, dest_encoding name ) → bytea
将text字符串(数据库编码)转换为编码dest_encoding中编码的二进制字符串。
convert_to(‘some_text’, ‘UTF8’) → \x736f6d655f74657874


encode ( bytes bytea, format text ) → text
将二进制数据编码成文本表示;支持的format值为: base64, escape, hex.
encode(‘123\000\001’, ‘base64’) → MTIzAAE=


decode ( string text, format text ) → bytea
从文本表示中解码二进制数据;支持的format值与encode相同。
decode(‘MTIzAAE=’, ‘base64’) → \x3132330001


6、位串函数和操作符

位串操作符


操作符
描述
例子


bit || bit → bit
连接
B’10001’ || B’011’ → 10001011


bit & bit → bit
按位与(输入的长度必须相等)
B’10001’ & B’01101’ → 00001


bit | bit → bit
按位或 (输入的长度必须相等)
B’10001’ | B’01101’ → 11101


bit # bit → bit
按位异或 (输入的长度必须相等)
B’10001’ # B’01101’ → 11100


~ bit → bit
按位求反
~ B’10001’ → 01110


bit << integer → bit
按位左移(字符串长度被保留)
B’10001’ << 3 → 01000


bit >> integer → bit
按位右移(字符串长度被保留)
B’10001’ >> 2 → 00100


位字符串函数


函数
描述
例子


bit_length ( bit ) → integer
返回位字符串中的位数。
bit_length(B’10111’) → 5


length ( bit ) → integer
返回位字符串中的位数。
length(B’10111’) → 5


octet_length ( bit ) → integer
返回位字符串中的字节数。
octet_length(B’1011111011’) → 2


overlay ( bits bit PLACING newsubstring bit FROM start integer [ FOR count integer ] ) → bit
替换从start位开始的bits的子字符串,并将newsubstring扩展count位。 如果count被省略,默认为newsubstring的长度。
overlay(B’01010101010101010’ placing B’11111’ from 2 for 3) → 0111110101010101010


position ( substring bit IN bits bit ) → integer
按返回指定substring的起始索引,以bits为单位,如果不存在则返回0。
position(B’010’ in B’000001101011’) → 8


substring ( bits bit [ FROM start integer ] [ FOR count integer ] ) → bit
如果指定了起始位,则提取从start位开始的bits的子字符串,如果指定了计数位,则在count位之后停止。 start和count至少提供一个。
substring(B’110010111111’ from 3 for 2) → 00


get_bit ( bits bit, n integer ) → integer
从位字符串中提取第n位;第一个(最左)位为第0位。
get_bit(B’101010101010101010’, 6) → 1


set_bit ( bits bit, n integer, newvalue integer ) → bit
将位字符串中的第n位设置为newvalue;第一个(最左)位是第0位。
set_bit(B’101010101010101010’, 6, 0) → 101010001010101010


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值