PostgreSQL 9.3 格式化拼接字符串

PostgreSQL 9.3 格式化拼接字符串,第1张

概述   2013-05-06 08:39:20|  分类: PgSQL Develop|举报|字号 订阅 PostgreSQL 9.3 引入的一个格式化输出函数, 有点类似C的sprintf用法. 语法如下 :  format(formatstr text [, formatarg "any" [, ...] ]) 其中formatstr是需要格式化的字符串, 包含普通字符以及格式字符. 后面的动态

2013-05-06 08:39:20|分类:PgSQL Develop|举报|字号订阅

Postgresql 9.3 引入的一个格式化输出函数,有点类似C的sprintf用法.

语法如下 :

format(formatstrtext[,formatarg"any"[,...]])

其中formatstr是需要格式化的字符串,包含普通字符以及格式字符.

后面的动态参数用来替换formatstr中的格式字符.

格式字符的语法如下 :

%[position][flags][wIDth]type

其中position,flags,wIDth都是可选项. type是必须的.

1. position 指变量位置.

Astringoftheformn$wherenistheindexoftheargumenttoprint.Index1meansthefirstargumentafterformatstr.Ifthepositionisomitted,thedefaultistousethenextargumentinsequence.

注意the next argument是指前一个已经取过的位置的下一个位置.

2. flags目前只有-,且需要与wIDth配合使用,-表示右补齐,没有-表示左补齐.

AdditionaloptionscontrollinghowtheformatspecifIEr'soutputisformatted.Currentlytheonlysupportedflagisaminussign(-)whichwillcausetheformatspecifIEr'soutputtobeleft-justifIEd.ThishasnoeffectunlessthewIDthfIEldisalsospecifIEd.

wIDth指显示的宽度,当宽度大于实际字符串时用空格补齐,当宽度小于实际字符串长度时不会截断字符串,相当于wIDth不起作用. wIDth也可以使用3. format函数的参数值来代替直接写在formatstr中.

SpecifIEs the minimum number of characters to use to display the format specifIEr's output. The output is padded on the left or right (depending on the - flag) with spaces as needed to fill the wIDth. A too-small wIDth does not cause truncation of the output,but is simply ignored. The wIDth may be specifIEd using any of the following:

a positive integer;

an asterisk (*) to use the next function argument as the wIDth;

or a string of the form *n$ to use the nth function argument as the wIDth.


If the wIDth comes from a function argument,that argument is consumed before the argument that is used for the format specifIEr's value. If the wIDth argument is negative, the result left aligned (as if the - flag had been specifIEd) within a fIEld of length abswIDth).

注意如果同一个格式中宽度用到了参数,那么宽度优先消耗这个参数. 因此如果没有指定位置时,默认会消耗下一个参数. 也就是宽度消耗的参数的下一个参数.

4. type指字符串类型,目前可以使用s,I,L. 分别表示字符串,IDentifIEd,和literal.

The type of format conversion to to produce the format specifIEr's output. The following types are supported:


s formats the argument value as a simple string. A null value is treated as an empty string.

I treats the argument value as an sql IDentifIEr,double-quoting it if necessary. It is an error for the value to be null.

L quotes the argument value as an sql literal. A null value is displayed as the string NulL,without quotes.


In addition to the format specifIErs described above,the special sequence %% may be used to output a literal % character.

[使用举例]

1.

digoal=#selectformat('|abc|%*2$s|',E'wabc\t','10','abc');format------------------|abc|abc|(1row)

*2$指wIDth取第二个参数. 10

s指字符串,这里去下一个参数也就是第三个参数'ab c'

2.

digoal=#selectformat('|abc|%*s|','8',E'wabc','abc');format----------------|abc|wabc|(1row)

*指wIDth,取下一个参数. 因为前面没有取过任何参数,所以这里是'8'

s指字符串,这里去下一个参数也就是第2个参数E'wab c'.

3.

digoal=#selectformat('|abc|%-*s|',所以这里是'8'

s指字符串,这里去下一个参数也就是第2个参数E'wab c'.

- 表示右边补齐

4.

postgres=#selectformat('|abc|%3$-*s|','4','abc');format------------|abc|10|(1row)

这里的*取的是第一个参数'4',因为前面没有取过参数,从1开始.

digoal=#selectformat('|abc|%2$s|%3$-*s|','abc');format------------------------|abc|wabc|10|(1row)

这里的*取的是第3个参数. 也就是%2$后面的一个参数.

%3$-*s这个格式中,是先取wIDth参数,再取其他参数的.

5.

digoal=#selectformat('|abc|%2$I|%3$-*I|','abc');format--------------------------|abc|"wabc"|"10"|(1row)

I指IDentifIEd,类似表名,字段名. 所以如果是包含了特殊字符则需要用双引号.

digoal=#selectformat('|abc|%2$I|%3$-*I|',E'wABc','abc');format-------------------------|abc|"wABc"|"10"|(1row)digoal=#selectformat('|abc|%2$I|%3$-*I|','abc');format-----------------------|abc|wabc|"10"|(1row)

6.

digoal=#selectformat('|abc|%2$L|%3$-*L|',E'wa\bc\t','abc');format-------------------------------|abc|'wa\x08c'|'10'|(1row)digoal=#selectformat('|abc|%2$L|%3$-*L|',E'wa\bc\t\','abc');format---------------------------------|abc|E'wa\x08c\'|'10'|(1row)

L指literal,类似字符串类型的值,所以涉及逃逸. 如山.

7.

因此format可用于构造动态sql.

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

Result: INSERT INTO "Foo bar" VALUES'O''Reilly')


'locations''C:\Program files' INSERT INTO locations VALUESE)

【参考】

1.http://www.postgresql.org/docs/devel/static/functions-string.html

2.http://www.postgresql.org/docs/devel/static/plpgsql-statements.html#PLPGSQL-QUOTE-LITERAL-EXAMPLE

3. man 3 sprintf

总结

以上是内存溢出为你收集整理的PostgreSQL 9.3 格式化拼接字符串全部内容,希望文章能够帮你解决PostgreSQL 9.3 格式化拼接字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存