rim([leading | trailing | both] [characters] from string)
text
从字串string的开头/结尾/两边/ 删除只包含characters(缺省是一个空白)的最长的字串
trim(both 'x' from 'xTomxx')
Tom
upper(string)
text
把字串转化为大写。
upper('tom')
TOM
ascii(text)
int
参数第一个字符的ASCII码
ascii('x')
120
btrim(string text [, characters text])
text
从string开头和结尾删除只包含在characters里(缺省是空白)的字符的最长字串
btrim('xyxtrimyyx','xy')
trim
chr(int)
text
给出ASCII码的字符
chr(65)
A
convert(string text, [src_encoding name,] dest_encoding name)
text
把字串转换为dest_encoding
convert( 'text_in_utf8', 'UTF8', 'LATIN1')
以ISO 8859-1编码表示的text_in_utf8
initcap(text)
text
把每个单词的第一个子母转为大写,其它的保留小写。单词是一系列字母数字组成的字符,用非字母数字分隔。
initcap('hi thomas')
Hi Thomas
length(string text)
int
string中字符的数目
length('jose')
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)
text
计算给出string的
MD5散列,以十六进制返回结果。
md5('abc')
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
通过填充字符fill(缺省时为空白),把string填充为长度length。如果string已经比length长则将其截断。
rpad('hi', 5, 'xy')
hixyx www.2cto.com
rtrim(string text [, character text])
text
从字串string的结尾删除只包含character(缺省是个空白)的最长的字
rtrim('trimxxxx','x')
trim
split_part(string text, delimiter text, field int)
text
根据delimiter分隔string返回生成的第field个子字串(1 Base)。
split_part('abc~@~def~@~ghi', '~@~', 2)
def
strpos(string, substring)
text
声明的子字串的位置。
strpos('high','ig')
2
substr(string, from [, count])
text
抽取子字串。
substr('alphabet', 3, 2)
ph
to_ascii(text [, encoding])
text
把text从其它编码转换为ASCII。
to_ascii('Karel')
Karel
to_hex(number int/bigint)
text
把number转换成其对应地十六进制表现形式。
to_hex(9223372036854775807)
7fffffffffffffff
translate(string text, from text, to text)
text
把在string中包含的任何匹配from中的字符的字符转化为对应的在to中的字符。
translate('12345', '14', 'ax')
a23x5
五、位串函数和操作符:
对于类型bit和bit varying,除了常用的比较操作符之外,还可以使用以下列表中由PostgreSQL提供的位串函数和操作符,其中&、|和#的位串操作数必须等长。在移位的时候,保留原始的位串的的长度。
操作符
描述
例子
结果
||
连接
B'10001' || B'011'
10001011
&
按位AND
B'10001' & B'01101'
00001
|
按位OR
B'10001' | B'01101'
11101
#
按位XOR
B'10001' # B'01101'
11100
~
按位NOT
~ B'10001'
01110
<<
按位左移
B'10001' << 3
01000
>>
按位右移
B'10001' >> 2
00100
除了以上列表中提及的操作符之外,位串还可以使用字符串函数:length, bit_length, octet_length, position, substring。此外,我们还可以在整数和bit之间来回转换,如:
MyTest=# SELECT 44::bit(10);
bit
------------
0000101100
(1 row)
MyTest=# SELECT 44::bit(3);
bit www.2cto.com
-----
100
(1 row)
MyTest=# SELECT cast(-44 as bit(12));
bit
--------------
111111010100
(1 row)
MyTest=# SELECT '1110'::bit(4)::integer;
int4
------
14
(1 row)
注意:如果只是转换为"bit",意思是转换成bit(1),因此只会转换成整数的最低位。
该博客中提供的所有信息均源自PostgreSQL官方文档,编写该篇博客的主要目的是便于今后的查阅,特此声明。
作者 Stephen_Liu