暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

PostgreSQL的format函数真有趣

原创 库海无涯 2024-05-28
344

##

PostgreSQL的format函数真有趣

1、概述

继放假5天之后,又开始上班了,客户让我给他拼接一个SQL语句,去查了下format的资料和案例,发现这个函数是真有趣。

2、format

The function format produces output formatted according to a format string, in a style similar to the C function sprintf.

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

formatstr is a format string that specifies how the result should be formatted. Text in the format string is copied directly to the result, except where format specifiers are used. Format specifiers act as placeholders in the string, defining how subsequent function arguments should be formatted and inserted into the result. Each formatarg argument is converted to text according to the usual output rules for its data type, and then formatted and inserted into the result string according to the format specifier(s).

Format specifiers are introduced by a % character and have the form

%[position][flags][width]type

where the component fields are:

  • position (optional)

    A string of the form *n*$ where n is the index of the argument to print. Index 1 means the first argument after formatstr. If the position is omitted, the default is to use the next argument in sequence.

  • flags (optional)

    Additional options controlling how the format specifier’s output is formatted. Currently the only supported flag is a minus sign (-) which will cause the format specifier’s output to be left-justified. This has no effect unless the width field is also specified.

  • width (optional)

    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 is left aligned (as if the - flag had been specified) within a field of length abs(width).

  • type (required)

    The type of format conversion to use 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 (equivalent to quote_ident).L quotes the argument value as an SQL literal. A null value is displayed as the string NULL, without quotes (equivalent to quote_nullable).

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

3、案例

微信截图_20240528225142.png

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论