PostgreSQL string functions and operators

function return type describe example result
string || string text string concatenation 'Post'丨丨'greSQL' PostgreSQL
bit_length(string) int the number of binary bits in the string bit_length('jose') 32
char_length(string) int the number of characters in the string char_length('jose') 4
convert(string using conversion_name) text Change the encoding using the specified conversion name. convert('PostgreSQL' using iso_8859_1_to_utf8) 'PostgreSQL'
lower(string) text convert string to lowercase lower('TOM') tom
octet_length(string) int the number of bytes in the string octet_length('jose') 4
overlay(string placing string from int [for int]) text replace substring overlay('Txxxxas' placing 'hom' from 2 for 4) Thomas
position(substring in string) int the position of the specified substring position('om' in 'Thomas') 3
substring(string [from int] [for int]) text extract substring substring('Thomas' from 2 for 3) him
substring(string from pattern) text extract substring matching POSIX regular expression substring('Thomas' from '…$') but
substring(string from pattern for escape) text Extract substring matching SQL regular expression substring('Thomas' from '%#"o_a#"_' for '#') own
trim([leading丨trailing 丨 both] [characters] from string) text Remove the longest string containing only characters (default is a blank) from the beginning/end/both sides of string trim(both 'x' from 'xTomxx') Tom
upper(string) text Convert a string to uppercase. upper('tom') TOM
ascii(text) int The ASCII code of the first character of the parameter ascii('x') 120
btrim(string text [, characters text]) text Remove the longest string of characters only contained in characters (blank by default) from the beginning and end of string btrim('xyxtrimyyx','xy') trim
chr(int) text Gives the ASCII code for the character chr(65) A
convert(string text, [src_encoding name,] dest_encoding name) text Convert string to dest_encoding convert( 'text_in_utf8', 'UTF8', 'LATIN1') text_in_utf8 expressed in ISO 8859-1 encoding
initcap(text) text Capitalize the first consonant of each word and keep the rest lowercase. A word is a sequence of alphanumeric characters separated by non-alphanumeric characters. initcap('hi thomas') Hi Thomas
length(string text) int number of characters in string length('jose') 4
lpad(string text, length int [, fill text]) text Fill the string with the length length by filling the character fill (default is blank). Truncates string (on the right) if it is already longer than length. lpad('hi', 5, 'xy') xyxhi
ltrim(string text [, characters text]) text Remove the longest string containing only characters (a blank by default) from the beginning of string. ltrim('zzzytrim','xyz') trim
md5(string text) text Computes the MD5 hash of the given string, returning the result in hexadecimal. md5('abc')
repeat(string text, number int) text Repeat string number times. repeat('Pg', 4) PgPgPgPg
replace(string text, from text, to text) text Replace all occurrences of the substring from in the string string with the substring to. replace('abcdefabcdef', 'cd', 'XX') abXXefabXXef
rpad(string text, length int [, fill text]) text Fill the string with the length length by filling the character fill (default is blank). If string is already longer than length it is truncated. rpad('hi', 5, 'xy') hixyx
rtrim(string text [, character text]) text Remove the longest character containing only character (blank by default) from the end of string 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

Guess you like

Origin blog.csdn.net/yeyaozhifengqi/article/details/130389432