PostgreSql string functions and operators

1. Standard SQL 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 digits in the string bit_length(‘jose’) 32
char_length(string) int the number of characters in the string char_length('database') 3
convert(string using conversion_name) text Use the conversion name to change the encoding, the conversion is defined by create conversion convert(‘PostgreSQL’ using ios_8859_1_to_utf8) UTF8 encoded 'PostgreSQL'
lower(string) text string to lowercase low(‘TOM’) tom
octet_length 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 a substring matching a POSIX regular expression substring(‘Thomas’ from ‘…s’) but
substring(string from pattern for escape) text Extract strings matching SQL regular expressions substring(‘Thomas’ from ‘%#“o_a#”_’ for ‘#’) own
trim([leading|trailing|both][characters] from string) text Delete the longest string containing only the characters in characters (default is a blank) from the beginning/end/both sides of the string string trim(both ‘x’ from ‘xTomxx’) Tom
upper(string) text convert string to uppercase upper(‘tom’) TOM

2. Other string functions

function return type describe example result
ascii(string) int The ASCII code of the first character of the parameter ascii(‘a’) 97
btrim(string text [,characters text]) text Remove the longest string containing only characters (a space by default) from the beginning or end of string btrim('aaosdbaa','aa') btrim('osdba','') btrim('osdba') osdb osdba osdba
chr(int) text Gives the ASCII code for the character chr(97) a
convert(string text,[src_encoding name,]dest_encoding name) text Convert the string originally encoded as src_encoding to dest_encoding (omit src_encoding to use database encoding) convert(‘aa’,‘UTF8’,‘GBK’) \x6161
decode(string text,type text) bytea Decode the binary data in the string originally encoded with encode, and the parameter type is the same as encode decode(‘b3NkYmEAAQ==’,‘base64’) \x6f736462610001
encode(data bytea,type text) text Encode binary data into ASCII-only data, supported types include base64, hex, escape, etc. encode(E’osdba\000\001’,‘base64’) b3NkYmEAAQ==
initcap(string) text The first letter of each word is converted to uppercase, and the other letters are converted to lowercase initcap(‘hi oSdBa’) Hi Osdba
length(string) int number of characters in string length(‘osdba’) 5
lpad(string text,length int [,fill text]) text By filling the character fill (default is blank), the string is filled to length length, if the string is already longer than length, its tail is truncated lpad(‘ok’,5,‘12’) 121ok
ltrim(string text [,characters text]) text Delete the characters contained in the parameter characters from the beginning of the string until a character that is not in the characters is encountered, the parameter characters default blank space ltrim('213osdba213', '123') osdba213
md5(string) text Computes the MD5 hash of string and returns the result in hexadecimal md5(‘osdba’) bc4e68be5b31f23d8d56c7f4c3351fec
pg_client_encoding() name Current client code name pg_client_encoding() UTF8
quote_ident(string) text Returns the given string suitable for quoting, as an identifier in an SQL statement string. Quotes are added only if necessary (for example, if the string contains non-identifier characters or will be case-folded). quote_ident(‘osdba a’) “osdba a”
quote_literal(string) text Returns a form suitable for use as text in an SQL statement, with embedded quotes and backslashes properly doubled quote_literal(E’O’Reilly’) ‘O’‘Reilly’
regexp_replace(string text,pattern text,replacement text[,flags text]) text Replace substring matching POSIX regular expression regexp_replace(‘os123dba’,‘.[1-9]+’,‘#’) o#dba
repeat(string text,unmber int) text 将string重复number次 repeat(‘osdba’,3) osdbaosdbaosdba
replace(string text,from text,to text) text 把字符串string中出现的所有子字符串from替换成子字符串to replace(‘123osdba45osdba78’,‘osdba’,‘-’) 123-45-78
rpad(string text,length int[,fill text]) text 使用填充字符fill(默认空白),把string填充到length长度,如果string已经比length长,则从尾部将其截断 rpad(‘os’,6,‘123’) os1231
rtrim(string text[,characters text]) text 从string末尾删除包含characters(默认空格)中仅包含字符的最长字符串 rtrim(‘trimxxxx’,‘x’) trim
split_part(string text,delimiter text,field int) text 根据delimiter分隔string返回生成的第field个子字符串(1为基) split_part(‘123#456#789’,‘#’,2) 456
strpos(string,substring) int 指定子字符串的位置,和position(substring in string)一样,不过参数顺序相反 strpos(‘osdba’,‘db’) 3
substr(string,from [,count]) text 抽取子字符串,和substring(string from for count)一样 substr(‘osdba’,2,2) sd
to_ascii(string text[,encoding text]) text 把string从其他编码转为ASCII(仅支持LATIN1,LATIN2,LATIN9,WIN1250编码) to_ascii(‘Osdba’) Osdba
to_hex(unmber int或bigint) text 把number转换成十六进制表现形式 to_hex(2147483647) 7fffffff
translate(string text,from text,to text) text 把string中包含的所有匹配from的字符转化为对应的在to中的字符 translate(‘12345’,‘14’,‘db’) d23b5

Guess you like

Origin blog.csdn.net/songyundong1993/article/details/131416803