8c sql手册 一

        1. format

函数format用于将字段的显示进行格式化。类似于C函数sprintf。语法:

format(formatstr text [, formatarg "any" [, ...] ])

formatstr指定了结果将被如何格式化。格式字符串中的文本,除格式说明符外,都将被直接复制到结果中。格式说明符定义了后续的函数参数将如何被格式化及插入到结果中。格式说明符由一个%字符开始,其语法表示为:

%[position][flags][width]type

其中:

  1. position(可选):形式为n&,其中n为要打印的参数的索引。如果忽略position会默认使用序列中的下一个参数;
  2. flags(可选):为符号-时,将格式说明符的输出左对齐。
  3. width(可选):用于显示格式说明符输出的最小字符数。表示形式:1)一个正整数;2)*表示使用下一个函数参数作为宽度;3)*n$的字符串表示使用第n个函数参数作为宽度。
  4. type(必需):格式转换的类型,用于产生格式说明符的输出,支持的类型为:1)s将参数格式化为一个简单的字符串;2)I将参数视为SQL标识符,并在必要时用双引号包围;3)L将参数引用为SQL文字。

下面为一些基本的格式转换的例子:

SELECT format('Hello %s', 'World');

结果:Hello World

SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three');

结果:Testing one, two, three, %

SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly');

结果:INSERT INTO "Foo bar" VALUES('O''Reilly')

SELECT format('INSERT INTO %I VALUES(%L)', 'locations', E'C:\\Program Files');

结果:INSERT INTO locations VALUES(E'C:\\Program Files')

使用width和-标志的例子:

SELECT format('|%10s|', 'foo');

结果:|       foo|

SELECT format('|%-10s|', 'foo');

结果:|foo       |

SELECT format('|%*s|', 10, 'foo');

结果:|       foo|

SELECT format('|%*s|', -10, 'foo');

结果:|foo       |

SELECT format('|%-*s|', 10, 'foo');

结果:|foo       |

SELECT format('|%-*s|', -10, 'foo');

结果:|foo       |

使用position的例子:

SELECT format('Testing %3$s, %2$s, %1$s', 'one', 'two', 'three');

结果:Testing three, two, one

SELECT format('|%*2$s|', 'foo', 10, 'bar');

结果:|       bar|

SELECT format('|%1$*2$s|', 'foo', 10, 'bar');

结果:|       foo|

      1. 二进制串函数和操作符

本节描述了检查和操作类型为bytea的函数和操作符,下表为SQL二进制串函数和操作符:

函数

返回类型

描述

例子

结果

string || string

bytea

串连接

E'\\\\Post'::bytea || E'\\047gres\\000'::bytea

\\Post'gres\000

octet_length(string)

int

二进制串中的字节数

octet_length(E'jo\\000se'::bytea)

5

overlay(string placing string from int [for int])

bytea

替换子串

overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 2 for 3)

T\\002\\003mas

position(substring in string)

int

指定子串的位置

position(E'\\000om'::bytea in E'Th\\000omas'::bytea)

3

substring(string [from int] [for int])

bytea

提取子串

substring(E'Th\\000omas'::bytea from 2 for 3)

h\000o

trim([both] bytes from string)

bytea

string的开头或结尾删除只包含出现在bytes中的字节的最长串

trim(E'\\000\\001'::bytea from E'\\000Tom\\001'::bytea)

Tom

以及一些其他二进制串函数:

函数

返回类型

描述

例子

结果

btrim(string bytea, bytes bytea)

bytea

string的开头或结尾删除只包含出现在bytes中的字节的最长串

btrim(E'\\000trim\\001'::bytea, E'\\000\\001'::bytea)

trim

decode(string text, format text)

bytea

string中的文本表示解码二进制数据。format的参数和在encode中一样。

decode(E'123\\000456', 'escape')

123\000456

encode(data bytea, format text)

text

将二进制数据编码为一个文本表示。支持的格式有:base64、hex、escape。escape将零字节和高位组字节转换为八进制序列(\nnn)和双反斜线。

encode(E'123\\000456'::bytea, 'escape')

123\000456

get_bit(string, offset)

int

从串中抽取位

get_bit(E'Th\\000omas'::bytea, 45)

1

get_byte(string, offset)

int

从串中抽取字节

get_byte(E'Th\\000omas'::bytea, 4)

109

length(string)

int

二进制串的长度

length(E'jo\\000se'::bytea)

5

md5(string)

text

计算string的MD5哈希码,以十六进制形式返回结果

md5(E'Th\\000omas'::bytea)

8ab2d3c9689aaf18 b4958c334c82d8b1

set_bit(string, offset, newvalue)

bytea

设置串中的位

set_bit(E'Th\\000omas'::bytea, 45, 0)

Th\000omAs

set_byte(string, offset, newvalue)

bytea

设置串中的字节

set_byte(E'Th\\000omas'::bytea, 4, 64)

Th\000o@as

get_byte和set_byte把一个二进制串中的一个字节计数为字节 0。get_bit和set_bit在每一个字节中从右边起计数位;例如位 0 是第一个字节的最低有效位,而位 15 是第二个字节的最高有效位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值