]> begriffs open source - ai-pg/blob - full-docs/txt/functions-string.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / functions-string.txt
1
2 9.4. String Functions and Operators #
3
4    9.4.1. format
5
6    This section describes functions and operators for examining and
7    manipulating string values. Strings in this context include values of
8    the types character, character varying, and text. Except where noted,
9    these functions and operators are declared to accept and return type
10    text. They will interchangeably accept character varying arguments.
11    Values of type character will be converted to text before the function
12    or operator is applied, resulting in stripping any trailing spaces in
13    the character value.
14
15    SQL defines some string functions that use key words, rather than
16    commas, to separate arguments. Details are in Table 9.9. PostgreSQL
17    also provides versions of these functions that use the regular function
18    invocation syntax (see Table 9.10).
19
20 Note
21
22    The string concatenation operator (||) will accept non-string input, so
23    long as at least one input is of string type, as shown in Table 9.9.
24    For other cases, inserting an explicit coercion to text can be used to
25    have non-string input accepted.
26
27    Table 9.9. SQL String Functions and Operators
28
29    Function/Operator
30
31    Description
32
33    Example(s)
34
35    text || text → text
36
37    Concatenates the two strings.
38
39    'Post' || 'greSQL' → PostgreSQL
40
41    text || anynonarray → text
42
43    anynonarray || text → text
44
45    Converts the non-string input to text, then concatenates the two
46    strings. (The non-string input cannot be of an array type, because that
47    would create ambiguity with the array || operators. If you want to
48    concatenate an array's text equivalent, cast it to text explicitly.)
49
50    'Value: ' || 42 → Value: 42
51
52    btrim ( string text [, characters text ] ) → text
53
54    Removes the longest string containing only characters in characters (a
55    space by default) from the start and end of string.
56
57    btrim('xyxtrimyyx', 'xyz') → trim
58
59    text IS [NOT] [form] NORMALIZED → boolean
60
61    Checks whether the string is in the specified Unicode normalization
62    form. The optional form key word specifies the form: NFC (the default),
63    NFD, NFKC, or NFKD. This expression can only be used when the server
64    encoding is UTF8. Note that checking for normalization using this
65    expression is often faster than normalizing possibly already normalized
66    strings.
67
68    U&'\0061\0308bc' IS NFD NORMALIZED → t
69
70    bit_length ( text ) → integer
71
72    Returns number of bits in the string (8 times the octet_length).
73
74    bit_length('jose') → 32
75
76    char_length ( text ) → integer
77
78    character_length ( text ) → integer
79
80    Returns number of characters in the string.
81
82    char_length('josé') → 4
83
84    lower ( text ) → text
85
86    Converts the string to all lower case, according to the rules of the
87    database's locale.
88
89    lower('TOM') → tom
90
91    lpad ( string text, length integer [, fill text ] ) → text
92
93    Extends the string to length length by prepending the characters fill
94    (a space by default). If the string is already longer than length then
95    it is truncated (on the right).
96
97    lpad('hi', 5, 'xy') → xyxhi
98
99    ltrim ( string text [, characters text ] ) → text
100
101    Removes the longest string containing only characters in characters (a
102    space by default) from the start of string.
103
104    ltrim('zzzytest', 'xyz') → test
105
106    normalize ( text [, form ] ) → text
107
108    Converts the string to the specified Unicode normalization form. The
109    optional form key word specifies the form: NFC (the default), NFD,
110    NFKC, or NFKD. This function can only be used when the server encoding
111    is UTF8.
112
113    normalize(U&'\0061\0308bc', NFC) → U&'\00E4bc'
114
115    octet_length ( text ) → integer
116
117    Returns number of bytes in the string.
118
119    octet_length('josé') → 5 (if server encoding is UTF8)
120
121    octet_length ( character ) → integer
122
123    Returns number of bytes in the string. Since this version of the
124    function accepts type character directly, it will not strip trailing
125    spaces.
126
127    octet_length('abc '::character(4)) → 4
128
129    overlay ( string text PLACING newsubstring text FROM start integer [
130    FOR count integer ] ) → text
131
132    Replaces the substring of string that starts at the start'th character
133    and extends for count characters with newsubstring. If count is
134    omitted, it defaults to the length of newsubstring.
135
136    overlay('Txxxxas' placing 'hom' from 2 for 4) → Thomas
137
138    position ( substring text IN string text ) → integer
139
140    Returns first starting index of the specified substring within string,
141    or zero if it's not present.
142
143    position('om' in 'Thomas') → 3
144
145    rpad ( string text, length integer [, fill text ] ) → text
146
147    Extends the string to length length by appending the characters fill (a
148    space by default). If the string is already longer than length then it
149    is truncated.
150
151    rpad('hi', 5, 'xy') → hixyx
152
153    rtrim ( string text [, characters text ] ) → text
154
155    Removes the longest string containing only characters in characters (a
156    space by default) from the end of string.
157
158    rtrim('testxxzx', 'xyz') → test
159
160    substring ( string text [ FROM start integer ] [ FOR count integer ] )
161    → text
162
163    Extracts the substring of string starting at the start'th character if
164    that is specified, and stopping after count characters if that is
165    specified. Provide at least one of start and count.
166
167    substring('Thomas' from 2 for 3) → hom
168
169    substring('Thomas' from 3) → omas
170
171    substring('Thomas' for 2) → Th
172
173    substring ( string text FROM pattern text ) → text
174
175    Extracts the first substring matching POSIX regular expression; see
176    Section 9.7.3.
177
178    substring('Thomas' from '...$') → mas
179
180    substring ( string text SIMILAR pattern text ESCAPE escape text ) →
181    text
182
183    substring ( string text FROM pattern text FOR escape text ) → text
184
185    Extracts the first substring matching SQL regular expression; see
186    Section 9.7.2. The first form has been specified since SQL:2003; the
187    second form was only in SQL:1999 and should be considered obsolete.
188
189    substring('Thomas' similar '%#"o_a#"_' escape '#') → oma
190
191    trim ( [ LEADING | TRAILING | BOTH ] [ characters text ] FROM string
192    text ) → text
193
194    Removes the longest string containing only characters in characters (a
195    space by default) from the start, end, or both ends (BOTH is the
196    default) of string.
197
198    trim(both 'xyz' from 'yxTomxx') → Tom
199
200    trim ( [ LEADING | TRAILING | BOTH ] [ FROM ] string text [, characters
201    text ] ) → text
202
203    This is a non-standard syntax for trim().
204
205    trim(both from 'yxTomxx', 'xyz') → Tom
206
207    unicode_assigned ( text ) → boolean
208
209    Returns true if all characters in the string are assigned Unicode
210    codepoints; false otherwise. This function can only be used when the
211    server encoding is UTF8.
212
213    upper ( text ) → text
214
215    Converts the string to all upper case, according to the rules of the
216    database's locale.
217
218    upper('tom') → TOM
219
220    Additional string manipulation functions and operators are available
221    and are listed in Table 9.10. (Some of these are used internally to
222    implement the SQL-standard string functions listed in Table 9.9.) There
223    are also pattern-matching operators, which are described in
224    Section 9.7, and operators for full-text search, which are described in
225    Chapter 12.
226
227    Table 9.10. Other String Functions and Operators
228
229    Function/Operator
230
231    Description
232
233    Example(s)
234
235    text ^@ text → boolean
236
237    Returns true if the first string starts with the second string
238    (equivalent to the starts_with() function).
239
240    'alphabet' ^@ 'alph' → t
241
242    ascii ( text ) → integer
243
244    Returns the numeric code of the first character of the argument. In
245    UTF8 encoding, returns the Unicode code point of the character. In
246    other multibyte encodings, the argument must be an ASCII character.
247
248    ascii('x') → 120
249
250    chr ( integer ) → text
251
252    Returns the character with the given code. In UTF8 encoding the
253    argument is treated as a Unicode code point. In other multibyte
254    encodings the argument must designate an ASCII character. chr(0) is
255    disallowed because text data types cannot store that character.
256
257    chr(65) → A
258
259    concat ( val1 "any" [, val2 "any" [, ...] ] ) → text
260
261    Concatenates the text representations of all the arguments. NULL
262    arguments are ignored.
263
264    concat('abcde', 2, NULL, 22) → abcde222
265
266    concat_ws ( sep text, val1 "any" [, val2 "any" [, ...] ] ) → text
267
268    Concatenates all but the first argument, with separators. The first
269    argument is used as the separator string, and should not be NULL. Other
270    NULL arguments are ignored.
271
272    concat_ws(',', 'abcde', 2, NULL, 22) → abcde,2,22
273
274    format ( formatstr text [, formatarg "any" [, ...] ] ) → text
275
276    Formats arguments according to a format string; see Section 9.4.1. This
277    function is similar to the C function sprintf.
278
279    format('Hello %s, %1$s', 'World') → Hello World, World
280
281    initcap ( text ) → text
282
283    Converts the first letter of each word to upper case and the rest to
284    lower case. Words are sequences of alphanumeric characters separated by
285    non-alphanumeric characters.
286
287    initcap('hi THOMAS') → Hi Thomas
288
289    casefold ( text ) → text
290
291    Performs case folding of the input string according to the collation.
292    Case folding is similar to case conversion, but the purpose of case
293    folding is to facilitate case-insensitive matching of strings, whereas
294    the purpose of case conversion is to convert to a particular cased
295    form. This function can only be used when the server encoding is UTF8.
296
297    Ordinarily, case folding simply converts to lowercase, but there may be
298    exceptions depending on the collation. For instance, some characters
299    have more than two lowercase variants, or fold to uppercase.
300
301    Case folding may change the length of the string. For instance, in the
302    PG_UNICODE_FAST collation, ß (U+00DF) folds to ss.
303
304    casefold can be used for Unicode Default Caseless Matching. It does not
305    always preserve the normalized form of the input string (see
306    normalize).
307
308    The libc provider doesn't support case folding, so casefold is
309    identical to lower.
310
311    left ( string text, n integer ) → text
312
313    Returns first n characters in the string, or when n is negative,
314    returns all but last |n| characters.
315
316    left('abcde', 2) → ab
317
318    length ( text ) → integer
319
320    Returns the number of characters in the string.
321
322    length('jose') → 4
323
324    md5 ( text ) → text
325
326    Computes the MD5 hash of the argument, with the result written in
327    hexadecimal.
328
329    md5('abc') → 900150983cd24fb0​d6963f7d28e17f72
330
331    parse_ident ( qualified_identifier text [, strict_mode boolean DEFAULT
332    true ] ) → text[]
333
334    Splits qualified_identifier into an array of identifiers, removing any
335    quoting of individual identifiers. By default, extra characters after
336    the last identifier are considered an error; but if the second
337    parameter is false, then such extra characters are ignored. (This
338    behavior is useful for parsing names for objects like functions.) Note
339    that this function does not truncate over-length identifiers. If you
340    want truncation you can cast the result to name[].
341
342    parse_ident('"SomeSchema".someTable') → {SomeSchema,sometable}
343
344    pg_client_encoding ( ) → name
345
346    Returns current client encoding name.
347
348    pg_client_encoding() → UTF8
349
350    quote_ident ( text ) → text
351
352    Returns the given string suitably quoted to be used as an identifier in
353    an SQL statement string. Quotes are added only if necessary (i.e., if
354    the string contains non-identifier characters or would be case-folded).
355    Embedded quotes are properly doubled. See also Example 41.1.
356
357    quote_ident('Foo bar') → "Foo bar"
358
359    quote_literal ( text ) → text
360
361    Returns the given string suitably quoted to be used as a string literal
362    in an SQL statement string. Embedded single-quotes and backslashes are
363    properly doubled. Note that quote_literal returns null on null input;
364    if the argument might be null, quote_nullable is often more suitable.
365    See also Example 41.1.
366
367    quote_literal(E'O\'Reilly') → 'O''Reilly'
368
369    quote_literal ( anyelement ) → text
370
371    Converts the given value to text and then quotes it as a literal.
372    Embedded single-quotes and backslashes are properly doubled.
373
374    quote_literal(42.5) → '42.5'
375
376    quote_nullable ( text ) → text
377
378    Returns the given string suitably quoted to be used as a string literal
379    in an SQL statement string; or, if the argument is null, returns NULL.
380    Embedded single-quotes and backslashes are properly doubled. See also
381    Example 41.1.
382
383    quote_nullable(NULL) → NULL
384
385    quote_nullable ( anyelement ) → text
386
387    Converts the given value to text and then quotes it as a literal; or,
388    if the argument is null, returns NULL. Embedded single-quotes and
389    backslashes are properly doubled.
390
391    quote_nullable(42.5) → '42.5'
392
393    regexp_count ( string text, pattern text [, start integer [, flags text
394    ] ] ) → integer
395
396    Returns the number of times the POSIX regular expression pattern
397    matches in the string; see Section 9.7.3.
398
399    regexp_count('123456789012', '\d\d\d', 2) → 3
400
401    regexp_instr ( string text, pattern text [, start integer [, N integer
402    [, endoption integer [, flags text [, subexpr integer ] ] ] ] ] ) →
403    integer
404
405    Returns the position within string where the N'th match of the POSIX
406    regular expression pattern occurs, or zero if there is no such match;
407    see Section 9.7.3.
408
409    regexp_instr('ABCDEF', 'c(.)(..)', 1, 1, 0, 'i') → 3
410
411    regexp_instr('ABCDEF', 'c(.)(..)', 1, 1, 0, 'i', 2) → 5
412
413    regexp_like ( string text, pattern text [, flags text ] ) → boolean
414
415    Checks whether a match of the POSIX regular expression pattern occurs
416    within string; see Section 9.7.3.
417
418    regexp_like('Hello World', 'world$', 'i') → t
419
420    regexp_match ( string text, pattern text [, flags text ] ) → text[]
421
422    Returns substrings within the first match of the POSIX regular
423    expression pattern to the string; see Section 9.7.3.
424
425    regexp_match('foobarbequebaz', '(bar)(beque)') → {bar,beque}
426
427    regexp_matches ( string text, pattern text [, flags text ] ) → setof
428    text[]
429
430    Returns substrings within the first match of the POSIX regular
431    expression pattern to the string, or substrings within all such matches
432    if the g flag is used; see Section 9.7.3.
433
434    regexp_matches('foobarbequebaz', 'ba.', 'g') →
435  {bar}
436  {baz}
437
438    regexp_replace ( string text, pattern text, replacement text [, flags
439    text ] ) → text
440
441    Replaces the substring that is the first match to the POSIX regular
442    expression pattern, or all such matches if the g flag is used; see
443    Section 9.7.3.
444
445    regexp_replace('Thomas', '.[mN]a.', 'M') → ThM
446
447    regexp_replace ( string text, pattern text, replacement text, start
448    integer [, N integer [, flags text ] ] ) → text
449
450    Replaces the substring that is the N'th match to the POSIX regular
451    expression pattern, or all such matches if N is zero, with the search
452    beginning at the start'th character of string. If N is omitted, it
453    defaults to 1. See Section 9.7.3.
454
455    regexp_replace('Thomas', '.', 'X', 3, 2) → ThoXas
456
457    regexp_replace(string=>'hello world', pattern=>'l', replacement=>'XX',
458    start=>1, "N"=>2) → helXXo world
459
460    regexp_split_to_array ( string text, pattern text [, flags text ] ) →
461    text[]
462
463    Splits string using a POSIX regular expression as the delimiter,
464    producing an array of results; see Section 9.7.3.
465
466    regexp_split_to_array('hello world', '\s+') → {hello,world}
467
468    regexp_split_to_table ( string text, pattern text [, flags text ] ) →
469    setof text
470
471    Splits string using a POSIX regular expression as the delimiter,
472    producing a set of results; see Section 9.7.3.
473
474    regexp_split_to_table('hello world', '\s+') →
475  hello
476  world
477
478    regexp_substr ( string text, pattern text [, start integer [, N integer
479    [, flags text [, subexpr integer ] ] ] ] ) → text
480
481    Returns the substring within string that matches the N'th occurrence of
482    the POSIX regular expression pattern, or NULL if there is no such
483    match; see Section 9.7.3.
484
485    regexp_substr('ABCDEF', 'c(.)(..)', 1, 1, 'i') → CDEF
486
487    regexp_substr('ABCDEF', 'c(.)(..)', 1, 1, 'i', 2) → EF
488
489    repeat ( string text, number integer ) → text
490
491    Repeats string the specified number of times.
492
493    repeat('Pg', 4) → PgPgPgPg
494
495    replace ( string text, from text, to text ) → text
496
497    Replaces all occurrences in string of substring from with substring to.
498
499    replace('abcdefabcdef', 'cd', 'XX') → abXXefabXXef
500
501    reverse ( text ) → text
502
503    Reverses the order of the characters in the string.
504
505    reverse('abcde') → edcba
506
507    right ( string text, n integer ) → text
508
509    Returns last n characters in the string, or when n is negative, returns
510    all but first |n| characters.
511
512    right('abcde', 2) → de
513
514    split_part ( string text, delimiter text, n integer ) → text
515
516    Splits string at occurrences of delimiter and returns the n'th field
517    (counting from one), or when n is negative, returns the
518    |n|'th-from-last field.
519
520    split_part('abc~@~def~@~ghi', '~@~', 2) → def
521
522    split_part('abc,def,ghi,jkl', ',', -2) → ghi
523
524    starts_with ( string text, prefix text ) → boolean
525
526    Returns true if string starts with prefix.
527
528    starts_with('alphabet', 'alph') → t
529
530    string_to_array ( string text, delimiter text [, null_string text ] ) →
531    text[]
532
533    Splits the string at occurrences of delimiter and forms the resulting
534    fields into a text array. If delimiter is NULL, each character in the
535    string will become a separate element in the array. If delimiter is an
536    empty string, then the string is treated as a single field. If
537    null_string is supplied and is not NULL, fields matching that string
538    are replaced by NULL. See also array_to_string.
539
540    string_to_array('xx~~yy~~zz', '~~', 'yy') → {xx,NULL,zz}
541
542    string_to_table ( string text, delimiter text [, null_string text ] ) →
543    setof text
544
545    Splits the string at occurrences of delimiter and returns the resulting
546    fields as a set of text rows. If delimiter is NULL, each character in
547    the string will become a separate row of the result. If delimiter is an
548    empty string, then the string is treated as a single field. If
549    null_string is supplied and is not NULL, fields matching that string
550    are replaced by NULL.
551
552    string_to_table('xx~^~yy~^~zz', '~^~', 'yy') →
553  xx
554  NULL
555  zz
556
557    strpos ( string text, substring text ) → integer
558
559    Returns first starting index of the specified substring within string,
560    or zero if it's not present. (Same as position(substring in string),
561    but note the reversed argument order.)
562
563    strpos('high', 'ig') → 2
564
565    substr ( string text, start integer [, count integer ] ) → text
566
567    Extracts the substring of string starting at the start'th character,
568    and extending for count characters if that is specified. (Same as
569    substring(string from start for count).)
570
571    substr('alphabet', 3) → phabet
572
573    substr('alphabet', 3, 2) → ph
574
575    to_ascii ( string text ) → text
576
577    to_ascii ( string text, encoding name ) → text
578
579    to_ascii ( string text, encoding integer ) → text
580
581    Converts string to ASCII from another encoding, which may be identified
582    by name or number. If encoding is omitted the database encoding is
583    assumed (which in practice is the only useful case). The conversion
584    consists primarily of dropping accents. Conversion is only supported
585    from LATIN1, LATIN2, LATIN9, and WIN1250 encodings. (See the unaccent
586    module for another, more flexible solution.)
587
588    to_ascii('Karél') → Karel
589
590    to_bin ( integer ) → text
591
592    to_bin ( bigint ) → text
593
594    Converts the number to its equivalent two's complement binary
595    representation.
596
597    to_bin(2147483647) → 1111111111111111111111111111111
598
599    to_bin(-1234) → 11111111111111111111101100101110
600
601    to_hex ( integer ) → text
602
603    to_hex ( bigint ) → text
604
605    Converts the number to its equivalent two's complement hexadecimal
606    representation.
607
608    to_hex(2147483647) → 7fffffff
609
610    to_hex(-1234) → fffffb2e
611
612    to_oct ( integer ) → text
613
614    to_oct ( bigint ) → text
615
616    Converts the number to its equivalent two's complement octal
617    representation.
618
619    to_oct(2147483647) → 17777777777
620
621    to_oct(-1234) → 37777775456
622
623    translate ( string text, from text, to text ) → text
624
625    Replaces each character in string that matches a character in the from
626    set with the corresponding character in the to set. If from is longer
627    than to, occurrences of the extra characters in from are deleted.
628
629    translate('12345', '143', 'ax') → a2x5
630
631    unistr ( text ) → text
632
633    Evaluate escaped Unicode characters in the argument. Unicode characters
634    can be specified as \XXXX (4 hexadecimal digits), \+XXXXXX (6
635    hexadecimal digits), \uXXXX (4 hexadecimal digits), or \UXXXXXXXX (8
636    hexadecimal digits). To specify a backslash, write two backslashes. All
637    other characters are taken literally.
638
639    If the server encoding is not UTF-8, the Unicode code point identified
640    by one of these escape sequences is converted to the actual server
641    encoding; an error is reported if that's not possible.
642
643    This function provides a (non-standard) alternative to string constants
644    with Unicode escapes (see Section 4.1.2.3).
645
646    unistr('d\0061t\+000061') → data
647
648    unistr('d\u0061t\U00000061') → data
649
650    The concat, concat_ws and format functions are variadic, so it is
651    possible to pass the values to be concatenated or formatted as an array
652    marked with the VARIADIC keyword (see Section 36.5.6). The array's
653    elements are treated as if they were separate ordinary arguments to the
654    function. If the variadic array argument is NULL, concat and concat_ws
655    return NULL, but format treats a NULL as a zero-element array.
656
657    See also the aggregate function string_agg in Section 9.21, and the
658    functions for converting between strings and the bytea type in
659    Table 9.13.
660
661 9.4.1. format #
662
663    The function format produces output formatted according to a format
664    string, in a style similar to the C function sprintf.
665
666 format(formatstr text [, formatarg "any" [, ...] ])
667
668    formatstr is a format string that specifies how the result should be
669    formatted. Text in the format string is copied directly to the result,
670    except where format specifiers are used. Format specifiers act as
671    placeholders in the string, defining how subsequent function arguments
672    should be formatted and inserted into the result. Each formatarg
673    argument is converted to text according to the usual output rules for
674    its data type, and then formatted and inserted into the result string
675    according to the format specifier(s).
676
677    Format specifiers are introduced by a % character and have the form
678 %[position][flags][width]type
679
680    where the component fields are:
681
682    position (optional)
683           A string of the form n$ where n is the index of the argument to
684           print. Index 1 means the first argument after formatstr. If the
685           position is omitted, the default is to use the next argument in
686           sequence.
687
688    flags (optional)
689           Additional options controlling how the format specifier's output
690           is formatted. Currently the only supported flag is a minus sign
691           (-) which will cause the format specifier's output to be
692           left-justified. This has no effect unless the width field is
693           also specified.
694
695    width (optional)
696           Specifies the minimum number of characters to use to display the
697           format specifier's output. The output is padded on the left or
698           right (depending on the - flag) with spaces as needed to fill
699           the width. A too-small width does not cause truncation of the
700           output, but is simply ignored. The width may be specified using
701           any of the following: a positive integer; an asterisk (*) to use
702           the next function argument as the width; or a string of the form
703           *n$ to use the nth function argument as the width.
704
705           If the width comes from a function argument, that argument is
706           consumed before the argument that is used for the format
707           specifier's value. If the width argument is negative, the result
708           is left aligned (as if the - flag had been specified) within a
709           field of length abs(width).
710
711    type (required)
712           The type of format conversion to use to produce the format
713           specifier's output. The following types are supported:
714
715           + s formats the argument value as a simple string. A null value
716             is treated as an empty string.
717           + I treats the argument value as an SQL identifier,
718             double-quoting it if necessary. It is an error for the value
719             to be null (equivalent to quote_ident).
720           + L quotes the argument value as an SQL literal. A null value is
721             displayed as the string NULL, without quotes (equivalent to
722             quote_nullable).
723
724    In addition to the format specifiers described above, the special
725    sequence %% may be used to output a literal % character.
726
727    Here are some examples of the basic format conversions:
728 SELECT format('Hello %s', 'World');
729 Result: Hello World
730
731 SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three');
732 Result: Testing one, two, three, %
733
734 SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly');
735 Result: INSERT INTO "Foo bar" VALUES('O''Reilly')
736
737 SELECT format('INSERT INTO %I VALUES(%L)', 'locations', 'C:\Program Files');
738 Result: INSERT INTO locations VALUES('C:\Program Files')
739
740    Here are examples using width fields and the - flag:
741 SELECT format('|%10s|', 'foo');
742 Result: |       foo|
743
744 SELECT format('|%-10s|', 'foo');
745 Result: |foo       |
746
747 SELECT format('|%*s|', 10, 'foo');
748 Result: |       foo|
749
750 SELECT format('|%*s|', -10, 'foo');
751 Result: |foo       |
752
753 SELECT format('|%-*s|', 10, 'foo');
754 Result: |foo       |
755
756 SELECT format('|%-*s|', -10, 'foo');
757 Result: |foo       |
758
759    These examples show use of position fields:
760 SELECT format('Testing %3$s, %2$s, %1$s', 'one', 'two', 'three');
761 Result: Testing three, two, one
762
763 SELECT format('|%*2$s|', 'foo', 10, 'bar');
764 Result: |       bar|
765
766 SELECT format('|%1$*2$s|', 'foo', 10, 'bar');
767 Result: |       foo|
768
769    Unlike the standard C function sprintf, PostgreSQL's format function
770    allows format specifiers with and without position fields to be mixed
771    in the same format string. A format specifier without a position field
772    always uses the next argument after the last argument consumed. In
773    addition, the format function does not require all function arguments
774    to be used in the format string. For example:
775 SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three');
776 Result: Testing three, two, three
777
778    The %I and %L format specifiers are particularly useful for safely
779    constructing dynamic SQL statements. See Example 41.1.