PostgreSql 字符串函数及操作符

一、标准SQL字符串函数和操作符

函数 返回类型 描述 示例 结果
string||string text 字符串连接 ‘Post’||‘greSQL’ PostgreSQL
bit_length(string) int 字符串中的二进制位数的个数 bit_length(‘jose’) 32
char_length(string) int 字符串中的字符个数 char_length(‘数据库’) 3
convert(string using conversion_name) text 使用转换名字改变编码,转换通过create conversion来定义 convert(‘PostgreSQL’ using ios_8859_1_to_utf8) UTF8编码的’PostgreSQL’
lower(string) text 字符串转为小写 low(‘TOM’) tom
octet_length 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正则表达式的子字符串 substring(‘Thomas’ from ‘…s’) mas
substring(string from pattern for escape) text 抽取匹配SQL正则表达式的字符串 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

二、其他字符串函数

函数 返回类型 描述 示例 结果
ascii(string) int 参数第一个字符的ASCII码 ascii(‘a’) 97
btrim(string text [,characters text]) text 从string的开头或结尾删除最长的只包含characters(默认是一个空格)的字符串 btrim(‘aaosdbaaa’,‘aa’) btrim(’ osdba ‘,’‘) btrim(’ osdba ') osdb osdba osdba
chr(int) text 给出ASCII码的字符 chr(97) a
convert(string text,[src_encoding name,]dest_encoding name) text 把原来编码为src_encoding的字符串转为dest_encoding编码(省略src_encoding将使用数据库编码) convert(‘aa’,‘UTF8’,‘GBK’) \x6161
decode(string text,type text) bytea 把原来用encode编码的string中的二进制数据解码,参数类型和encode相同 decode(‘b3NkYmEAAQ==’,‘base64’) \x6f736462610001
encode(data bytea,type text) text 把二进制数据编码为只包含ASCII形式的数据,支持的类型有base64、hex、escape等 encode(E’osdba\000\001’,‘base64’) b3NkYmEAAQ==
initcap(string) text 每个单词第一个字母转为大写,其他字母转为小写 initcap(‘hi oSdBa’) Hi Osdba
length(string) int string中字符的数目 length(‘osdba’) 5
lpad(string text,length int [,fill text]) text 通过填充字符fill(默认为空白),把string填充为length长度,如果string已经比length长,则将其尾部截断 lpad(‘ok’,5,‘12’) 121ok
ltrim(string text [,characters text]) text 从string开头删除包含在参数characters中的字符,直到遇到一个不在characters中的字符为止,参数characters默认空格 ltrim(‘213osdba213’,‘123’) osdba213
md5(string) text 计算string的MD5散列,以十六进制返回结果 md5(‘osdba’) bc4e68be5b31f23d8d56c7f4c3351fec
pg_client_encoding() name 当前客户编码名称 pg_client_encoding() UTF8
quote_ident(string) text 返回适合引用的给定字符串,作为SQL语句字符串中的标识符。只有在必要的情况下才添加引号(例如,如果字符串包含非标识符字符或将被大小写折叠)。 quote_ident(‘osdba a’) “osdba a”
quote_literal(string) text 返回适用于在SQL语句中当做文本使用的形式,嵌入的引号和反斜杠被恰当地写为双份 quote_literal(E’O’Reilly’) ‘O’‘Reilly’
regexp_replace(string text,pattern text,replacement text[,flags text]) text 替换匹配POSIX正则表达式的子字符串 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

猜你喜欢

转载自blog.csdn.net/songyundong1993/article/details/131416803
今日推荐