postgresql----字符串函数与操作符
| 函数 | 返回值类型 | 描述 | 示例 | 结果 |
| 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 text, from text, to 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(string, substring) | 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 text, str "any" [,str "any" [, ...] ]) | text | 功能同上,只是第一个参数是连接分隔符 | select concat_ws(',','x','man',3); | x,man,3 |
| convert(string bytea,src_encoding name, dest_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 text, n int) | text | 返回字符串前n个字符,n为负数时返回除最后|n|个字符以外的所有字符 | select left('hello',-2); | hel |
| right(str text, n int) | text | 返回字符串后n个字符,n为负数时返回除最前|n|个字符意外的所有字符 | select right('hello',2); | he |
| lpad(string text, length int [,fill text]) | text | 在字符串开头填充text至长度为length,缺省为空白,如果string的长度已经大于length,则会截断后面多余length的字符 | select lpad('123',5,'0'); | 00123 |
| rpad(string text, length 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 text, number int) | text | 将string重复number次 | select repeat('Hi',2); | HiHi |
| split_part(string text,delimiter text, field 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(stringtext, pattern text [, flags text]) | text[] | 将字符串匹配posix正则表达式分割为字符串数组 | select regexp_split_to_array('hello world', E'\\s+'); | {hello,world} |
| regexp_split_to_table(stringtext, pattern text [, flagstext]) |
setof text |
功能同上,只是以单列形式返回 | select regexp_split_to_table('hello world', E'\\s+'); |
hello |
| regexp_matches(string text,pattern text [, flags text]) | setof text[] | 返回string中第一个匹配posix正则表达式的子串,如果flag=g,则返回所有 | select regexp_matches('foobarbequebaz', '(b..)','g'); |
{bar} |
|
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----字符串函数与操作符的更多相关文章
- PostgreSql字符串函数和操作符
本节描述了用于检查和操作字符串数值的函数和操作符.在这个环境中的字符串包括所有 character, character varying, text 类型的值.除非另外说明,所有下面列出的函数都可以处 ...
- [转] PostgreSQL学习手册(函数和操作符)
一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是PostgreSQL中提供的比较操作符列表: 操作符 描述 < ...
- PostgreSQL学习手册(五) 函数和操作符
PostgreSQL学习手册(五) 函数和操作符 一.逻辑操作符: 常用的逻辑操作符有:AND.OR和NOT.其语义与其它编程语言中的逻辑操作符完全相同. 二.比较操作符: 下面是Post ...
- PostgreSQL 存储过程/函数
1.有用的链接 postgresql 常用小函数 Postgresql数据库的一些字符串操作函数 PostgreSQL function里面调用function PostgreSQL学习手册(函数和操 ...
- MySQL最常用字符串函数
字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...
- MySQL常用字符串函数
字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...
- ORACLE常用数值函数、转换函数、字符串函数介绍
ORACLE常用数值函数.转换函数.字符串函数介绍. 数值函数: abs(m) m的绝对值 mod(m,n) m被n除后的余数 power(m,n) m的n次方 round(m[,n]) m四舍五入至 ...
- PHP字符串——字符串函数
比较字符串PHP有两个操作符和6个函数用于字符串间相互比较. 精确比较你可以用==和===操作符来比较两个字符串是否相等.这两个操作符的不同在于它们如何处理非字符串数据类型的操作数.==操作符把非字符 ...
- [转]MySQL常用字符串函数
本文转载自:http://www.cnblogs.com/geaozhang/ 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str ...
- sql常用格式化函数及字符串函数
一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') YYYY:年份 MM:月份号(01-12) ...
随机推荐
- Ubuntu创建新用户并增加管理员权限 删除某个用户
sudo adduser xxx 这样的命令会在home目录下添加一个帐号sudo useradd xxx 仅仅是添加用户, 不会在home目录添加帐号 删除:终端方法:以下用newuser代替想要删 ...
- repo 安装
repo是android用于管理git仓库的工具. 参考链接 http://www.cnblogs.com/xiaoerlang/p/3549156.html mkdir ~/bin export P ...
- 第三百一十二节,Django框架,Cookie
第三百一十二节,Django框架,Cookie 注意:获取Cookie是在请求对象里处理,设置Cookie是在响应对象里处理 普通Cookieset_cookie()设置普通cookie 参数: ke ...
- ffmpeg 源码分析
http://blog.csdn.net/liuhongxiangm/article/details/8824761 https://code.google.com/p/ffmpegsource/is ...
- PHP 5种方式获取文件后缀名
<?php header("content-type:text/html;charset=utf-8"); function get_ext1($filename) { re ...
- MyEclipse 对项目进行build path无效
今天发现昨天从svn下载下来的项目在MyEclipse中无法build path .提示no actions available 在网上找了下,是由于.projects文件的问题,须要在当中的natu ...
- 《开源框架那些事儿22》:UI框架设计实战
UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...
- Tomcat源码学习
Tomcat源码学习(一) 转自:http://carllgc.blog.ccidnet.com/blog-htm-do-showone-uid-4092-type-blog-itemid-26309 ...
- PHP大小写是否敏感问题
一.大小写敏感 1. 变量名区分大小写 所有变量均区分大小写,包括普通变量以及$_GET,$_POST,$_REQUEST,$_COOKIE,$_SESSION,$GLOBALS,$_SERVER,$ ...
- 扁平化你的Qt应用程序
什么是扁平化 这里的扁平化指的是交互设计方面的一种风格. 扁平化是随着极简注意的风潮流行起来的,这个概念最核心的地方就是放弃一切装饰效果,诸如阴影.透视,纹理,渐变等等能做出3D效果的元素一概不用.全 ...