函数 返回值类型 描述 示例 结果
string||string text 字符串连接 select 'Post'||'gresql'||' good!'; Postgresql good!
string||non-string或non-string||string text 非字符串与字符串连接 select 1||' one'; 1 one
bit_length(string) int 字符串的位数,注意一个汉字是3个字节 select bit_length('one'); 24
char_length(string) int 字符串中字符的个数 select char_length('中国'); 2
length(string) int 字符串中字符个数,同char_length(string) select length('中国'); 2
length(string,encoding name) int 以name的编码格式字符串的字符个数 select length('中国','GBK'); 3
octet_length(string) int 字符串的字节数 select octet_length('中国'); 6
lower(string) text 将字符串转换成小写 select lower('HeLLO'); hello
upper(string) text 将字符串转换成大写 select lower('HellO'); HELLO
initcap(string) text 将字符串中每个单词的首字母大写 select initcap('hello world !'); Hello World !
overlay(string placing string from int [for int]) text 替换字符串,其中第一个int是开始位置,第二个int是长度,如果没有第二个int则长度默认为第二个字符串的长度 select overlay('Txxxxas' placing 'hom' from 2 for 4); Thomas
replace(string,string,string) text 替换字符串,将第一个字符串中的第二个字符串替换成第三个字符串 select replace('Txxxxas','xxxx','hom'); Thomas
translate(string textfrom textto text) 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) int 给定子字符串在字符串的位置 select position('lo' in 'hello'); 4
strpos(stringsubstring) int 功能同上,只是入参顺序相反

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

4

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

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

select substring('hello world' from 2);

ell

ell world

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

trim([leading | trailing | both]

[characters] from string)

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

trim([leading | trailing | both]

[from] string[, characters] )

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

select ascii('xa');

select ascii('x');

120

120

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

parse_ident(qualified_identifiertext [,

strictmode booleanDEFAULT true ] )

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

setof

text

功能同上,只是以单列形式返回 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}

regexp_replace(string text,pattern text,

replacement text[, flags text])

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

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)

postgresql----字符串函数与操作符的更多相关文章

  1. PostgreSql字符串函数和操作符

    本节描述了用于检查和操作字符串数值的函数和操作符.在这个环境中的字符串包括所有 character, character varying, text 类型的值.除非另外说明,所有下面列出的函数都可以处 ...

  2. [转] PostgreSQL学习手册(函数和操作符)

    一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是PostgreSQL中提供的比较操作符列表: 操作符 描述 < ...

  3. PostgreSQL学习手册(五) 函数和操作符

    PostgreSQL学习手册(五) 函数和操作符 一.逻辑操作符:    常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符:    下面是Post ...

  4. PostgreSQL 存储过程/函数

    1.有用的链接 postgresql 常用小函数 Postgresql数据库的一些字符串操作函数 PostgreSQL function里面调用function PostgreSQL学习手册(函数和操 ...

  5. MySQL最常用字符串函数

    字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...

  6. MySQL常用字符串函数

    字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...

  7. ORACLE常用数值函数、转换函数、字符串函数介绍

    ORACLE常用数值函数.转换函数.字符串函数介绍. 数值函数: abs(m) m的绝对值 mod(m,n) m被n除后的余数 power(m,n) m的n次方 round(m[,n]) m四舍五入至 ...

  8. PHP字符串——字符串函数

    比较字符串PHP有两个操作符和6个函数用于字符串间相互比较. 精确比较你可以用==和===操作符来比较两个字符串是否相等.这两个操作符的不同在于它们如何处理非字符串数据类型的操作数.==操作符把非字符 ...

  9. [转]MySQL常用字符串函数

    本文转载自:http://www.cnblogs.com/geaozhang/ 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str ...

  10. sql常用格式化函数及字符串函数

    一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') YYYY:年份 MM:月份号(01-12) ...

随机推荐

  1. C/C++预处理指令

    预处理指令 Preprocessor Directives define undef ifdef ifndef if endif else and elif line error include 预定 ...

  2. awk基础命令

    1. 命令awk简介 a. awk是一种编程语言,用于对文本和数据进行处理. b.具有强大得文本格式话能力 c.利用命令awk,可以将一些文本整理成需要的格式. d.命令awk是逐行进行处理的 2. ...

  3. 选择列表中的列……无效,因为该列没有包含在聚合函数或 GROUP BY 子句中

    今天用SQL Server尝试实现一个SQL语句的时候,报了如标题所示的错误,通过在百度里面搜索,并亲自动手实现,终于发现问题所在,现在把它记录下来. 语句如下: select [OrderID],[ ...

  4. 【转】 如何利用C#代码来进行操作AD

    要用代码访问 Active Directory域服务,需引用System.DirectoryServices命名空间,该命名空间包含两个组件类,DirectoryEntry和 DirectorySea ...

  5. Runtime是什么?

    在看 RPC 的概念模型与实现解析 的时候,看到图片上有Runtime,又想到见过很多Runtime之类的东西,所以就想弄明白这到底是个什么东西. (因为是程序名,所以根本没想到代码的“编译-运行”~ ...

  6. iOS推送证书从申请到使用

    关于这个话题,已经有非常多写的非常好的文章了.可是,在自己做的过程中,即使别人写的已经非常好了,还是会遇到这样那样的问题. 自己还是再写一遍吧. 本文记录了从无到有申请证书,到最后可以发出通知.当然, ...

  7. php对gzip的使用(理论)

    gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992年10月31日第一 ...

  8. 总结一下前端面试题之Html和CSS

    总结一下关于前端的面试题,今天我们分享关于Html和CSS部分的 面试 (1) 1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? 2. 说下行内元素和块级元素的区别?行内块元素的 ...

  9. Strut2------源码下载

    转载: http://download.csdn.net/detail/dingkui/6858009

  10. 2014年王道论坛研究生机试练习赛(二)set 2 货币问题

    题目描述: 已知有面值为1元,2元,5元,10元,20元,50元,100元的货币若干(可认为无穷多),需支付价格为x的物品,并需要恰好支付,即没有找零产生.求,至少需要几张货币才能完成支付.如,若支付 ...