相关文章推荐
overlay( string placing string from int [ for int ]) 替换字符串,其中第一个int是开始位置,第二个int是长度,如果没有第二个int则长度默认为第二个字符串的长度 select overlay('Txxxxas' placing 'hom' from 2 for 4); Thomas replace(string,string,string) 替换字符串,将第一个字符串中的第二个字符串替换成第三个字符串 select replace('Txxxxas','xxxx','hom'); Thomas translate( string text , from text , to text ) 把string中from字符替换成对应to中的字符,如('text','tx','nd')中t->n,x->d,替换后结果为nedn;如果from比to长,则删除from多出的字符,如('Txxxxas','Txas','hom')结果为hooom;如果from中有重复字符,对应顺序也是一一对应的,但是重复字符以第一个为准,如('txxxa','xxx','abcd'),结果为taaad

select translate('Txxxxas','xxxxa','hom');

select translate('Txxxxas','Txas','hom');

select translate('txxxa','xxxa','abcd');

Thhhhs

hoooom

taaad

position( substring in string ) 给定子字符串在字符串的位置 select position('lo' in 'hello'); strpos( string , substring ) 功能同上,只是入参顺序相反

select strpos('hello','lo');

substring( string from int [ for int ]) 截取字符串,从from位置截取长度for,如果for省略,则是从from至结尾

select substring('hello world' from 2 for 3);

select substring('hello world' from 2);

ell world

substring(string,from int[, for int]) select substring('hello world',2,3); substring( string from pattern ) 截取匹配posix正则表达式的字符串 select substring('hello world' from '...$'); substring( string from pattern for escape ) 截取匹配posix正则表达式的字符串,for为转移字符 select substring('Thomas' from '%#"o_a#"_' for '#');

trim([ leading | trailing | both]

[ characters ] from string )

删除字符串头部(leading),尾部(trailing)或两边的空白活[characters]字符 select trim(both 'x' from 'xxjdhdxxxx');

trim([ leading | trailing | both]

[ from] string [ , characters ] )

select trim(both from '  jdhd   ',' '); btrim( string text [ , characters text ]) 删除字符串两边指定的字符 select btrim('xxhhhxxx','x'); rtrim( string text [ , characters text ]) 删除字符串尾部指定的字符 select rtrim('xxhhhxxx','x'); xxhhh ltrim( string text [ , characters text ]) 删除字符串开头指定的字符 select ltrim('xxhhhxxx','x'); hhhxxx ascii( string ) 字符串第一个字符的ASCII值

select ascii('xa');

select ascii('x');

chr( int ) 将数字转换成字符 select chr(65); concat( str "any" [, str "any" [, ...] ]) 连接所有参数,个数不限,类型不限 select concat('x','man',3); xman3 concat_ws( sep text , str "any" [, str "any" [, ...] ]) 功能同上,只是第一个参数是连接分隔符 select concat_ws(',','x','man',3); x,man,3 convert( string bytea , src_encoding name , dest_encoding name ) 将字符串从指定编码转换至目的编码格式 select convert('Hello','UTF8','GBK'); \x48656c6c6f format ( formatstr text [, formatarg "any" [, ...] ]) 格式化字符串,类似C语言的sprintf,其中n$表示第n个参数 select format('Hello %s, %1$s', 'World'); Hello World, World left( str text , n int ) 返回字符串前n个字符,n为负数时返回除最后|n|个字符以外的所有字符 select left('hello',-2); right( str text , n int ) 返回字符串后n个字符,n为负数时返回除最前|n|个字符意外的所有字符 select right('hello',2); lpad( string text , length int [ , fill text ]) 在字符串开头填充text至长度为length,缺省为空白,如果string的长度已经大于length,则会截断后面多余length的字符 select lpad('123',5,'0'); 00123 rpad( string text , length int [ , fill text ]) 在字符串尾部填充text至长度为length,缺省为空白,如果string的长度已经大于length,则会截断后面多余length的字符 select rpad('he',1,'o'); md5( string ) 计算string的md5散列值,并以十六进制返回 select md5('hello'); 5d41402abc4b2a76b9719d911017c592

parse_ident( qualified_identifier text [,

strictmode boolean DEFAULT true ] )

text[] 将qualified_identifier拆分解析到一个数组中,以句点为分隔符。 select parse_ident('SomeSchema.someTable'); {someschema,sometable} pg_client_encoding() 获取当前客户端编码 select pg_client_encoding(); quote_ident( string text ) 返回适合SQL语句标志符且使用适当引号的字符串,在字符串两端加双引号,如果字符串中出现双引号,返回结果中将变成两个,如果有2个连续的单引号,返回时只有1个 select quote_ident('Foo"''"bar'); "Foo""'""bar" quote_literal( string text ) 功能同上,只是内嵌的单引号和双引号被原样保留 select quote_literal('Foo"''bar'); 'Foo"''bar' quote_literal( value anyelement ) 将给定值转成text select quote_literal(45); quote_nullable( string text ) 功能同quote_literal( string text ),只是参数是NULL时返回NULL split_part( string text , delimiter text , field int ) 将字符串string以delimiter进行分割,并返回第field个子串 select split_part('1#2#3','#',2); to_hex( number int or bigint ) 将数值转换成十六进制 select to_hex(155); reverse( str ) 将字符串逆序输出 select reverse('hello'); olleh regexp_split_to_array( string text , pattern text [, flags text ]) text[] 将字符串匹配posix正则表达式分割为字符串数组 select regexp_split_to_array('hello world', E'\\s+'); {hello,world} regexp_split_to_table( string text , pattern text [, flags text ])

setof

功能同上,只是以单列形式返回 select regexp_split_to_table('hello world', E'\\s+');

hello
world

regexp_matches( string text , pattern text [, flags text ]) setof text[] 返回string中第一个匹配posix正则表达式的子串,如果flag=g,则返回所有 select regexp_matches('foobarbequebaz', '(b..)','g');

{bar}
{beq}
{baz}

将匹配posix正则表达式的第一个子串替换成指定字符串,如果flag=g,则替换所有 select regexp_replace('Thomas', '.[mN]a.', 'M');

format格式化

格式说明符由 % 字符引进,格式为

%[ position ] type
组件的字段有:
position (optional)
n$ 格式的字符串,这里的n是要打印的参数的索引。索引为1表示在formatstr之后的第一个参数。如果省略了position,默认使用序列中的下一个参数。
type (required)
格式转换的类型用来产生格式说明符的输出。支持下列的类型:
s 格式参数值为简单的字符串。空值作为空字符串对待。
I 将参数值作为SQL标识符对待,如果需要,双写它。值为空是错误的。
L 引用参数值作为SQL文字。空值用字符串 NULL 显示,没有引用。
除了上述的格式说明符,特殊的序列 %% 可以用作输出 % 字符。

test=# SELECT format('Hello %s', 'World');
   format    
-------------
 Hello World
(1 row)
test=# SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three');
           format           
----------------------------
 Testing one, two, three, %
(1 row)
test=# SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly');
                  format                   
-------------------------------------------
 INSERT INTO "Foo bar" VALUES('O''Reilly')
(1 row)