func.sgml 215.0 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.125 2002/09/22 17:27:23 tgl Exp $
3
PostgreSQL documentation
4
-->
5

6 7 8 9 10
 <chapter id="functions">
  <title>Functions and Operators</title>

  <indexterm zone="functions">
   <primary>functions</primary>
11 12
  </indexterm>

13 14
  <indexterm zone="functions">
   <primary>operators</primary>
15 16
  </indexterm>

17
  <para>
18 19 20 21 22 23 24 25
   <productname>PostgreSQL</productname> provides a large number of
   functions and operators for the built-in data types.  Users can also
   define their own functions and operators, as described in the
   <citetitle>Programmer's Guide</citetitle>.  The
   <application>psql</application> commands <command>\df</command> and
   <command>\do</command> can be used to show the list of all actually
   available functions and operators, respectively.
  </para>
26

27 28 29 30 31 32 33 34 35 36
  <para>
   If you are concerned about portability then take note that most of
   the functions and operators described in this chapter, with the
   exception of the most trivial arithmetic and comparison operators
   and some explicitly marked functions, are not specified by the
   <acronym>SQL</acronym>
   standard. Some of this extended functionality is present in other
   <acronym>RDBMS</acronym> products, and in many cases this
   functionality is compatible and consistent between various products.
  </para>
37

38 39 40 41 42 43 44

  <sect1 id="functions-logical">
   <title>Logical Operators</title>

   <indexterm zone="functions-logical">
    <primary>operators</primary>
    <secondary>logical</secondary>
45 46 47
   </indexterm>

   <indexterm>
48 49 50
    <primary>Boolean</primary>
    <secondary>operators</secondary>
    <see>operators, logical</see>
51 52
   </indexterm>

53 54
   <para>
    The usual logical operators are available:
55

56 57 58 59
    <indexterm>
     <primary>and</primary>
     <secondary>operator</secondary>
    </indexterm>
60

61 62 63 64
    <indexterm>
     <primary>or</primary>
     <secondary>operator</secondary>
    </indexterm>
65

66 67 68 69
    <indexterm>
     <primary>not</primary>
     <secondary>operator</secondary>
    </indexterm>
70

71 72 73 74 75
    <simplelist>
     <member>AND</member>
     <member>OR</member>
     <member>NOT</member>
    </simplelist>
76

P
Peter Eisentraut 已提交
77
    <acronym>SQL</acronym> uses a three-valued Boolean logic where the null value represents
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    <quote>unknown</quote>.  Observe the following truth tables:

    <informaltable>
     <tgroup cols="4">
      <thead>
       <row>
        <entry><replaceable>a</replaceable></entry>
        <entry><replaceable>b</replaceable></entry>
        <entry><replaceable>a</replaceable> AND <replaceable>b</replaceable></entry>
        <entry><replaceable>a</replaceable> OR <replaceable>b</replaceable></entry>
       </row>
      </thead>

      <tbody>
       <row>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>TRUE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>TRUE</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
       </row>

       <row>
        <entry>FALSE</entry>
        <entry>NULL</entry>
        <entry>FALSE</entry>
        <entry>NULL</entry>
       </row>

       <row>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
       </row>
      </tbody>
     </tgroup>
    </informaltable>

    <informaltable>
     <tgroup cols="2">
      <thead>
       <row>
        <entry><replaceable>a</replaceable></entry>
        <entry>NOT <replaceable>a</replaceable></entry>
       </row>
      </thead>

      <tbody>
       <row>
        <entry>TRUE</entry>
        <entry>FALSE</entry>
       </row>
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
       <row>
        <entry>FALSE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>NULL</entry>
        <entry>NULL</entry>
       </row>
      </tbody>
     </tgroup>
    </informaltable>
   </para>
  </sect1>

  <sect1 id="functions-comparison">
   <title>Comparison Operators</title>

   <indexterm zone="functions-comparison">
    <primary>comparison</primary>
    <secondary>operators</secondary>
   </indexterm>

   <table>
    <title>Comparison Operators</title>
    <tgroup cols="2">
     <thead>
179
      <row>
180 181
       <entry>Operator</entry>
       <entry>Description</entry>
182
      </row>
183
     </thead>
184

185
     <tbody>
186
      <row>
187 188
       <entry> <literal>&lt;</literal> </entry>
       <entry>less than</entry>
189
      </row>
190 191

      <row>
192 193
       <entry> <literal>&gt;</literal> </entry>
       <entry>greater than</entry>
194
      </row>
195 196

      <row>
197 198
       <entry> <literal>&lt;=</literal> </entry>
       <entry>less than or equal to</entry>
199 200 201
      </row>

      <row>
202 203
       <entry> <literal>&gt;=</literal> </entry>
       <entry>greater than or equal to</entry>
204 205 206
      </row>

      <row>
207 208
       <entry> <literal>=</literal> </entry>
       <entry>equal</entry>
209 210 211
      </row>

      <row>
212 213
       <entry> <literal>&lt;&gt;</literal> or <literal>!=</literal> </entry>
       <entry>not equal</entry>
214 215 216
      </row>
     </tbody>
    </tgroup>
217
   </table>
218

219 220 221 222 223 224 225 226
   <note>
    <para>
     The <literal>!=</literal> operator is converted to
     <literal>&lt;&gt;</literal> in the parser stage.  It is not
     possible to implement <literal>!=</literal> and
     <literal>&lt;&gt;</literal> operators that do different things.
    </para>
   </note>
227

T
Thomas G. Lockhart 已提交
228
   <para>
229 230 231 232 233 234
    Comparison operators are available for all data types where this
    makes sense.  All comparison operators are binary operators that
    return values of type <type>boolean</type>; expressions like
    <literal>1 &lt; 2 &lt; 3</literal> are not valid (because there is
    no <literal>&lt;</literal> operator to compare a Boolean value with
    <literal>3</literal>).
T
Thomas G. Lockhart 已提交
235
   </para>
236

237 238 239 240 241 242 243
   <para>
    <indexterm>
     <primary>between</primary>
    </indexterm>
    In addition to the comparison operators, the special
    <token>BETWEEN</token> construct is available.
    <synopsis>
P
Peter Eisentraut 已提交
244
<replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
245 246 247
    </synopsis>
    is equivalent to
    <synopsis>
B
Bruce Momjian 已提交
248
<replaceable>a</replaceable> &gt;= <replaceable>x</replaceable> AND <replaceable>a</replaceable> &lt;= <replaceable>y</replaceable>
249 250 251
    </synopsis>
    Similarly,
    <synopsis>
P
Peter Eisentraut 已提交
252
<replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
253 254 255
    </synopsis>
    is equivalent to
    <synopsis>
B
Bruce Momjian 已提交
256
<replaceable>a</replaceable> &lt; <replaceable>x</replaceable> OR <replaceable>a</replaceable> &gt; <replaceable>y</replaceable>
257 258 259 260 261
    </synopsis>
    There is no difference between the two respective forms apart from
    the <acronym>CPU</acronym> cycles required to rewrite the first one
    into the second one internally.
   </para>
P
Peter Eisentraut 已提交
262

263
   <para>
P
Peter Eisentraut 已提交
264
    To check whether a value is or is not null, use the constructs
265
    <synopsis>
266 267
<replaceable>expression</replaceable> IS NULL
<replaceable>expression</replaceable> IS NOT NULL
268 269 270
    </synopsis>
    or the equivalent, but less standard, constructs
    <synopsis>
271 272
<replaceable>expression</replaceable> ISNULL
<replaceable>expression</replaceable> NOTNULL
273 274
    </synopsis>
   </para>
275

276 277 278
   <para>
    Do <emphasis>not</emphasis> write
    <literal><replaceable>expression</replaceable> = NULL</literal>
P
Peter Eisentraut 已提交
279 280 281
    because <literal>NULL</> is not <quote>equal to</quote>
    <literal>NULL</>.  (The null value represents an unknown value,
    and it is not known whether two unknown values are equal.)
282
   </para>
283

284 285 286 287
   <para>
    Some applications may (incorrectly) require that
    <literal><replaceable>expression</replaceable> = NULL</literal>
    returns true if <replaceable>expression</replaceable> evaluates to
P
Peter Eisentraut 已提交
288
    the null value.  To support these applications, the run-time option
289 290 291 292 293 294 295
    <varname>transform_null_equals</varname> can be turned on (e.g.,
    <literal>SET transform_null_equals TO ON;</literal>).
    <productname>PostgreSQL</productname> will then convert
    <literal>x = NULL</literal> clauses to
    <literal>x IS NULL</literal>.  This was 
    the default behavior in releases 6.5 through 7.1.
   </para>
296

297 298 299
   <para>
    Boolean values can also be tested using the constructs
    <synopsis>
300 301 302 303 304 305
<replaceable>expression</replaceable> IS TRUE
<replaceable>expression</replaceable> IS NOT TRUE
<replaceable>expression</replaceable> IS FALSE
<replaceable>expression</replaceable> IS NOT FALSE
<replaceable>expression</replaceable> IS UNKNOWN
<replaceable>expression</replaceable> IS NOT UNKNOWN
306 307
    </synopsis>
    These are similar to <literal>IS NULL</literal> in that they will
P
Peter Eisentraut 已提交
308 309
    always return true or false, never a null value, even when the operand is null.
    A null input is treated as the logical value <quote>unknown</>.
310 311
   </para>
  </sect1>
312

313 314
  <sect1 id="functions-math">
   <title>Mathematical Functions and Operators</title>
315

316 317 318 319 320 321 322
   <para>
    Mathematical operators are provided for many
    <productname>PostgreSQL</productname> types. For types without
    common mathematical conventions for all possible permutations 
    (e.g. date/time types) we
    describe the actual behavior in subsequent sections.
   </para>
323 324

   <table>
325
    <title>Mathematical Operators</title>
326

327
    <tgroup cols="4">
328 329
     <thead>
      <row>
330 331
       <entry>Name</entry>
       <entry>Description</entry>
332 333 334 335 336 337 338
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
339 340 341 342
       <entry> <literal>+</literal> </entry>
       <entry>Addition</entry>
       <entry>2 + 3</entry>
       <entry>5</entry>
343
      </row>
344

345
      <row>
346 347 348 349
       <entry> <literal>-</literal> </entry>
       <entry>Subtraction</entry>
       <entry>2 - 3</entry>
       <entry>-1</entry>
350
      </row>
351

352
      <row>
353 354 355 356
       <entry> <literal>*</literal> </entry>
       <entry>Multiplication</entry>
       <entry>2 * 3</entry>
       <entry>6</entry>
357
      </row>
358

359
      <row>
360 361 362 363
       <entry> <literal>/</literal> </entry>
       <entry>Division (integer division truncates results)</entry>
       <entry>4 / 2</entry>
       <entry>2</entry>
364
      </row>
365

366
      <row>
367 368 369 370
       <entry> <literal>%</literal> </entry>
       <entry>Modulo (remainder)</entry>
       <entry>5 % 4</entry>
       <entry>1</entry>
371
      </row>
372

373
      <row>
374 375 376 377
       <entry> <literal>^</literal> </entry>
       <entry>Exponentiation</entry>
       <entry>2.0 ^ 3.0</entry>
       <entry>8</entry>
378 379
      </row>

380 381 382 383 384 385
      <row>
       <entry> <literal>|/</literal> </entry>
       <entry>Square root</entry>
       <entry>|/ 25.0</entry>
       <entry>5</entry>
      </row>
386

387 388 389 390 391 392
      <row>
       <entry> <literal>||/</literal> </entry>
       <entry>Cube root</entry>
       <entry>||/ 27.0</entry>
       <entry>3</entry>
      </row>
393

394 395 396 397 398 399
      <row>
       <entry> <literal>!</literal> </entry>
       <entry>Factorial</entry>
       <entry>5 !</entry>
       <entry>120</entry>
      </row>
400

401 402 403 404 405 406
      <row>
       <entry> <literal>!!</literal> </entry>
       <entry>Factorial (prefix operator)</entry>
       <entry>!! 5</entry>
       <entry>120</entry>
      </row>
407

408 409 410 411 412 413
      <row>
       <entry> <literal>@</literal> </entry>
       <entry>Absolute value</entry>
       <entry>@ -5.0</entry>
       <entry>5</entry>
      </row>
414

415 416 417 418 419 420
      <row>
       <entry> <literal>&amp;</literal> </entry>
       <entry>Binary AND</entry>
       <entry>91 & 15</entry>
       <entry>11</entry>
      </row>
421

422 423 424 425 426 427
      <row>
       <entry> <literal>|</literal> </entry>
       <entry>Binary OR</entry>
       <entry>32 | 3</entry>
       <entry>35</entry>
      </row>
428

429 430 431 432 433 434
      <row>
       <entry> <literal>#</literal> </entry>
       <entry>Binary XOR</entry>
       <entry>17 # 5</entry>
       <entry>20</entry>
      </row>
435

436 437 438 439 440 441
      <row>
       <entry> <literal>~</literal> </entry>
       <entry>Binary NOT</entry>
       <entry>~1</entry>
       <entry>-2</entry>
      </row>
442

443 444 445 446 447 448
      <row>
       <entry> &lt;&lt; </entry>
       <entry>Binary shift left</entry>
       <entry>1 &lt;&lt; 4</entry>
       <entry>16</entry>
      </row>
449

450 451 452 453 454 455
      <row>
       <entry> &gt;&gt; </entry>
       <entry>Binary shift right</entry>
       <entry>8 &gt;&gt; 2</entry>
       <entry>2</entry>
      </row>
456

457 458 459
     </tbody>
    </tgroup>
   </table>
460

461 462 463
   <para>
    The <quote>binary</quote> operators are also available for the bit
    string types <type>BIT</type> and <type>BIT VARYING</type>.
464

465 466
    <table>
     <title>Bit String Binary Operators</title>
467

468 469 470 471 472 473 474
     <tgroup cols="2">
      <thead>
       <row>
        <entry>Example</entry>
        <entry>Result</entry>
       </row>
      </thead>
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      <tbody>
       <row>
        <entry>B'10001' & B'01101'</entry>
        <entry>00001</entry>
       </row>
       <row>
        <entry>B'10001' | B'01101'</entry>
        <entry>11101</entry>
       </row>
       <row>
        <entry>B'10001' # B'01101'</entry>
        <entry>11110</entry>
       </row>
       <row>
        <entry>~ B'10001'</entry>
        <entry>01110</entry>
       </row>
       <row>
        <entry>B'10001' << 3</entry>
        <entry>01000</entry>
       </row>
       <row>
        <entry>B'10001' >> 2</entry>
        <entry>00100</entry>
       </row>
      </tbody>
     </tgroup>
    </table>
504

505 506 507 508 509
    Bit string arguments to <literal>&</literal>, <literal>|</literal>,
    and <literal>#</literal> must be of equal length.  When bit
    shifting, the original length of the string is preserved, as shown
    here.
   </para>
510

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
   <table tocentry="1">
    <title>Mathematical Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
       <entry><function>abs</function>(<replaceable>x</replaceable>)</entry>
       <entry>(same as x)</entry>
       <entry>absolute value</entry>
       <entry><literal>abs(-17.4)</literal></entry>
       <entry>17.4</entry>
      </row>

      <row>
       <entry><function>cbrt</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>cube root</entry>
       <entry><literal>cbrt(27.0)</literal></entry>
       <entry>3</entry>
      </row>

      <row>
       <entry><function>ceil</function>(<type>numeric</type>)</entry>
       <entry><type>numeric</type></entry>
       <entry>smallest integer not less than argument</entry>
       <entry><literal>ceil(-42.8)</literal></entry>
       <entry>-42</entry>
      </row>

      <row>
       <entry><function>degrees</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>radians to degrees</entry>
       <entry><literal>degrees(0.5)</literal></entry>
       <entry>28.6478897565412</entry>
      </row>

      <row>
       <entry><function>exp</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>exponential</entry>
       <entry><literal>exp(1.0)</literal></entry>
       <entry>2.71828182845905</entry>
      </row>

      <row>
       <entry><function>floor</function>(<type>numeric</type>)</entry>
       <entry><type>numeric</type></entry>
       <entry>largest integer not greater than argument</entry>
       <entry><literal>floor(-42.8)</literal></entry>
       <entry>-43</entry>
      </row>

      <row>
       <entry><function>ln</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>natural logarithm</entry>
       <entry><literal>ln(2.0)</literal></entry>
       <entry>0.693147180559945</entry>
      </row>

      <row>
       <entry><function>log</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>base 10 logarithm</entry>
       <entry><literal>log(100.0)</literal></entry>
       <entry>2</entry>
      </row>

      <row>
       <entry><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
        <parameter>x</parameter> <type>numeric</type>)</entry>
       <entry><type>numeric</type></entry>
       <entry>logarithm to base <parameter>b</parameter></entry>
       <entry><literal>log(2.0, 64.0)</literal></entry>
       <entry>6.0000000000</entry>
      </row>

      <row>
       <entry><function>mod</function>(<parameter>y</parameter>,
        <parameter>x</parameter>)</entry>
       <entry>(same as argument types)</entry>
       <entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
       <entry><literal>mod(9,4)</literal></entry>
       <entry>1</entry>
      </row>

      <row>
       <entry><function>pi</function>()</entry>
       <entry><type>dp</type></entry>
       <entry><quote>Pi</quote> constant</entry>
       <entry><literal>pi()</literal></entry>
       <entry>3.14159265358979</entry>
      </row>

      <row>
       <entry><function>pow</function>(<parameter>e</parameter> <type>dp</type>,
        <parameter>n</parameter> <type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>raise a number to exponent <parameter>e</parameter></entry>
       <entry><literal>pow(9.0, 3.0)</literal></entry>
       <entry>729</entry>
      </row>

      <row>
       <entry><function>radians</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>degrees to radians</entry>
       <entry><literal>radians(45.0)</literal></entry>
       <entry>0.785398163397448</entry>
      </row>

      <row>
       <entry><function>random</function>()</entry>
       <entry><type>dp</type></entry>
       <entry>value between 0.0 to 1.0</entry>
       <entry><literal>random()</literal></entry>
       <entry></entry>
      </row>

      <row>
       <entry><function>round</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>round to nearest integer</entry>
       <entry><literal>round(42.4)</literal></entry>
       <entry>42</entry>
      </row>

      <row>
       <entry><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
       <entry><type>numeric</type></entry>
       <entry>round to <parameter>s</parameter> decimal places</entry>
       <entry><literal>round(42.4382, 2)</literal></entry>
       <entry>42.44</entry>
      </row>
655 656
<!--
     <row>
657
      <entry><function>setseed</function>(<replaceable>new-seed</replaceable>)</entry>
658
      <entry>set seed for subsequent random() calls</entry>
659
      <entry><literal>setseed(0.54823)</literal></entry>
660 661 662
      <entry></entry>
     </row>
-->
663 664 665 666 667 668 669
      <row>
       <entry><function>sign</function>(<type>numeric</type>)</entry>
       <entry><type>numeric</type></entry>
       <entry>sign of the argument (-1, 0, +1)</entry>
       <entry><literal>sign(-8.4)</literal></entry>
       <entry>-1</entry>
      </row>
P
Peter Eisentraut 已提交
670

671 672 673 674 675 676 677
      <row>
       <entry><function>sqrt</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>square root</entry>
       <entry><literal>sqrt(2.0)</literal></entry>
       <entry>1.4142135623731</entry>
      </row>
678

679 680 681 682 683 684 685
      <row>
       <entry><function>trunc</function>(<type>dp</type>)</entry>
       <entry><type>dp</type></entry>
       <entry>truncate toward zero</entry>
       <entry><literal>trunc(42.8)</literal></entry>
       <entry>42</entry>
      </row>
686

687 688
      <row>
       <entry><function>trunc</function>(<type>numeric</type>,
689
        <parameter>r</parameter> <type>integer</type>)</entry>
690 691 692 693 694
       <entry><type>numeric</type></entry>
       <entry>truncate to <parameter>s</parameter> decimal places</entry>
       <entry><literal>trunc(42.4382, 2)</literal></entry>
       <entry>42.43</entry>
      </row>
695

696 697 698
     </tbody>
    </tgroup>
   </table>
699

700 701 702 703 704 705 706 707 708 709 710 711 712 713
   <para>
    In the table above, <literal>dp</literal> indicates <type>double precision</type>.
    The functions <function>exp</function>, <function>ln</function>,
    <function>log</function>, <function>pow</function>,
    <function>round</function> (1 argument), <function>sqrt</function>,
    and <function>trunc</function> (1 argument) are also available for
    the type <type>numeric</type> in place of
    <type>double precision</type>.
    Functions returning a <type>numeric</type> result take
    <type>numeric</type> input arguments, unless otherwise specified.
    Many of these functions are implemented on top
    of the host system's C library; accuracy and behavior in boundary cases
    could therefore vary depending on the host system.
   </para>
714

715 716
   <table>
    <title>Trigonometric Functions</title>
717

718 719 720 721 722 723 724
    <tgroup cols="2">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Description</entry>
      </row>
     </thead>
725

726 727 728 729 730
     <tbody>
      <row>
       <entry><function>acos</function>(<replaceable>x</replaceable>)</entry>
       <entry>inverse cosine</entry>
      </row>
731

732 733 734 735
      <row>
       <entry><function>asin</function>(<replaceable>x</replaceable>)</entry>
       <entry>inverse sine</entry>
      </row>
736

737 738 739 740
      <row>
       <entry><function>atan</function>(<replaceable>x</replaceable>)</entry>
       <entry>inverse tangent</entry>
      </row>
741

742 743 744 745
      <row>
       <entry><function>atan2</function>(<replaceable>x</replaceable>,
        <replaceable>y</replaceable>)</entry>
       <entry>inverse tangent of
B
Bruce Momjian 已提交
746
        <replaceable>x</replaceable>/<replaceable>y</replaceable></entry>
747
      </row>
748

749 750 751 752
      <row>
       <entry><function>cos</function>(<replaceable>x</replaceable>)</entry>
       <entry>cosine</entry>
      </row>
753

754 755 756 757
      <row>
       <entry><function>cot</function>(<replaceable>x</replaceable>)</entry>
       <entry>cotangent</entry>
      </row>
758

759 760 761 762
      <row>
       <entry><function>sin</function>(<replaceable>x</replaceable>)</entry>
       <entry>sine</entry>
      </row>
763

764 765 766 767 768 769 770
      <row>
       <entry><function>tan</function>(<replaceable>x</replaceable>)</entry>
       <entry>tangent</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
771

772 773 774 775
   <para>
    All trigonometric functions have arguments and return values of
    type <type>double precision</type>.
   </para>
776

777
  </sect1>
778 779


780 781
  <sect1 id="functions-string">
   <title>String Functions and Operators</title>
782

783 784 785 786 787 788 789 790 791 792 793 794
   <para>
    This section describes functions and operators for examining and
    manipulating string values.  Strings in this context include values
    of all the types <type>CHARACTER</type>, <type>CHARACTER
     VARYING</type>, and <type>TEXT</type>.  Unless otherwise noted, all
    of the functions listed below work on all of these types, but be
    wary of potential effects of the automatic padding when using the
    <type>CHARACTER</type> type.  Generally, the functions described
    here also work on data of non-string types by converting that data
    to a string representation first.  Some functions also exist
    natively for bit-string types.
   </para>
795

796 797 798 799 800 801 802
   <para>
    <acronym>SQL</acronym> defines some string functions with a special syntax where
    certain keywords rather than commas are used to separate the
    arguments.  Details are in <xref linkend="functions-string-sql">.
    These functions are also implemented using the regular syntax for
    function invocation.  (See <xref linkend="functions-string-other">.)
   </para>
803

804 805 806 807 808 809 810 811 812 813 814 815
   <table id="functions-string-sql">
    <title><acronym>SQL</acronym> String Functions and Operators</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>  
      </row>
     </thead>
816

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
     <tbody>
      <row>
       <entry> <parameter>string</parameter> <literal>||</literal>
        <parameter>string</parameter> </entry>
       <entry> <type>text</type> </entry>
       <entry>
        string concatenation
        <indexterm>
         <primary>character strings</primary>
         <secondary>concatenation</secondary>
        </indexterm>
       </entry>
       <entry><literal>'Postgre' || 'SQL'</literal></entry>
       <entry><literal>PostgreSQL</literal></entry>
      </row>
832

833 834 835 836 837 838 839
      <row>
       <entry><function>bit_length</function>(<parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>number of bits in string</entry>
       <entry><literal>bit_length('jose')</literal></entry>
       <entry><literal>32</literal></entry>
      </row>
P
Peter Eisentraut 已提交
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
      <row>
       <entry><function>char_length</function>(<parameter>string</parameter>) or <function>character_length</function>(<parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>
        number of characters in string
        <indexterm>
         <primary>character strings</primary>
         <secondary>length</secondary>
        </indexterm>
        <indexterm>
         <primary>length</primary>
         <secondary>character strings</secondary>
         <see>character strings, length</see>
        </indexterm>
       </entry>
       <entry><literal>char_length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>
859

T
Tatsuo Ishii 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872
      <row>
       <entry><function>convert</function>(<parameter>string</parameter>
       using <parameter>conversion_name</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Change encoding using specified conversion name.
       Conversions can be defined by <command>CREATE CONVERSION</command>.
       Also there are some pre-defined conversion names. 
       See <xref linkend="conversion-names"> for available
       conversion names. </entry>
       <entry><literal>convert('PostgreSQL' using iso8859_1_to_utf8)</literal></entry>
       <entry><literal>'PostgreSQL' in UNICODE(UTF-8) encoding</literal></entry>
      </row>

873 874 875 876 877 878 879
      <row>
       <entry><function>lower</function>(<parameter>string</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Convert string to lower case.</entry>
       <entry><literal>lower('TOM')</literal></entry>
       <entry><literal>tom</literal></entry>
      </row>
880

881 882 883 884 885 886 887
      <row>
       <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>number of bytes in string</entry>
       <entry><literal>octet_length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>
888

889 890 891 892 893 894 895 896 897 898 899 900
      <row>
       <entry><function>overlay</function>(<parameter>string</parameter> placing <parameter>string</parameter> from <type>integer</type> <optional>for <type>integer</type></optional>)</entry>
       <entry><type>text</type></entry>
       <entry>
        insert substring
        <indexterm>
         <primary>overlay</primary>
        </indexterm>
       </entry>
       <entry><literal>overlay('Txxxxas' placing 'hom' from 2 for 4)</literal></entry>
       <entry><literal>Thomas</literal></entry>
      </row>
901

902 903 904 905 906 907 908
      <row>
       <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>location of specified substring</entry>
       <entry><literal>position('om' in 'Thomas')</literal></entry>
       <entry><literal>3</literal></entry>
      </row>
909

910 911 912 913 914 915 916 917 918 919 920 921
      <row>
       <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
       <entry><type>text</type></entry>
       <entry>
        extract substring
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
       <entry><literal>hom</literal></entry>
      </row>
922

923
      <row>
924
       <entry><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable>)</entry>
925 926
       <entry><type>text</type></entry>
       <entry>
927
        extract substring matching POSIX regular expression
928 929 930 931
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
932
       <entry><literal>substring('Thomas' from '...$')</literal></entry>
933 934
       <entry><literal>mas</literal></entry>
      </row>
935

936 937 938 939 940 941 942 943 944 945 946 947 948
      <row>
       <entry><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable> for <replaceable>escape</replaceable>)</entry>
       <entry><type>text</type></entry>
       <entry>
        extract substring matching SQL99 regular expression
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Thomas' from '%#"o_a#"_' for '#')</literal></entry>
       <entry><literal>oma</literal></entry>
      </row>

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
      <row>
       <entry>
        <function>trim</function>(<optional>leading | trailing | both</optional>
        <optional><parameter>characters</parameter></optional> from
        <parameter>string</parameter>)
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Removes the longest string containing only the
        <parameter>characters</parameter> (a space by default) from the
        beginning/end/both ends of the <parameter>string</parameter>.
       </entry>
       <entry><literal>trim(both 'x' from 'xTomxx')</literal></entry>
       <entry><literal>Tom</literal></entry>
      </row>
964

965 966 967 968 969 970 971 972 973 974
      <row>
       <entry><function>upper</function>(<parameter>string</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Convert string to upper case.</entry>
       <entry><literal>upper('tom')</literal></entry>
       <entry><literal>TOM</literal></entry>
      </row>
     </tbody>
    </tgroup>
   </table>
975

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
   <para>
    Additional string manipulation functions are available and are
    listed below.  Some of them are used internally to implement the
    <acronym>SQL</acronym>-standard string functions listed above.
   </para>

   <table id="functions-string-other">
    <title>Other String Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
       <entry><function>ascii</function>(<type>text</type>)</entry>
       <entry>integer</entry>
       <entry>Returns the <acronym>ASCII</acronym> code of the first character of the argument.</entry>
       <entry><literal>ascii('x')</literal></entry>
       <entry><literal>120</literal></entry>
      </row>

      <row>
       <entry><function>btrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
       <entry><type>text</type></entry>
       <entry>
        Remove (trim) the longest string consisting only of characters
        in <parameter>trim</parameter> from the start and end of
        <parameter>string</parameter>.
       </entry>
       <entry><literal>btrim('xyxtrimyyx','xy')</literal></entry>
       <entry><literal>trim</literal></entry>
      </row>

      <row>
       <entry><function>chr</function>(<type>integer</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Returns the character with the given <acronym>ASCII</acronym> code.</entry>
       <entry><literal>chr(65)</literal></entry>
       <entry><literal>A</literal></entry>
      </row>

      <row>
       <entry>
        <function>convert</function>(<parameter>string</parameter>
        <type>text</type>,
        <optional><parameter>src_encoding</parameter> <type>name</type>,</optional>
        <parameter>dest_encoding</parameter> <type>name</type>)
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Converts string using <parameter>dest_encoding</parameter>.
        The original encoding is specified by
        <parameter>src_encoding</parameter>.  If
        <parameter>src_encoding</parameter> is omitted, database
        encoding is assumed.
       </entry>
       <entry><literal>convert('text_in_unicode', 'UNICODE', 'LATIN1')</literal></entry>
       <entry><literal>text_in_unicode</literal> represented in ISO 8859-1</entry>
      </row>

      <row>
       <entry>
        <function>decode</function>(<parameter>string</parameter> <type>text</type>,
        <parameter>type</parameter> <type>text</type>)
       </entry>
       <entry><type>bytea</type></entry>
       <entry>
        Decodes binary data from <parameter>string</parameter> previously 
        encoded with encode().  Parameter type is same as in encode().
       </entry>
       <entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
       <entry><literal>123\000\001</literal></entry>
      </row>       

      <row>
       <entry>
        <function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
        <parameter>type</parameter> <type>text</type>)
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Encodes binary data to <acronym>ASCII</acronym>-only representation.  Supported
        types are: 'base64', 'hex', 'escape'.
       </entry>
       <entry><literal>encode('123\\000\\001', 'base64')</literal></entry>
       <entry><literal>MTIzAAE=</literal></entry>
      </row>       

      <row>
       <entry><function>initcap</function>(<type>text</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Converts first letter of each word (whitespace separated) to upper case.</entry>
       <entry><literal>initcap('hi thomas')</literal></entry>
       <entry><literal>Hi Thomas</literal></entry>
      </row>

      <row>
       <entry><function>length</function>(<parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>
        length of string
        <indexterm>
         <primary>character strings</primary>
         <secondary>length</secondary>
        </indexterm>
        <indexterm>
         <primary>length</primary>
         <secondary>character strings</secondary>
         <see>character strings, length</see>
        </indexterm>
       </entry>
       <entry><literal>length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>

      <row>
       <entry>
        <function>lpad</function>(<parameter>string</parameter> <type>text</type>,
        <parameter>length</parameter> <type>integer</type>
        <optional>, <parameter>fill</parameter> <type>text</type></optional>)
       </entry>
       <entry>text</entry>
       <entry>
        Fills up the <parameter>string</parameter> to length
        <parameter>length</parameter> by prepending the characters
        <parameter>fill</parameter> (a space by default).  If the
        <parameter>string</parameter> is already longer than
        <parameter>length</parameter> then it is truncated (on the
        right).
       </entry>
       <entry><literal>lpad('hi', 5, 'xy')</literal></entry>
       <entry><literal>xyxhi</literal></entry>
      </row>

      <row>
       <entry><function>ltrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>text</parameter> <type>text</type>)</entry>
       <entry><type>text</type></entry>
       <entry>
        Removes the longest string containing only characters from
        <parameter>trim</parameter> from the start of the string.
       </entry>
       <entry><literal>ltrim('zzzytrim','xyz')</literal></entry>
       <entry><literal>trim</literal></entry>
      </row>

      <row>
       <entry><function>pg_client_encoding</function>()</entry>
       <entry><type>name</type></entry>
       <entry>
        Returns current client encoding name.
       </entry>
       <entry><literal>pg_client_encoding()</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
      </row>

      <row>
       <entry><function>quote_ident</function>(<parameter>string</parameter> text)</entry>
       <entry><type>text</type></entry>
       <entry>
        Returns the given string suitably quoted to be used as an identifier
	in an SQL query string.
	Quotes are added only if necessary (i.e., if the string contains
	non-identifier characters or would be case-folded).
	Embedded quotes are properly doubled.
       </entry>
       <entry><literal>quote_ident('Foo')</literal></entry>
       <entry><literal>"Foo"</literal></entry>
      </row>

      <row>
       <entry><function>quote_literal</function>(<parameter>string</parameter> text)</entry>
       <entry><type>text</type></entry>
       <entry>
        Returns the given string suitably quoted to be used as a literal
	in an SQL query string.
	Embedded quotes and backslashes are properly doubled.
       </entry>
       <entry><literal>quote_literal('O\'Reilly')</literal></entry>
       <entry><literal>'O''Reilly'</literal></entry>
      </row>

      <row>
       <entry><function>repeat</function>(<type>text</type>, <type>integer</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Repeat text a number of times.</entry>
       <entry><literal>repeat('Pg', 4)</literal></entry>
       <entry><literal>PgPgPgPg</literal></entry>
      </row>

      <row>
       <entry><function>replace</function>(<parameter>string</parameter> <type>text</type>,
       <parameter>from</parameter> <type>text</type>,
       <parameter>to</parameter> <type>text</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Replace all occurrences in <parameter>string</parameter> of substring
        <parameter>from</parameter> with substring <parameter>to</parameter>
       </entry>
       <entry><literal>replace('abcdefabcdef', 'cd', 'XX')</literal></entry>
       <entry><literal>abXXefabXXef</literal></entry>
      </row>

      <row>
       <entry>
        <function>rpad</function>(<parameter>string</parameter> <type>text</type>,
        <parameter>length</parameter> <type>integer</type>
        <optional>, <parameter>fill</parameter> <type>text</type></optional>)
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Fills up the <parameter>string</parameter> to length
        <parameter>length</parameter> by appending the characters
        <parameter>fill</parameter> (a space by default).  If the
        <parameter>string</parameter> is already longer than
        <parameter>length</parameter> then it is truncated.
       </entry>
       <entry><literal>rpad('hi', 5, 'xy')</literal></entry>
       <entry><literal>hixyx</literal></entry>
      </row>

      <row>
       <entry><function>rtrim</function>(<parameter>string</parameter>
        text, <parameter>trim</parameter> text)</entry>
       <entry><type>text</type></entry>
       <entry>
        Removes the longest string containing only characters from
        <parameter>trim</parameter> from the end of the string.
       </entry>
       <entry><literal>rtrim('trimxxxx','x')</literal></entry>
       <entry><literal>trim</literal></entry>
      </row>

      <row>
       <entry><function>split_part</function>(<parameter>string</parameter> <type>text</type>,
       <parameter>delimiter</parameter> <type>text</type>,
       <parameter>column</parameter> <type>integer</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Split <parameter>string</parameter> on <parameter>delimiter</parameter>
        returning the resulting (one based) <parameter>column</parameter> number.
       </entry>
       <entry><literal>split_part('abc~@~def~@~ghi','~@~',2)</literal></entry>
       <entry><literal>def</literal></entry>
      </row>

      <row>
       <entry><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>
        Locates specified substring. (same as
        <literal>position(<parameter>substring</parameter> in
         <parameter>string</parameter>)</literal>, but note the reversed
        argument order)
       </entry>
       <entry><literal>strpos('high','ig')</literal></entry>
       <entry><literal>2</literal></entry>
      </row>

      <row>
       <entry><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</entry>
       <entry><type>text</type></entry>
       <entry>
        Extracts specified substring. (same as
        <literal>substring(<parameter>string</parameter> from <parameter>from</parameter> for <parameter>count</parameter>)</literal>)
       </entry>
       <entry><literal>substr('alphabet', 3, 2)</literal></entry>
       <entry><literal>ph</literal></entry>
      </row>

      <row>
       <entry><function>to_ascii</function>(<type>text</type>
        <optional>, <parameter>encoding</parameter></optional>)</entry>
       <entry><type>text</type></entry>
       <entry>Converts text from multibyte encoding to <acronym>ASCII</acronym>.</entry>
       <entry><literal>to_ascii('Karel')</literal></entry>
       <entry><literal>Karel</literal></entry>
      </row>

      <row>
       <entry><function>to_hex</function>(<parameter>number</parameter> <type>integer</type>
       or <type>bigint</type>)</entry>
       <entry><type>text</type></entry>
       <entry>Convert <parameter>number</parameter> to its equivalent hexadecimal
        representation.
       </entry>
       <entry><literal>to_hex(9223372036854775807::bigint)</literal></entry>
       <entry><literal>7fffffffffffffff</literal></entry>
      </row>

      <row>
       <entry>
        <function>translate</function>(<parameter>string</parameter>
        <type>text</type>,
        <parameter>from</parameter> <type>text</type>,
        <parameter>to</parameter> <type>text</type>)
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Any character in <parameter>string</parameter> that matches a
        character in the <parameter>from</parameter> set is replaced by
        the corresponding character in the <parameter>to</parameter>
        set.
       </entry>
       <entry><literal>translate('12345', '14', 'ax')</literal></entry>
       <entry><literal>a23x5</literal></entry>
      </row>       
      
     </tbody>
    </tgroup>
   </table>

   <para>
    The <function>to_ascii</function> function supports conversion from
    LATIN1, LATIN2, WIN1250 (CP1250) only.
   </para>


T
Tatsuo Ishii 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
   <table id="conversion-names">
    <title>Available conversion names</title>
    <tgroup cols="3">
     <thead>
      <row>
       <entry>conversion</entry>
       <entry>source encoding</entry>
       <entry>destination encoding</entry>
      </row>
     </thead>
1308

T
Tatsuo Ishii 已提交
1309 1310
     <tbody>
      <row>
P
Peter Eisentraut 已提交
1311 1312 1313
       <entry><literal>ascii_to_mic</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1314 1315 1316
      </row>

      <row>
P
Peter Eisentraut 已提交
1317 1318 1319
       <entry><literal>ascii_to_utf_8</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1320 1321 1322
      </row>

      <row>
P
Peter Eisentraut 已提交
1323 1324 1325
       <entry><literal>big5_to_euc_tw</literal></entry>
       <entry><literal>BIG5</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1326 1327 1328
      </row>

      <row>
P
Peter Eisentraut 已提交
1329 1330 1331
       <entry><literal>big5_to_mic</literal></entry>
       <entry><literal>BIG5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1332 1333 1334
      </row>

      <row>
P
Peter Eisentraut 已提交
1335 1336 1337
       <entry><literal>big5_to_utf_8</literal></entry>
       <entry><literal>BIG5</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1338 1339 1340
      </row>

      <row>
P
Peter Eisentraut 已提交
1341 1342 1343
       <entry><literal>euc_cn_to_mic</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1344 1345 1346
      </row>

      <row>
P
Peter Eisentraut 已提交
1347 1348 1349
       <entry><literal>euc_cn_to_utf_8</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1350 1351 1352
      </row>

      <row>
P
Peter Eisentraut 已提交
1353 1354 1355
       <entry><literal>euc_jp_to_mic</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1356 1357 1358
      </row>

      <row>
P
Peter Eisentraut 已提交
1359 1360 1361
       <entry><literal>euc_jp_to_sjis</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1362 1363 1364
      </row>

      <row>
P
Peter Eisentraut 已提交
1365 1366 1367
       <entry><literal>euc_jp_to_utf_8</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1368 1369 1370
      </row>

      <row>
P
Peter Eisentraut 已提交
1371 1372 1373
       <entry><literal>euc_kr_to_mic</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1374 1375 1376
      </row>

      <row>
P
Peter Eisentraut 已提交
1377 1378 1379
       <entry><literal>euc_kr_to_utf_8</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1380 1381 1382
      </row>

      <row>
P
Peter Eisentraut 已提交
1383 1384 1385
       <entry><literal>euc_tw_to_big5</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1386 1387 1388
      </row>

      <row>
P
Peter Eisentraut 已提交
1389 1390 1391
       <entry><literal>euc_tw_to_mic</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1392 1393 1394
      </row>

      <row>
P
Peter Eisentraut 已提交
1395 1396 1397
       <entry><literal>euc_tw_to_utf_8</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1398 1399 1400
      </row>

      <row>
P
Peter Eisentraut 已提交
1401 1402 1403
       <entry><literal>gb18030_to_utf_8</literal></entry>
       <entry><literal>GB18030</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1404 1405 1406
      </row>

      <row>
P
Peter Eisentraut 已提交
1407 1408 1409
       <entry><literal>gbk_to_utf_8</literal></entry>
       <entry><literal>GBK</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1410 1411 1412
      </row>

      <row>
P
Peter Eisentraut 已提交
1413 1414 1415
       <entry><literal>iso_8859_10_to_utf_8</literal></entry>
       <entry><literal>LATIN6</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1416 1417 1418
      </row>

      <row>
P
Peter Eisentraut 已提交
1419 1420 1421
       <entry><literal>iso_8859_13_to_utf_8</literal></entry>
       <entry><literal>LATIN7</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1422 1423 1424
      </row>

      <row>
P
Peter Eisentraut 已提交
1425 1426 1427
       <entry><literal>iso_8859_14_to_utf_8</literal></entry>
       <entry><literal>LATIN8</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1428 1429 1430
      </row>

      <row>
P
Peter Eisentraut 已提交
1431 1432 1433
       <entry><literal>iso_8859_15_to_utf_8</literal></entry>
       <entry><literal>LATIN9</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1434 1435 1436
      </row>

      <row>
P
Peter Eisentraut 已提交
1437 1438 1439
       <entry><literal>iso_8859_16_to_utf_8</literal></entry>
       <entry><literal>LATIN10</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1440 1441 1442
      </row>

      <row>
P
Peter Eisentraut 已提交
1443 1444 1445
       <entry><literal>iso_8859_1_to_mic</literal></entry>
       <entry><literal>LATIN1</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1446 1447 1448
      </row>

      <row>
P
Peter Eisentraut 已提交
1449 1450 1451
       <entry><literal>iso_8859_1_to_utf_8</literal></entry>
       <entry><literal>LATIN1</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1452 1453 1454
      </row>

      <row>
P
Peter Eisentraut 已提交
1455 1456 1457
       <entry><literal>iso_8859_2_to_mic</literal></entry>
       <entry><literal>LATIN2</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1458 1459 1460
      </row>

      <row>
P
Peter Eisentraut 已提交
1461 1462 1463
       <entry><literal>iso_8859_2_to_utf_8</literal></entry>
       <entry><literal>LATIN2</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1464 1465 1466
      </row>

      <row>
P
Peter Eisentraut 已提交
1467 1468 1469
       <entry><literal>iso_8859_2_to_win1250</literal></entry>
       <entry><literal>LATIN2</literal></entry>
       <entry><literal>WIN1250</literal></entry>
1470 1471 1472
      </row>

      <row>
P
Peter Eisentraut 已提交
1473 1474 1475
       <entry><literal>iso_8859_3_to_mic</literal></entry>
       <entry><literal>LATIN3</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1476 1477 1478
      </row>

      <row>
P
Peter Eisentraut 已提交
1479 1480 1481
       <entry><literal>iso_8859_3_to_utf_8</literal></entry>
       <entry><literal>LATIN3</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1482 1483 1484
      </row>

      <row>
P
Peter Eisentraut 已提交
1485 1486 1487
       <entry><literal>iso_8859_4_to_mic</literal></entry>
       <entry><literal>LATIN4</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1488 1489 1490
      </row>

      <row>
P
Peter Eisentraut 已提交
1491 1492 1493
       <entry><literal>iso_8859_4_to_utf_8</literal></entry>
       <entry><literal>LATIN4</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1494 1495 1496
      </row>

      <row>
P
Peter Eisentraut 已提交
1497 1498 1499
       <entry><literal>iso_8859_5_to_koi8r</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>KOI8</literal></entry>
1500 1501 1502
      </row>

      <row>
P
Peter Eisentraut 已提交
1503 1504 1505
       <entry><literal>iso_8859_5_to_mic</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1506 1507 1508
      </row>

      <row>
P
Peter Eisentraut 已提交
1509 1510 1511
       <entry><literal>iso_8859_5_to_utf_8</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1512 1513 1514
      </row>

      <row>
P
Peter Eisentraut 已提交
1515 1516 1517
       <entry><literal>iso_8859_5_to_win1251</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>WIN</literal></entry>
1518 1519 1520
      </row>

      <row>
P
Peter Eisentraut 已提交
1521 1522 1523
       <entry><literal>iso_8859_5_to_win866</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>ALT</literal></entry>
1524 1525 1526
      </row>

      <row>
P
Peter Eisentraut 已提交
1527 1528 1529
       <entry><literal>iso_8859_6_to_utf_8</literal></entry>
       <entry><literal>ISO_8859_6</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1530 1531 1532
      </row>

      <row>
P
Peter Eisentraut 已提交
1533 1534 1535
       <entry><literal>iso_8859_7_to_utf_8</literal></entry>
       <entry><literal>ISO_8859_7</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1536 1537 1538
      </row>

      <row>
P
Peter Eisentraut 已提交
1539 1540 1541
       <entry><literal>iso_8859_8_to_utf_8</literal></entry>
       <entry><literal>ISO_8859_8</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1542 1543 1544
      </row>

      <row>
P
Peter Eisentraut 已提交
1545 1546 1547
       <entry><literal>iso_8859_9_to_utf_8</literal></entry>
       <entry><literal>LATIN5</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1548 1549 1550
      </row>

      <row>
P
Peter Eisentraut 已提交
1551 1552 1553
       <entry><literal>johab_to_utf_8</literal></entry>
       <entry><literal>JOHAB</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1554 1555
      </row>

1556
      <row>
P
Peter Eisentraut 已提交
1557 1558 1559
       <entry><literal>koi8r_to_iso_8859_5</literal></entry>
       <entry><literal>KOI8</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1560 1561 1562
      </row>

      <row>
P
Peter Eisentraut 已提交
1563 1564 1565
       <entry><literal>koi8r_to_mic</literal></entry>
       <entry><literal>KOI8</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1566 1567 1568
      </row>

      <row>
P
Peter Eisentraut 已提交
1569 1570 1571
       <entry><literal>koi8r_to_utf_8</literal></entry>
       <entry><literal>KOI8</literal></entry>
       <entry><literal>UNICODE</literal></entry>
1572 1573 1574
      </row>

      <row>
P
Peter Eisentraut 已提交
1575 1576 1577
       <entry><literal>koi8r_to_win1251</literal></entry>
       <entry><literal>KOI8</literal></entry>
       <entry><literal>WIN</literal></entry>
1578 1579 1580
      </row>

      <row>
P
Peter Eisentraut 已提交
1581 1582 1583
       <entry><literal>koi8r_to_win866</literal></entry>
       <entry><literal>KOI8</literal></entry>
       <entry><literal>ALT</literal></entry>
1584 1585 1586
      </row>

      <row>
P
Peter Eisentraut 已提交
1587 1588 1589
       <entry><literal>mic_to_ascii</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
1590 1591
      </row>

T
Tatsuo Ishii 已提交
1592
      <row>
P
Peter Eisentraut 已提交
1593 1594 1595
       <entry><literal>mic_to_big5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1596 1597
      </row>

1598
      <row>
P
Peter Eisentraut 已提交
1599 1600 1601
       <entry><literal>mic_to_euc_cn</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
1602 1603
      </row>

T
Tatsuo Ishii 已提交
1604
      <row>
P
Peter Eisentraut 已提交
1605 1606 1607
       <entry><literal>mic_to_euc_jp</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1608 1609
      </row>

1610
      <row>
P
Peter Eisentraut 已提交
1611 1612 1613
       <entry><literal>mic_to_euc_kr</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
1614 1615
      </row>

T
Tatsuo Ishii 已提交
1616
      <row>
P
Peter Eisentraut 已提交
1617 1618 1619
       <entry><literal>mic_to_euc_tw</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1620 1621
      </row>

1622
      <row>
P
Peter Eisentraut 已提交
1623 1624 1625
       <entry><literal>mic_to_iso_8859_1</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN1</literal></entry>
1626 1627 1628
      </row>

      <row>
P
Peter Eisentraut 已提交
1629 1630 1631
       <entry><literal>mic_to_iso_8859_2</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN2</literal></entry>
1632 1633 1634
      </row>

      <row>
P
Peter Eisentraut 已提交
1635 1636 1637
       <entry><literal>mic_to_iso_8859_3</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN3</literal></entry>
1638 1639 1640
      </row>

      <row>
P
Peter Eisentraut 已提交
1641 1642 1643
       <entry><literal>mic_to_iso_8859_4</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN4</literal></entry>
1644 1645 1646
      </row>

      <row>
P
Peter Eisentraut 已提交
1647 1648 1649
       <entry><literal>mic_to_iso_8859_5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1650 1651 1652
      </row>

      <row>
P
Peter Eisentraut 已提交
1653 1654 1655
       <entry><literal>mic_to_koi8r</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>KOI8</literal></entry>
1656 1657
      </row>

T
Tatsuo Ishii 已提交
1658
      <row>
P
Peter Eisentraut 已提交
1659 1660 1661
       <entry><literal>mic_to_sjis</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1662 1663
      </row>

1664
      <row>
P
Peter Eisentraut 已提交
1665 1666 1667
       <entry><literal>mic_to_win1250</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>WIN1250</literal></entry>
1668 1669 1670
      </row>

      <row>
P
Peter Eisentraut 已提交
1671 1672 1673
       <entry><literal>mic_to_win1251</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>WIN</literal></entry>
1674 1675 1676
      </row>

      <row>
P
Peter Eisentraut 已提交
1677 1678 1679
       <entry><literal>mic_to_win866</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>ALT</literal></entry>
1680 1681
      </row>

T
Tatsuo Ishii 已提交
1682
      <row>
P
Peter Eisentraut 已提交
1683 1684 1685
       <entry><literal>sjis_to_euc_jp</literal></entry>
       <entry><literal>SJIS</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1686 1687 1688
      </row>

      <row>
P
Peter Eisentraut 已提交
1689 1690 1691
       <entry><literal>sjis_to_mic</literal></entry>
       <entry><literal>SJIS</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1692 1693 1694
      </row>

      <row>
P
Peter Eisentraut 已提交
1695 1696 1697
       <entry><literal>sjis_to_utf_8</literal></entry>
       <entry><literal>SJIS</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1698 1699 1700
      </row>

      <row>
P
Peter Eisentraut 已提交
1701 1702 1703
       <entry><literal>tcvn_to_utf_8</literal></entry>
       <entry><literal>TCVN</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1704 1705 1706
      </row>

      <row>
P
Peter Eisentraut 已提交
1707 1708 1709
       <entry><literal>uhc_to_utf_8</literal></entry>
       <entry><literal>UHC</literal></entry>
       <entry><literal>UNICODE</literal></entry>
T
Tatsuo Ishii 已提交
1710 1711 1712
      </row>

      <row>
P
Peter Eisentraut 已提交
1713 1714 1715
       <entry><literal>utf_8_to_ascii</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
T
Tatsuo Ishii 已提交
1716 1717 1718
      </row>

      <row>
P
Peter Eisentraut 已提交
1719 1720 1721
       <entry><literal>utf_8_to_big5</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1722 1723 1724
      </row>

      <row>
P
Peter Eisentraut 已提交
1725 1726 1727
       <entry><literal>utf_8_to_euc_cn</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
T
Tatsuo Ishii 已提交
1728 1729 1730
      </row>

      <row>
P
Peter Eisentraut 已提交
1731 1732 1733
       <entry><literal>utf_8_to_euc_jp</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1734 1735 1736
      </row>

      <row>
P
Peter Eisentraut 已提交
1737 1738 1739
       <entry><literal>utf_8_to_euc_kr</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
T
Tatsuo Ishii 已提交
1740 1741 1742
      </row>

      <row>
P
Peter Eisentraut 已提交
1743 1744 1745
       <entry><literal>utf_8_to_euc_tw</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1746 1747 1748
      </row>

      <row>
P
Peter Eisentraut 已提交
1749 1750 1751
       <entry><literal>utf_8_to_gb18030</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>GB18030</literal></entry>
T
Tatsuo Ishii 已提交
1752 1753 1754
      </row>

      <row>
P
Peter Eisentraut 已提交
1755 1756 1757
       <entry><literal>utf_8_to_gbk</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>GBK</literal></entry>
T
Tatsuo Ishii 已提交
1758 1759 1760
      </row>

      <row>
P
Peter Eisentraut 已提交
1761 1762 1763
       <entry><literal>utf_8_to_iso_8859_1</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN1</literal></entry>
T
Tatsuo Ishii 已提交
1764 1765 1766
      </row>

      <row>
P
Peter Eisentraut 已提交
1767 1768 1769
       <entry><literal>utf_8_to_iso_8859_10</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN6</literal></entry>
T
Tatsuo Ishii 已提交
1770 1771 1772
      </row>

      <row>
P
Peter Eisentraut 已提交
1773 1774 1775
       <entry><literal>utf_8_to_iso_8859_13</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN7</literal></entry>
T
Tatsuo Ishii 已提交
1776 1777 1778
      </row>

      <row>
P
Peter Eisentraut 已提交
1779 1780 1781
       <entry><literal>utf_8_to_iso_8859_14</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN8</literal></entry>
T
Tatsuo Ishii 已提交
1782 1783 1784
      </row>

      <row>
P
Peter Eisentraut 已提交
1785 1786 1787
       <entry><literal>utf_8_to_iso_8859_15</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN9</literal></entry>
T
Tatsuo Ishii 已提交
1788 1789 1790
      </row>

      <row>
P
Peter Eisentraut 已提交
1791 1792 1793
       <entry><literal>utf_8_to_iso_8859_16</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN10</literal></entry>
T
Tatsuo Ishii 已提交
1794 1795 1796
      </row>

      <row>
P
Peter Eisentraut 已提交
1797 1798 1799
       <entry><literal>utf_8_to_iso_8859_2</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN2</literal></entry>
T
Tatsuo Ishii 已提交
1800 1801 1802
      </row>

      <row>
P
Peter Eisentraut 已提交
1803 1804 1805
       <entry><literal>utf_8_to_iso_8859_3</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN3</literal></entry>
T
Tatsuo Ishii 已提交
1806 1807 1808
      </row>

      <row>
P
Peter Eisentraut 已提交
1809 1810 1811
       <entry><literal>utf_8_to_iso_8859_4</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN4</literal></entry>
T
Tatsuo Ishii 已提交
1812 1813 1814
      </row>

      <row>
P
Peter Eisentraut 已提交
1815 1816 1817
       <entry><literal>utf_8_to_iso_8859_5</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
T
Tatsuo Ishii 已提交
1818 1819 1820
      </row>

      <row>
P
Peter Eisentraut 已提交
1821 1822 1823
       <entry><literal>utf_8_to_iso_8859_6</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>ISO_8859_6</literal></entry>
T
Tatsuo Ishii 已提交
1824 1825 1826
      </row>

      <row>
P
Peter Eisentraut 已提交
1827 1828 1829
       <entry><literal>utf_8_to_iso_8859_7</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>ISO_8859_7</literal></entry>
T
Tatsuo Ishii 已提交
1830 1831 1832
      </row>

      <row>
P
Peter Eisentraut 已提交
1833 1834 1835
       <entry><literal>utf_8_to_iso_8859_8</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>ISO_8859_8</literal></entry>
T
Tatsuo Ishii 已提交
1836 1837 1838
      </row>

      <row>
P
Peter Eisentraut 已提交
1839 1840 1841
       <entry><literal>utf_8_to_iso_8859_9</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>LATIN5</literal></entry>
T
Tatsuo Ishii 已提交
1842 1843 1844
      </row>

      <row>
P
Peter Eisentraut 已提交
1845 1846 1847
       <entry><literal>utf_8_to_johab</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>JOHAB</literal></entry>
T
Tatsuo Ishii 已提交
1848 1849 1850
      </row>

      <row>
P
Peter Eisentraut 已提交
1851 1852 1853
       <entry><literal>utf_8_to_koi8r</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>KOI8</literal></entry>
1854 1855 1856
      </row>

      <row>
P
Peter Eisentraut 已提交
1857 1858 1859
       <entry><literal>utf_8_to_sjis</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1860 1861 1862
      </row>

      <row>
P
Peter Eisentraut 已提交
1863 1864 1865
       <entry><literal>utf_8_to_tcvn</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>TCVN</literal></entry>
T
Tatsuo Ishii 已提交
1866 1867 1868
      </row>

      <row>
P
Peter Eisentraut 已提交
1869 1870 1871
       <entry><literal>utf_8_to_uhc</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>UHC</literal></entry>
T
Tatsuo Ishii 已提交
1872 1873
      </row>

1874
      <row>
P
Peter Eisentraut 已提交
1875 1876 1877
       <entry><literal>utf_8_to_win1250</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>WIN1250</literal></entry>
1878 1879 1880
      </row>

      <row>
P
Peter Eisentraut 已提交
1881 1882 1883
       <entry><literal>utf_8_to_win1251</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>WIN</literal></entry>
1884 1885 1886
      </row>

      <row>
P
Peter Eisentraut 已提交
1887 1888 1889
       <entry><literal>utf_8_to_win1256</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>WIN1256</literal></entry>
1890 1891 1892
      </row>

      <row>
P
Peter Eisentraut 已提交
1893 1894 1895
       <entry><literal>utf_8_to_win866</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>ALT</literal></entry>
1896 1897 1898
      </row>

      <row>
1899 1900 1901
       <entry><literal>utf_8_to_win874</literal></entry>
       <entry><literal>UNICODE</literal></entry>
       <entry><literal>WIN874</literal></entry>
1902
      </row>
1903

1904
      <row>
1905 1906 1907
       <entry><literal>win1250_to_iso_8859_2</literal></entry>
       <entry><literal>WIN1250</literal></entry>
       <entry><literal>LATIN2</literal></entry>
1908
      </row>
1909

1910
      <row>
1911 1912 1913
       <entry><literal>win1250_to_mic</literal></entry>
       <entry><literal>WIN1250</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1914 1915 1916
      </row>

      <row>
1917 1918 1919
       <entry><literal>win1250_to_utf_8</literal></entry>
       <entry><literal>WIN1250</literal></entry>
       <entry><literal>UNICODE</literal></entry>
1920 1921
      </row>

1922
      <row>
1923 1924 1925
       <entry><literal>win1251_to_iso_8859_5</literal></entry>
       <entry><literal>WIN</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1926
      </row>
1927

1928
      <row>
1929 1930 1931
       <entry><literal>win1251_to_koi8r</literal></entry>
       <entry><literal>WIN</literal></entry>
       <entry><literal>KOI8</literal></entry>
1932 1933
      </row>

1934
      <row>
1935 1936 1937
       <entry><literal>win1251_to_mic</literal></entry>
       <entry><literal>WIN</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1938
      </row>
1939

1940
      <row>
1941 1942 1943
       <entry><literal>win1251_to_utf_8</literal></entry>
       <entry><literal>WIN</literal></entry>
       <entry><literal>UNICODE</literal></entry>
1944
      </row>
1945

1946
      <row>
1947 1948 1949
       <entry><literal>win1251_to_win866</literal></entry>
       <entry><literal>WIN</literal></entry>
       <entry><literal>ALT</literal></entry>
1950 1951
      </row>

1952
      <row>
1953 1954 1955
       <entry><literal>win1256_to_utf_8</literal></entry>
       <entry><literal>WIN1256</literal></entry>
       <entry><literal>UNICODE</literal></entry>
1956
      </row>
1957

1958
      <row>
1959 1960 1961
       <entry><literal>win866_to_iso_8859_5</literal></entry>
       <entry><literal>ALT</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1962
      </row>
1963

1964
      <row>
1965 1966 1967
       <entry><literal>win866_to_koi8r</literal></entry>
       <entry><literal>ALT</literal></entry>
       <entry><literal>KOI8</literal></entry>
1968
      </row>
1969

1970
      <row>
1971 1972 1973
       <entry><literal>win866_to_mic</literal></entry>
       <entry><literal>ALT</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1974 1975
      </row>

1976
      <row>
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
       <entry><literal>win866_to_utf_8</literal></entry>
       <entry><literal>ALT</literal></entry>
       <entry><literal>UNICODE</literal></entry>
      </row>

      <row>
       <entry><literal>win866_to_win1251</literal></entry>
       <entry><literal>ALT</literal></entry>
       <entry><literal>WIN</literal></entry>
      </row>

      <row>
       <entry><literal>win874_to_utf_8</literal></entry>
       <entry><literal>WIN874</literal></entry>
       <entry><literal>UNICODE</literal></entry>
      </row>

1994 1995 1996
     </tbody>
    </tgroup>
   </table>
1997

1998
  </sect1>
1999

2000

2001 2002
  <sect1 id="functions-binarystring">
   <title>Binary String Functions and Operators</title>
2003

2004 2005 2006 2007 2008
   <para>
    This section describes functions and operators for examining and
    manipulating binary string values.  Strings in this context include
    values of the type <type>BYTEA</type>.
   </para>
2009

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
   <para>
    <acronym>SQL</acronym> defines some string functions with a
    special syntax where 
    certain keywords rather than commas are used to separate the
    arguments.  Details are in
    <xref linkend="functions-binarystring-sql">.
    Some functions are also implemented using the regular syntax for
    function invocation.
    (See <xref linkend="functions-binarystring-other">.)
   </para>
2020

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
   <table id="functions-binarystring-sql">
    <title><acronym>SQL</acronym> Binary String Functions and Operators</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>  
      </row>
     </thead>
2033

2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
     <tbody>
      <row>
       <entry> <parameter>string</parameter> <literal>||</literal>
        <parameter>string</parameter> </entry>
       <entry> <type>bytea</type> </entry>
       <entry>
        string concatenation
        <indexterm>
         <primary>binary strings</primary>
         <secondary>concatenation</secondary>
        </indexterm>
       </entry>
       <entry><literal>'\\\\Postgre'::bytea || '\\047SQL\\000'::bytea</literal></entry>
       <entry><literal>\\Postgre'SQL\000</literal></entry>
      </row>
2049

2050 2051 2052 2053 2054 2055 2056
      <row>
       <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>number of bytes in binary string</entry>
       <entry><literal>octet_length('jo\\000se'::bytea)</literal></entry>
       <entry><literal>5</literal></entry>
      </row>
2057

2058 2059 2060 2061 2062 2063 2064
      <row>
       <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>location of specified substring</entry>
      <entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
       <entry><literal>3</literal></entry>
      </row>
2065

2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
      <row>
       <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
       <entry><type>bytea</type></entry>
       <entry>
        extract substring
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
       <entry><literal>h\000o</literal></entry>
      </row>
2078

2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
      <row>
       <entry>
        <function>trim</function>(<optional>both</optional>
        <parameter>characters</parameter> from
        <parameter>string</parameter>)
       </entry>
       <entry><type>bytea</type></entry>
       <entry>
        Removes the longest string containing only the
        <parameter>characters</parameter> from the
        beginning/end/both ends of the <parameter>string</parameter>.
       </entry>
       <entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
       <entry><literal>Tom</literal></entry>
      </row>
     </tbody>
    </tgroup>
   </table>
2097

2098 2099 2100 2101 2102
   <para>
    Additional binary string manipulation functions are available and are
    listed below.  Some of them are used internally to implement the
    <acronym>SQL</acronym>-standard string functions listed above.
   </para>
2103

2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
   <table id="functions-binarystring-other">
    <title>Other Binary String Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>
2116

2117 2118 2119 2120 2121 2122 2123 2124 2125
     <tbody>
      <row>
       <entry><function>btrim</function>(<parameter>string</parameter>
        <type>bytea</type> <parameter>trim</parameter> <type>bytea</type>)</entry>
       <entry><type>bytea</type></entry>
       <entry>
        Remove (trim) the longest string consisting only of characters
        in <parameter>trim</parameter> from the start and end of
        <parameter>string</parameter>.
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
      </entry>
      <entry><literal>btrim('\\000trim\\000'::bytea,'\\000'::bytea)</literal></entry>
      <entry><literal>trim</literal></entry>
     </row>

     <row>
      <entry><function>length</function>(<parameter>string</parameter>)</entry>
      <entry><type>integer</type></entry>
      <entry>
       length of binary string
       <indexterm>
        <primary>binary strings</primary>
	<secondary>length</secondary>
       </indexterm>
       <indexterm>
        <primary>length</primary>
	<secondary>binary strings</secondary>
	<see>binary strings, length</see>
       </indexterm>
      </entry>
2146 2147
      <entry><literal>length('jo\\000se'::bytea)</literal></entry>
      <entry><literal>5</literal></entry>
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
     </row>

     <row>
      <entry>
       <function>encode</function>(<parameter>string</parameter> <type>bytea</type>,
              <parameter>type</parameter> <type>text</type>)
      </entry>
      <entry><type>text</type></entry>
      <entry>
       Encodes binary string to <acronym>ASCII</acronym>-only representation.  Supported
       types are: 'base64', 'hex', 'escape'.
      </entry>
      <entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
      <entry><literal>123\000456</literal></entry>
     </row>       

     <row>
      <entry>
       <function>decode</function>(<parameter>string</parameter> <type>text</type>,
              <parameter>type</parameter> <type>text</type>)
      </entry>
      <entry><type>bytea</type></entry>
      <entry>
       Decodes binary string from <parameter>string</parameter> previously 
       encoded with encode().  Parameter type is same as in encode().
      </entry>
      <entry><literal>decode('123\\000456', 'escape')</literal></entry>
      <entry><literal>123\000456</literal></entry>
     </row>       

    </tbody>
   </tgroup>
  </table>

 </sect1>


2185 2186 2187
 <sect1 id="functions-matching">
  <title>Pattern Matching</title>

2188
   <para>
2189
    There are three separate approaches to pattern matching provided by
2190 2191
    <productname>PostgreSQL</productname>:  the traditional
    <acronym>SQL</acronym> 
2192
    <function>LIKE</function> operator, the more recent
2193
    <acronym>SQL99</acronym> 
2194
    <function>SIMILAR TO</function> operator, and
2195 2196
    <acronym>POSIX</acronym>-style regular expressions.
    Additionally, a pattern matching function,
2197 2198
    <function>SUBSTRING</function>, is available, using either
    <acronym>SQL99</acronym>-style or POSIX-style regular expressions.
2199 2200 2201 2202 2203 2204 2205 2206
   </para>

   <tip>
    <para>
     If you have pattern matching needs that go beyond this,
     consider writing a user-defined function in Perl or Tcl.
    </para>
   </tip>
2207

2208
  <sect2 id="functions-like">
2209
   <title><function>LIKE</function></title>
2210

2211 2212 2213 2214
   <indexterm>
    <primary>like</primary>
   </indexterm>

2215 2216 2217 2218
    <synopsis>
<replaceable>string</replaceable> LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
<replaceable>string</replaceable> NOT LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
    </synopsis>
2219

2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
    <para>
     Every <replaceable>pattern</replaceable> defines a set of strings.
     The <function>LIKE</function> expression returns true if the
     <replaceable>string</replaceable> is contained in the set of
     strings represented by <replaceable>pattern</replaceable>.  (As
     expected, the <function>NOT LIKE</function> expression returns
     false if <function>LIKE</function> returns true, and vice versa.
     An equivalent expression is
     <literal>NOT (<replaceable>string</replaceable> LIKE
      <replaceable>pattern</replaceable>)</literal>.)
    </para>
2231 2232

    <para>
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
     If <replaceable>pattern</replaceable> does not contain percent
     signs or underscore, then the pattern only represents the string
     itself; in that case <function>LIKE</function> acts like the
     equals operator.  An underscore (<literal>_</literal>) in
     <replaceable>pattern</replaceable> stands for (matches) any single
     character; a percent sign (<literal>%</literal>) matches any string
     of zero or more characters.
    </para>

    <informalexample>
     <para>
      Some examples:
      <programlisting>
2246 2247 2248 2249
'abc' LIKE 'abc'    <lineannotation>true</lineannotation>
'abc' LIKE 'a%'     <lineannotation>true</lineannotation>
'abc' LIKE '_b_'    <lineannotation>true</lineannotation>
'abc' LIKE 'c'      <lineannotation>false</lineannotation>
2250 2251 2252
      </programlisting>
     </para>
    </informalexample>
2253

T
Thomas G. Lockhart 已提交
2254
   <para>
2255
    <function>LIKE</function> pattern matches always cover the entire
2256
    string.  To match a pattern anywhere within a string, the
2257
    pattern must therefore start and end with a percent sign.
T
Thomas G. Lockhart 已提交
2258
   </para>
2259 2260

   <para>
2261 2262 2263 2264
    To match a literal underscore or percent sign without matching
    other characters, the respective character in
    <replaceable>pattern</replaceable> must be 
    preceded by the escape character.  The default escape
2265
    character is the backslash but a different one may be selected by
2266 2267 2268 2269 2270 2271 2272
    using the <literal>ESCAPE</literal> clause.  To match the escape
    character itself, write two escape characters.
   </para>

   <para>
    Note that the backslash already has a special meaning in string
    literals, so to write a pattern constant that contains a backslash
2273 2274 2275
    you must write two backslashes in the query.  Thus, writing a pattern
    that actually matches a literal backslash means writing four backslashes
    in the query.  You can avoid this by selecting a different escape
P
Peter Eisentraut 已提交
2276
    character with <literal>ESCAPE</literal>; then backslash is not special
2277
    to <function>LIKE</function> anymore. (But it is still special to the string
2278 2279 2280 2281 2282
    literal parser, so you still need two of them.)
   </para>

   <para>
    It's also possible to select no escape character by writing
2283
    <literal>ESCAPE ''</literal>.  This effectively disables the
B
Bruce Momjian 已提交
2284 2285
    escape mechanism, which makes it impossible to turn off the
    special meaning of underscore and percent signs in the pattern.
2286
   </para>
2287 2288

   <para>
2289 2290
    The keyword <token>ILIKE</token> can be used instead of
    <token>LIKE</token> to make the match case insensitive according
2291
    to the active locale.  This is not in the <acronym>SQL</acronym> standard but is a
2292
    <productname>PostgreSQL</productname> extension.
T
Thomas G. Lockhart 已提交
2293
   </para>
2294

2295 2296
   <para>
    The operator <literal>~~</literal> is equivalent to
2297 2298 2299
    <function>LIKE</function>, and <literal>~~*</literal> corresponds to
    <function>ILIKE</function>.  There are also
    <literal>!~~</literal> and <literal>!~~*</literal> operators that
2300
    represent <function>NOT LIKE</function> and <function>NOT
2301
    ILIKE</function>.  All of these operators are
2302
    <productname>PostgreSQL</productname>-specific.
2303 2304
   </para>
  </sect2>
2305

2306

2307 2308
  <sect2 id="functions-sql99-regexp">
   <title><function>SIMILAR TO</function> and <acronym>SQL99</acronym>
2309
     Regular Expressions</title>
2310

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
   <indexterm zone="functions-sql99-regexp">
    <primary>regular expressions</primary>
    <seealso>pattern matching</seealso>
   </indexterm>

   <indexterm>
    <primary>similar to</primary>
   </indexterm>

   <indexterm>
    <primary>substring</primary>
   </indexterm>

    <synopsis>
<replaceable>string</replaceable> SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
<replaceable>string</replaceable> NOT SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
    </synopsis>

    <para>
     The <function>SIMILAR TO</function> operator returns true or false
     depending on whether its pattern matches the given string.  It is
     much like <function>LIKE</function>, except that it interprets the
     pattern using <acronym>SQL99</acronym>'s definition of a regular
     expression.
     <acronym>SQL99</acronym>'s regular expressions are a curious cross
     between <function>LIKE</function> notation and common regular expression
     notation.
    </para>

    <para>
     Like <function>LIKE</function>, the  <function>SIMILAR TO</function>
     operator succeeds only if its pattern matches the entire string;
     this is unlike common regular expression practice, wherein the pattern
     may match any part of the string.
     Also like
     <function>LIKE</function>, <function>SIMILAR TO</function> uses
     <literal>%</> and <literal>_</> as wildcard characters denoting
     any string and any single character, respectively (these are
     comparable to <literal>.*</> and <literal>.</> in POSIX regular
     expressions).
    </para>

    <para>
     In addition to these facilities borrowed from <function>LIKE</function>,
     <function>SIMILAR TO</function> supports these pattern-matching
     metacharacters borrowed from POSIX regular expressions:

    <itemizedlist>
     <listitem>
      <para>
       <literal>|</literal> denotes alternation (either of two alternatives).
      </para>
     </listitem>
     <listitem>
      <para>
       <literal>*</literal> denotes repetition of the previous item zero
       or more times.
      </para>
     </listitem>
     <listitem>
      <para>
       <literal>+</literal> denotes repetition of the previous item one
       or more times.
      </para>
     </listitem>
     <listitem>
      <para>
       Parentheses <literal>()</literal> may be used to group items into
       a single logical item.
      </para>
     </listitem>
     <listitem>
      <para>
       A bracket expression <literal>[...]</literal> specifies a character
       class, just as in POSIX regular expressions.
      </para>
     </listitem>
    </itemizedlist>

     Notice that bounded repetition (<literal>?</> and <literal>{...}</>)
     are not provided, though they exist in POSIX.  Also, dot (<literal>.</>)
     is not a metacharacter.
    </para>

    <para>
     As with <function>LIKE</>, a backslash disables the special meaning
     of any of these metacharacters; or a different escape character can
     be specified with <literal>ESCAPE</>.
    </para>

    <informalexample>
     <para>
      Some examples:
      <programlisting>
'abc' SIMILAR TO 'abc'      <lineannotation>true</lineannotation>
'abc' SIMILAR TO 'a'        <lineannotation>false</lineannotation>
'abc' SIMILAR TO '%(b|d)%'  <lineannotation>true</lineannotation>
'abc' SIMILAR TO '(b|c)%'   <lineannotation>false</lineannotation>
      </programlisting>
     </para>
    </informalexample>

    <para>
     The SUBSTRING function with three parameters,
     <function>SUBSTRING</function>(<parameter>string</parameter> FROM
     <replaceable>pattern</replaceable> FOR
     <replaceable>escape</replaceable>), provides extraction of a substring
     that matches a SQL99 regular expression pattern.  As with SIMILAR TO,
     the specified pattern must match to the entire data string, else the
     function fails and returns NULL.  To indicate the part of the pattern
     that should be returned on success, SQL99 specifies that the pattern
     must contain two occurrences of the escape character followed by
     double quote (<literal>"</>).  The text matching the portion of the
     pattern between these markers is returned.
    </para>

    <informalexample>
     <para>
      Some examples:
      <programlisting>
SUBSTRING('foobar' FROM '%#"o_b#"%' FOR '#')   <lineannotation>oob</lineannotation>
SUBSTRING('foobar' FROM '#"o_b#"%' FOR '#')    <lineannotation>NULL</lineannotation>
      </programlisting>
     </para>
    </informalexample>

  </sect2>

  <sect2 id="functions-posix-regexp">
   <title><acronym>POSIX</acronym> Regular Expressions</title>

   <indexterm zone="functions-posix-regexp">
2443 2444 2445 2446
    <primary>regular expressions</primary>
    <seealso>pattern matching</seealso>
   </indexterm>

2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
   <table>
    <title>Regular Expression Match Operators</title>

    <tgroup cols="3">
     <thead>
      <row>
       <entry>Operator</entry>
       <entry>Description</entry>
       <entry>Example</entry>
      </row>
     </thead>

2459 2460 2461 2462 2463 2464
      <tbody>
       <row>
        <entry> <literal>~</literal> </entry>
        <entry>Matches regular expression, case sensitive</entry>
        <entry><literal>'thomas' ~ '.*thomas.*'</literal></entry>
       </row>
2465

2466 2467 2468 2469 2470
       <row>
        <entry> <literal>~*</literal> </entry>
        <entry>Matches regular expression, case insensitive</entry>
        <entry><literal>'thomas' ~* '.*Thomas.*'</literal></entry>
       </row>
2471

2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
       <row>
        <entry> <literal>!~</literal> </entry>
        <entry>Does not match regular expression, case sensitive</entry>
        <entry><literal>'thomas' !~ '.*Thomas.*'</literal></entry>
       </row>

       <row>
        <entry> <literal>!~*</literal> </entry>
        <entry>Does not match regular expression, case insensitive</entry>
        <entry><literal>'thomas' !~* '.*vadim.*'</literal></entry>
       </row>
      </tbody>
     </tgroup>
    </table>

    <para>
     <acronym>POSIX</acronym> regular expressions provide a more
     powerful means for 
2490 2491
     pattern matching than the <function>LIKE</function> and
     <function>SIMILAR TO</> operators.
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
     Many Unix tools such as <command>egrep</command>,
     <command>sed</command>, or <command>awk</command> use a pattern
     matching language that is similar to the one described here.
    </para>

    <para>
     A regular expression is a character sequence that is an
     abbreviated definition of a set of strings (a <firstterm>regular
      set</firstterm>).  A string is said to match a regular expression
     if it is a member of the regular set described by the regular
     expression.  As with <function>LIKE</function>, pattern characters
     match string characters exactly unless they are special characters
     in the regular expression language --- but regular expressions use
     different special characters than <function>LIKE</function> does.
     Unlike <function>LIKE</function> patterns, a
     regular expression is allowed to match anywhere within a string, unless
     the regular expression is explicitly anchored to the beginning or
     end of the string.
    </para>
2511

2512 2513 2514 2515
    <informalexample>
     <para>
      Some examples:
      <programlisting>
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
'abc' ~ 'abc'    <lineannotation>true</lineannotation>
'abc' ~ '^a'     <lineannotation>true</lineannotation>
'abc' ~ '(b|d)'  <lineannotation>true</lineannotation>
'abc' ~ '^(b|c)' <lineannotation>false</lineannotation>
      </programlisting>
     </para>
    </informalexample>

    <para>
     The SUBSTRING function with two parameters,
     <function>SUBSTRING</function>(<parameter>string</parameter> FROM
     <replaceable>pattern</replaceable>), provides extraction of a substring
     that matches a POSIX regular expression pattern.  It returns NULL if
     there is no match, otherwise the portion of the text that matched the
     pattern.  But if the pattern contains any parentheses, the portion
     of the text that matched the first parenthesized subexpression (the
     one whose left parenthesis comes first) is
     returned.  You can always put parentheses around the whole expression
     if you want to use parentheses within it without triggering this
     exception.
    </para>

    <informalexample>
     <para>
      Some examples:
      <programlisting>
SUBSTRING('foobar' FROM 'o.b')     <lineannotation>oob</lineannotation>
SUBSTRING('foobar' FROM 'o(.)b')   <lineannotation>o</lineannotation>
2544 2545 2546
      </programlisting>
     </para>
    </informalexample>
2547 2548

<!-- derived from the re_format.7 man page -->
T
Thomas G. Lockhart 已提交
2549
   <para>
P
Peter Eisentraut 已提交
2550
    Regular expressions (<acronym>RE</acronym>s), as defined in
2551
     <acronym>POSIX</acronym> 
P
Peter Eisentraut 已提交
2552
    1003.2, come in two forms: modern <acronym>RE</acronym>s (roughly those of
2553
    <command>egrep</command>; 1003.2 calls these
P
Peter Eisentraut 已提交
2554 2555
    <quote>extended</quote> <acronym>RE</acronym>s) and obsolete <acronym>RE</acronym>s (roughly those of
    <command>ed</command>; 1003.2 <quote>basic</quote> <acronym>RE</acronym>s).
2556
    <productname>PostgreSQL</productname> implements the modern form.
T
Thomas G. Lockhart 已提交
2557
   </para>
2558

T
Thomas G. Lockhart 已提交
2559
   <para>
2560 2561 2562 2563
    A (modern) RE is one or more non-empty
    <firstterm>branches</firstterm>, separated by
    <literal>|</literal>.  It matches anything that matches one of the
    branches.
T
Thomas G. Lockhart 已提交
2564
   </para>
2565

2566
   <para>
2567 2568 2569
    A branch is one or more <firstterm>pieces</firstterm>,
    concatenated.  It matches a match for the first, followed by a
    match for the second, etc.
2570
   </para>
2571

B
Bruce Momjian 已提交
2572
   <para>
2573 2574 2575 2576 2577 2578 2579 2580
    A piece is an <firstterm>atom</firstterm> possibly followed by a
    single <literal>*</literal>, <literal>+</literal>,
    <literal>?</literal>, or <firstterm>bound</firstterm>.  An atom
    followed by <literal>*</literal> matches a sequence of 0 or more
    matches of the atom.  An atom followed by <literal>+</literal>
    matches a sequence of 1 or more matches of the atom.  An atom
    followed by <literal>?</literal> matches a sequence of 0 or 1
    matches of the atom.
B
Bruce Momjian 已提交
2581
   </para>
2582

2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
   <para>
    A <firstterm>bound</firstterm> is <literal>{</literal> followed by
    an unsigned decimal integer, possibly followed by
    <literal>,</literal> possibly followed by another unsigned decimal
    integer, always followed by <literal>}</literal>.  The integers
    must lie between 0 and <symbol>RE_DUP_MAX</symbol> (255)
    inclusive, and if there are two of them, the first may not exceed
    the second.  An atom followed by a bound containing one integer
    <replaceable>i</replaceable> and no comma matches a sequence of
    exactly <replaceable>i</replaceable> matches of the atom.  An atom
    followed by a bound containing one integer
    <replaceable>i</replaceable> and a comma matches a sequence of
    <replaceable>i</replaceable> or more matches of the atom.  An atom
    followed by a bound containing two integers
    <replaceable>i</replaceable> and <replaceable>j</replaceable>
    matches a sequence of <replaceable>i</replaceable> through
    <replaceable>j</replaceable> (inclusive) matches of the atom.
   </para>

   <note>
    <para>
     A repetition operator (<literal>?</literal>,
     <literal>*</literal>, <literal>+</literal>, or bounds) cannot
     follow another repetition operator.  A repetition operator cannot
     begin an expression or subexpression or follow
     <literal>^</literal> or <literal>|</literal>.
    </para>
   </note>
2611

2612
   <para>
2613 2614 2615 2616 2617
    An <firstterm>atom</firstterm> is a regular expression enclosed in
    <literal>()</literal> (matching a match for the regular
    expression), an empty set of <literal>()</literal> (matching the
    null string), a <firstterm>bracket expression</firstterm> (see
    below), <literal>.</literal> (matching any single character),
2618 2619 2620
    <literal>^</literal> (matching the null string at the beginning of the
    input string), <literal>$</literal> (matching the null string at the end
    of the input string), a <literal>\</literal> followed by one of the
2621 2622 2623 2624 2625 2626 2627 2628 2629
    characters <literal>^.[$()|*+?{\</literal> (matching that
    character taken as an ordinary character), a <literal>\</literal>
    followed by any other character (matching that character taken as
    an ordinary character, as if the <literal>\</literal> had not been
    present), or a single character with no other significance
    (matching that character).  A <literal>{</literal> followed by a
    character other than a digit is an ordinary character, not the
    beginning of a bound.  It is illegal to end an RE with
    <literal>\</literal>.
2630
   </para>
2631

2632 2633 2634 2635 2636 2637 2638
   <para>
    Note that the backslash (<literal>\</literal>) already has a special
    meaning in string
    literals, so to write a pattern constant that contains a backslash
    you must write two backslashes in the query.
   </para>

T
Thomas G. Lockhart 已提交
2639
   <para>
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
    A <firstterm>bracket expression</firstterm> is a list of
    characters enclosed in <literal>[]</literal>.  It normally matches
    any single character from the list (but see below).  If the list
    begins with <literal>^</literal>, it matches any single character
    (but see below) not from the rest of the list.  If two characters
    in the list are separated by <literal>-</literal>, this is
    shorthand for the full range of characters between those two
    (inclusive) in the collating sequence,
    e.g. <literal>[0-9]</literal> in <acronym>ASCII</acronym> matches
    any decimal digit.  It is illegal for two ranges to share an
    endpoint, e.g.  <literal>a-c-e</literal>.  Ranges are very
    collating-sequence-dependent, and portable programs should avoid
    relying on them.
T
Thomas G. Lockhart 已提交
2653
   </para>
2654

T
Thomas G. Lockhart 已提交
2655
   <para>
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
    To include a literal <literal>]</literal> in the list, make it the
    first character (following a possible <literal>^</literal>).  To
    include a literal <literal>-</literal>, make it the first or last
    character, or the second endpoint of a range.  To use a literal
    <literal>-</literal> as the first endpoint of a range, enclose it
    in <literal>[.</literal> and <literal>.]</literal> to make it a
    collating element (see below).  With the exception of these and
    some combinations using <literal>[</literal> (see next
    paragraphs), all other special characters, including
    <literal>\</literal>, lose their special significance within a
    bracket expression.
   </para>

   <para>
    Within a bracket expression, a collating element (a character, a
2671
    multiple-character sequence that collates as if it were a single
2672 2673 2674 2675
    character, or a collating-sequence name for either) enclosed in
    <literal>[.</literal> and <literal>.]</literal> stands for the
    sequence of characters of that collating element.  The sequence is
    a single element of the bracket expression's list.  A bracket
2676
    expression containing a multiple-character collating element can thus
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
    match more than one character, e.g. if the collating sequence
    includes a <literal>ch</literal> collating element, then the RE
    <literal>[[.ch.]]*c</literal> matches the first five characters of
    <literal>chchcc</literal>.
   </para>

   <para>
    Within a bracket expression, a collating element enclosed in
    <literal>[=</literal> and <literal>=]</literal> is an equivalence
    class, standing for the sequences of characters of all collating
    elements equivalent to that one, including itself.  (If there are
    no other equivalent collating elements, the treatment is as if the
    enclosing delimiters were <literal>[.</literal> and
    <literal>.]</literal>.)  For example, if <literal>o</literal> and
    <literal>^</literal> are the members of an equivalence class, then
    <literal>[[=o=]]</literal>, <literal>[[=^=]]</literal>, and
    <literal>[o^]</literal> are all synonymous.  An equivalence class
    may not be an endpoint of a range.
   </para>

   <para>
    Within a bracket expression, the name of a character class
    enclosed in <literal>[:</literal> and <literal>:]</literal> stands
    for the list of all characters belonging to that class.  Standard
    character class names are: <literal>alnum</literal>,
    <literal>alpha</literal>, <literal>blank</literal>,
    <literal>cntrl</literal>, <literal>digit</literal>,
    <literal>graph</literal>, <literal>lower</literal>,
    <literal>print</literal>, <literal>punct</literal>,
    <literal>space</literal>, <literal>upper</literal>,
    <literal>xdigit</literal>.  These stand for the character classes
    defined in
    <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
    A locale may provide others.  A character class may not be used as
    an endpoint of a range.
   </para>

   <para>
    There are two special cases of bracket expressions:  the bracket
    expressions <literal>[[:<:]]</literal> and
    <literal>[[:>:]]</literal> match the null string at the beginning
    and end of a word respectively.  A word is defined as a sequence
    of word characters which is neither preceded nor followed by word
    characters.  A word character is an alnum character (as defined by
    <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
    or an underscore.  This is an extension, compatible with but not
2723
    specified by <acronym>POSIX</acronym> 1003.2, and should be used with caution in
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
    software intended to be portable to other systems.
   </para>

   <para>
    In the event that an RE could match more than one substring of a
    given string, the RE matches the one starting earliest in the
    string.  If the RE could match more than one substring starting at
    that point, it matches the longest.  Subexpressions also match the
    longest possible substrings, subject to the constraint that the
    whole match be as long as possible, with subexpressions starting
    earlier in the RE taking priority over ones starting later.  Note
    that higher-level subexpressions thus take priority over their
    lower-level component subexpressions.
   </para>

   <para>
    Match lengths are measured in characters, not collating
    elements.  A null string is considered longer than no match at
    all.  For example, <literal>bb*</literal> matches the three middle
    characters of <literal>abbbc</literal>,
    <literal>(wee|week)(knights|nights)</literal> matches all ten
    characters of <literal>weeknights</literal>, when
    <literal>(.*).*</literal> is matched against
    <literal>abc</literal> the parenthesized subexpression matches all
    three characters, and when <literal>(a*)*</literal> is matched
    against <literal>bc</literal> both the whole RE and the
    parenthesized subexpression match the null string.
   </para>

   <para>
    If case-independent matching is specified, the effect is much as
    if all case distinctions had vanished from the alphabet.  When an
    alphabetic that exists in multiple cases appears as an ordinary
    character outside a bracket expression, it is effectively
    transformed into a bracket expression containing both cases,
    e.g. <literal>x</literal> becomes <literal>[xX]</literal>.  When
    it appears inside a bracket expression, all case counterparts of
    it are added to the bracket expression, so that (e.g.)
    <literal>[x]</literal> becomes <literal>[xX]</literal> and
    <literal>[^x]</literal> becomes <literal>[^xX]</literal>.
   </para>

   <para>
P
Peter Eisentraut 已提交
2767
    There is no particular limit on the length of <acronym>RE</acronym>s, except insofar
2768 2769 2770 2771 2772 2773 2774
    as memory is limited.  Memory usage is approximately linear in RE
    size, and largely insensitive to RE complexity, except for bounded
    repetitions.  Bounded repetitions are implemented by macro
    expansion, which is costly in time and space if counts are large
    or bounded repetitions are nested.  An RE like, say,
    <literal>((((a{1,100}){1,100}){1,100}){1,100}){1,100}</literal>
    will (eventually) run almost any existing machine out of swap
2775 2776 2777 2778 2779 2780 2781 2782
    space.
    <footnote>
     <para>
      This was written in 1994, mind you.  The
      numbers have probably changed, but the problem
      persists.
     </para>
    </footnote>
T
Thomas G. Lockhart 已提交
2783
   </para>
2784 2785
<!-- end re_format.7 man page -->
  </sect2>
2786

2787 2788 2789 2790
 </sect1>


  <sect1 id="functions-formatting">
2791
   <title>Data Type Formatting Functions</title>
2792

2793 2794 2795 2796
   <indexterm zone="functions-formatting">
    <primary>formatting</primary>
   </indexterm>

2797 2798 2799
   <note>
    <title>Author</title>
    <para>
2800
     Written by Karel Zak (<email>zakkr@zf.jcu.cz</email>) on 2000-01-24
2801 2802
    </para>
   </note>
2803

2804
   <para>
2805
    The <productname>PostgreSQL</productname> formatting functions
2806 2807
    provide a powerful set of tools for converting various data types
    (date/time, integer, floating point, numeric) to formatted strings
2808
    and for converting from formatted strings to specific data types.
2809
    These functions all follow a common calling convention: the first
2810
    argument is the value to be formatted and the second argument is a
2811
    template that defines the output or input format.
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
   </para>

    <table tocentry="1">
     <title>Formatting Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
2827 2828 2829 2830
	<entry><function>to_char</function>(<type>timestamp</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
	<entry>convert time stamp to string</entry>
	<entry><literal>to_char(timestamp 'now','HH12:MI:SS')</literal></entry>
2831
       </row>
B
Bruce Momjian 已提交
2832
       <row>
2833 2834
	<entry><function>to_char</function>(<type>interval</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
B
Bruce Momjian 已提交
2835
	<entry>convert interval to string</entry>
2836
	<entry><literal>to_char(interval '15h 2m 12s','HH24:MI:SS')</literal></entry>
B
Bruce Momjian 已提交
2837
       </row>
2838
       <row>
2839 2840
	<entry><function>to_char</function>(<type>int</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
2841
	<entry>convert int4/int8 to string</entry>
2842
	<entry><literal>to_char(125, '999')</literal></entry>
2843 2844
       </row>
       <row>
2845 2846
	<entry><function>to_char</function>(<type>double precision</type>,
        <type>text</type>)</entry>
2847
	<entry><type>text</type></entry>
2848
	<entry>convert real/double precision to string</entry>
2849
	<entry><literal>to_char(125.8, '999D9')</literal></entry>
2850 2851
       </row>
       <row>
2852 2853
	<entry><function>to_char</function>(<type>numeric</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
2854
	<entry>convert numeric to string</entry>
2855
	<entry><literal>to_char(numeric '-125.8', '999D99S')</literal></entry>
2856 2857
       </row>
       <row>
2858 2859
	<entry><function>to_date</function>(<type>text</type>, <type>text</type>)</entry>
	<entry><type>date</type></entry>
2860
	<entry>convert string to date</entry>
2861
	<entry><literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
2862 2863
       </row>
       <row>
2864 2865 2866 2867
	<entry><function>to_timestamp</function>(<type>text</type>, <type>text</type>)</entry>
	<entry><type>timestamp</type></entry>
	<entry>convert string to time stamp</entry>
	<entry><literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
2868 2869
       </row>
       <row>
2870 2871
	<entry><function>to_number</function>(<type>text</type>, <type>text</type>)</entry>
	<entry><type>numeric</type></entry>
2872
	<entry>convert string to numeric</entry>
2873
	<entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
2874 2875 2876 2877 2878
       </row>
      </tbody>
     </tgroup>
    </table>

2879 2880 2881 2882
   <para>
    In an output template string, there are certain patterns that are
    recognized and replaced with appropriately-formatted data from the value
    to be formatted.  Any text that is not a template pattern is simply
P
Peter Eisentraut 已提交
2883
    copied verbatim.  Similarly, in an input template string, template patterns
2884 2885 2886 2887
    identify the parts of the input data string to be looked at and the
    values to be found there.
   </para>

2888
    <table tocentry="1">
2889
     <title>Template patterns for date/time conversions</title>
2890 2891 2892
     <tgroup cols="2">
      <thead>
       <row>
2893
	<entry>Pattern</entry>
2894 2895 2896 2897 2898
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
2899
	<entry><literal>HH</literal></entry>
2900
	<entry>hour of day (01-12)</entry>
2901 2902
       </row>
       <row>
2903
	<entry><literal>HH12</literal></entry>
2904
	<entry>hour of day (01-12)</entry>
2905
       </row>       
2906
       <row>
2907
	<entry><literal>HH24</literal></entry>
2908 2909
	<entry>hour of day (00-23)</entry>
       </row>       
2910
       <row>
2911
	<entry><literal>MI</literal></entry>
2912
	<entry>minute (00-59)</entry>
2913 2914
       </row>   
       <row>
2915
	<entry><literal>SS</literal></entry>
2916
	<entry>second (00-59)</entry>
2917
       </row>
B
Bruce Momjian 已提交
2918
       <row>
2919
	<entry><literal>MS</literal></entry>
B
Bruce Momjian 已提交
2920 2921 2922
	<entry>millisecond (000-999)</entry>
       </row>
       <row>
2923
	<entry><literal>US</literal></entry>
B
Bruce Momjian 已提交
2924 2925
	<entry>microsecond (000000-999999)</entry>
       </row>
2926
       <row>
2927
	<entry><literal>SSSS</literal></entry>
2928
	<entry>seconds past midnight (0-86399)</entry>
2929
       </row>
B
Bruce Momjian 已提交
2930
       <row>
2931
	<entry><literal>AM</literal> or <literal>A.M.</literal> or
2932
	<literal>PM</literal> or <literal>P.M.</literal></entry>
B
Bruce Momjian 已提交
2933 2934 2935
	<entry>meridian indicator (upper case)</entry>
       </row>
       <row>
2936
	<entry><literal>am</literal> or <literal>a.m.</literal> or
2937
	<literal>pm</literal> or <literal>p.m.</literal></entry>
B
Bruce Momjian 已提交
2938 2939
	<entry>meridian indicator (lower case)</entry>
       </row>
2940
       <row>
2941
	<entry><literal>Y,YYY</literal></entry>
2942
	<entry>year (4 and more digits) with comma</entry>
2943 2944
       </row>
       <row>
2945
	<entry><literal>YYYY</literal></entry>
2946
	<entry>year (4 and more digits)</entry>
2947 2948
       </row>
       <row>
2949
	<entry><literal>YYY</literal></entry>
2950
	<entry>last 3 digits of year</entry>
2951 2952
       </row>
       <row>
2953
	<entry><literal>YY</literal></entry>
2954
	<entry>last 2 digits of year</entry>
2955 2956
       </row>
       <row>
2957
	<entry><literal>Y</literal></entry>
2958
	<entry>last digit of year</entry>
2959
       </row>
B
Bruce Momjian 已提交
2960
       <row>
2961
	<entry><literal>BC</literal> or <literal>B.C.</literal> or
2962
	<literal>AD</literal> or <literal>A.D.</literal></entry>
2963
	<entry>era indicator (upper case)</entry>
B
Bruce Momjian 已提交
2964 2965
       </row>
       <row>
2966
	<entry><literal>bc</literal> or <literal>b.c.</literal> or
2967
	<literal>ad</literal> or <literal>a.d.</literal></entry>
2968
	<entry>era indicator (lower case)</entry>
B
Bruce Momjian 已提交
2969
       </row>
2970
       <row>
2971
	<entry><literal>MONTH</literal></entry>
2972
	<entry>full upper case month name (blank-padded to 9 chars)</entry>
2973 2974
       </row>
       <row>
2975
	<entry><literal>Month</literal></entry>
2976
	<entry>full mixed case month name (blank-padded to 9 chars)</entry>
2977 2978
       </row>
       <row>
2979
	<entry><literal>month</literal></entry>
2980
	<entry>full lower case month name (blank-padded to 9 chars)</entry>
2981 2982
       </row>
       <row>
2983
	<entry><literal>MON</literal></entry>
2984
	<entry>abbreviated upper case month name (3 chars)</entry>
2985 2986
       </row>
       <row>
2987
	<entry><literal>Mon</literal></entry>
2988
	<entry>abbreviated mixed case month name (3 chars)</entry>
2989 2990
       </row>
       <row>
2991
	<entry><literal>mon</literal></entry>
2992
	<entry>abbreviated lower case month name (3 chars)</entry>
2993 2994
       </row>
       <row>
2995
	<entry><literal>MM</literal></entry>
2996
	<entry>month number (01-12)</entry>
2997 2998
       </row>
       <row>
2999
	<entry><literal>DAY</literal></entry>
3000
	<entry>full upper case day name (blank-padded to 9 chars)</entry>
3001 3002
       </row>
       <row>
3003
	<entry><literal>Day</literal></entry>
3004
	<entry>full mixed case day name (blank-padded to 9 chars)</entry>
3005 3006
       </row>
       <row>
3007
	<entry><literal>day</literal></entry>
3008
	<entry>full lower case day name (blank-padded to 9 chars)</entry>
3009 3010
       </row>
       <row>
3011
	<entry><literal>DY</literal></entry>
3012
	<entry>abbreviated upper case day name (3 chars)</entry>
3013 3014
       </row>
       <row>
3015
	<entry><literal>Dy</literal></entry>
3016
	<entry>abbreviated mixed case day name (3 chars)</entry>
3017 3018
       </row>
       <row>
3019
	<entry><literal>dy</literal></entry>
3020
	<entry>abbreviated lower case day name (3 chars)</entry>
3021 3022
       </row>
       <row>
3023
	<entry><literal>DDD</literal></entry>
3024
	<entry>day of year (001-366)</entry>
3025 3026
       </row>
       <row>
3027
	<entry><literal>DD</literal></entry>
3028
	<entry>day of month (01-31)</entry>
3029 3030
       </row>
       <row>
3031
	<entry><literal>D</literal></entry>
3032
	<entry>day of week (1-7; SUN=1)</entry>
3033 3034
       </row>
       <row>
3035
	<entry><literal>W</literal></entry>
3036
	<entry>week of month (1-5) where first week start on the first day of the month</entry>
3037 3038
       </row> 
       <row>
3039
	<entry><literal>WW</literal></entry>
3040
	<entry>week number of year (1-53) where first week start on the first day of the year</entry>
3041
       </row>
B
Bruce Momjian 已提交
3042
       <row>
3043
	<entry><literal>IW</literal></entry>
3044
	<entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
B
Bruce Momjian 已提交
3045
       </row>
3046
       <row>
3047
	<entry><literal>CC</literal></entry>
3048
	<entry>century (2 digits)</entry>
3049 3050
       </row>
       <row>
3051
	<entry><literal>J</literal></entry>
3052
	<entry>Julian Day (days since January 1, 4712 BC)</entry>
3053 3054
       </row>
       <row>
3055
	<entry><literal>Q</literal></entry>
3056
	<entry>quarter</entry>
3057 3058
       </row>
       <row>
3059
	<entry><literal>RM</literal></entry>
3060
	<entry>month in Roman Numerals (I-XII; I=January) - upper case</entry>
B
Bruce Momjian 已提交
3061 3062
       </row>
       <row>
3063
	<entry><literal>rm</literal></entry>
3064
	<entry>month in Roman Numerals (I-XII; I=January) - lower case</entry>
3065
       </row>
3066
       <row>
3067
	<entry><literal>TZ</literal></entry>
3068
	<entry>timezone name - upper case</entry>
3069 3070
       </row>
       <row>
3071
	<entry><literal>tz</literal></entry>
3072
	<entry>timezone name - lower case</entry>
3073
       </row>
3074 3075 3076 3077 3078
      </tbody>
     </tgroup>
    </table>

   <para>
3079 3080 3081 3082
    Certain modifiers may be applied to any template pattern to alter its
    behavior.  For example, <quote><literal>FMMonth</literal></quote>
    is the <quote><literal>Month</literal></quote> pattern with the
    <quote><literal>FM</literal></quote> prefix.
3083 3084 3085
   </para>

    <table tocentry="1">
3086
     <title>Template pattern modifiers for date/time conversions</title>
3087 3088 3089
     <tgroup cols="3">
      <thead>
       <row>
3090
	<entry>Modifier</entry>
3091 3092 3093 3094 3095 3096
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
3097 3098
	<entry><literal>FM</literal> prefix</entry>
	<entry>fill mode (suppress padding blanks and zeroes)</entry>
3099
	<entry><literal>FMMonth</literal></entry>
3100 3101
       </row>
       <row>
3102 3103
	<entry><literal>TH</literal> suffix</entry>
	<entry>add upper-case ordinal number suffix</entry>
3104
	<entry><literal>DDTH</literal></entry>
3105 3106
       </row>	
       <row>
3107 3108
	<entry><literal>th</literal> suffix</entry>
	<entry>add lower-case ordinal number suffix</entry>
3109
	<entry><literal>DDth</literal></entry>
3110 3111
       </row>
       <row>
3112
	<entry><literal>FX</literal> prefix</entry>
3113 3114
	<entry>Fixed format global option (see below)</entry>
	<entry><literal>FX Month DD Day</literal></entry>
3115 3116
       </row>	
       <row>
3117
	<entry><literal>SP</literal> suffix</entry>
3118
	<entry>spell mode (not yet implemented)</entry>
3119
	<entry><literal>DDSP</literal></entry>
3120 3121 3122 3123 3124 3125
       </row>       
      </tbody>
     </tgroup>
    </table>

   <para>
3126 3127 3128
    Usage notes:

    <itemizedlist>
3129 3130 3131 3132 3133 3134 3135 3136
     <listitem>
      <para>
       <literal>FM</literal> suppresses leading zeroes or trailing blanks
       that would otherwise be added to make the output of a pattern be
       fixed-width.
      </para>
     </listitem>

3137 3138 3139
     <listitem>
      <para>
       <function>to_timestamp</function> and <function>to_date</function>
3140
       skip multiple blank spaces in the input string if the <literal>FX</literal> option 
3141 3142 3143
       is not used. <literal>FX</literal> must be specified as the first item
       in the template; for example 
       <literal>to_timestamp('2000    JUN','YYYY MON')</literal> is right, but
3144 3145
       <literal>to_timestamp('2000    JUN','FXYYYY MON')</literal> returns an error,
       because <function>to_timestamp</function> expects one blank space only.
3146 3147 3148 3149 3150
      </para>
     </listitem>

     <listitem>
      <para>
3151 3152 3153 3154
       If a backslash (<quote><literal>\</literal></quote>) is desired
       in a string constant, a double backslash
       (<quote><literal>\\</literal></quote>) must be entered; for
       example <literal>'\\HH\\MI\\SS'</literal>.  This is true for
3155
       any string constant in <productname>PostgreSQL</productname>.
3156 3157 3158 3159 3160
      </para>
     </listitem>

     <listitem>
      <para>
3161
       Ordinary text is allowed in <function>to_char</function>
3162 3163 3164 3165 3166 3167
       templates and will be output literally.  You can put a substring
       in double quotes to force it to be interpreted as literal text
       even if it contains pattern keywords.  For example, in
       <literal>'"Hello Year: "YYYY'</literal>, the <literal>YYYY</literal>
       will be replaced by year data, but the single <literal>Y</literal>
       will not be.
3168 3169 3170 3171 3172
      </para>
     </listitem>

     <listitem>
      <para>
3173 3174 3175
       If you want to have a double quote in the output you must
       precede it with a backslash, for example <literal>'\\"YYYY
       Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
3176 3177
      </para>
     </listitem>
3178 3179 3180

     <listitem>
      <para>
3181 3182
       <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
       <type>date</type> is restricted if you use a year with more than 4 digits. You must
P
Peter Eisentraut 已提交
3183
       use some non-digit character or template after <literal>YYYY</literal>,
3184
       otherwise the year is always interpreted as 4 digits. For example
P
Peter Eisentraut 已提交
3185 3186
       (with year 20000):
       <literal>to_date('200001131', 'YYYYMMDD')</literal> will be 
3187
       interpreted as a 4-digit year; better is to use a non-digit 
P
Peter Eisentraut 已提交
3188 3189 3190
       separator after the year, like
       <literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
       <literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
3191 3192
      </para>
     </listitem>
B
Bruce Momjian 已提交
3193 3194 3195

     <listitem>
      <para>
3196
       Millisecond <literal>MS</literal> and microsecond <literal>US</literal>
P
Peter Eisentraut 已提交
3197 3198
       values in a conversion from string to time stamp are used as part of the
       seconds after the decimal point. For example 
B
Bruce Momjian 已提交
3199
       <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
P
Peter Eisentraut 已提交
3200
       but 300, because the conversion counts it as 12 + 0.3.
3201 3202
       This means for the format <literal>SS:MS</literal>, the input values
       <literal>12:3</literal>, <literal>12:30</literal>, and <literal>12:300</literal> specify the
P
Peter Eisentraut 已提交
3203
       same number of milliseconds. To get three milliseconds, one must use
3204
       <literal>12:003</literal>, which the conversion counts as
P
Peter Eisentraut 已提交
3205 3206 3207 3208 3209
       12 + 0.003 = 12.003 seconds.
      </para>

      <para>
       Here is a more 
B
Bruce Momjian 已提交
3210 3211
       complex example: 
       <literal>to_timestamp('15:12:02.020.001230','HH:MI:SS.MS.US')</literal>
P
Peter Eisentraut 已提交
3212 3213
       is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
       1230 microseconds = 2.021230 seconds. 
B
Bruce Momjian 已提交
3214 3215
      </para>
     </listitem>
3216
    </itemizedlist>
3217
   </para>
3218

3219
    <table tocentry="1">
3220
     <title>Template patterns for numeric conversions</title>
3221 3222 3223
     <tgroup cols="2">
      <thead>
       <row>
3224
	<entry>Pattern</entry>
3225 3226 3227 3228 3229
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
3230
	<entry><literal>9</literal></entry>
3231
	<entry>value with the specified number of digits</entry>
3232 3233
       </row>
       <row>
3234
	<entry><literal>0</literal></entry>
3235
	<entry>value with leading zeros</entry>
3236 3237
       </row>
       <row>
3238
	<entry><literal>.</literal> (period)</entry>
3239
	<entry>decimal point</entry>
3240 3241
       </row>       
       <row>
3242
	<entry><literal>,</literal> (comma)</entry>
3243
	<entry>group (thousand) separator</entry>
3244 3245
       </row>
       <row>
3246
	<entry><literal>PR</literal></entry>
3247
	<entry>negative value in angle brackets</entry>
3248 3249
       </row>
       <row>
3250
	<entry><literal>S</literal></entry>
3251
	<entry>negative value with minus sign (uses locale)</entry>
3252 3253
       </row>
       <row>
3254
	<entry><literal>L</literal></entry>
3255
	<entry>currency symbol (uses locale)</entry>
3256 3257
       </row>
       <row>
3258
	<entry><literal>D</literal></entry>
3259
	<entry>decimal point (uses locale)</entry>
3260 3261
       </row>
       <row>
3262
	<entry><literal>G</literal></entry>
3263
	<entry>group separator (uses locale)</entry>
3264 3265
       </row>
       <row>
3266
	<entry><literal>MI</literal></entry>
3267
	<entry>minus sign in specified position (if number < 0)</entry>
3268 3269
       </row>
       <row>
3270
	<entry><literal>PL</literal></entry>
3271
	<entry>plus sign in specified position (if number > 0)</entry>
3272 3273
       </row>
       <row>
3274
	<entry><literal>SG</literal></entry>
3275
	<entry>plus/minus sign in specified position</entry>
3276 3277
       </row>
       <row>
3278
	<entry><literal>RN</literal></entry>
3279
	<entry>roman numeral (input between 1 and 3999)</entry>
3280 3281
       </row>
       <row>
3282
	<entry><literal>TH</literal> or <literal>th</literal></entry>
3283
	<entry>convert to ordinal number</entry>
3284 3285
       </row>
       <row>
3286
	<entry><literal>V</literal></entry>
3287
	<entry>shift <replaceable>n</replaceable> digits (see
3288
	 notes)</entry>
3289 3290
       </row>
       <row>
3291
	<entry><literal>EEEE</literal></entry>
P
Peter Eisentraut 已提交
3292
	<entry>scientific notation (not implemented yet)</entry>
3293 3294 3295 3296 3297 3298
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
3299 3300 3301 3302 3303
    Usage notes:

    <itemizedlist>
     <listitem>
      <para>
3304 3305
       A sign formatted using <literal>SG</literal>, <literal>PL</literal>, or
       <literal>MI</literal> is not an anchor in
3306
       the number; for example,
3307 3308
       <literal>to_char(-12, 'S9999')</literal> produces <literal>'  -12'</literal>,
       but <literal>to_char(-12, 'MI9999')</literal> produces <literal>'-  12'</literal>.
3309 3310
       The Oracle implementation does not allow the use of
       <literal>MI</literal> ahead of <literal>9</literal>, but rather
3311
       requires that <literal>9</literal> precede
3312 3313 3314 3315 3316 3317
       <literal>MI</literal>.
      </para>
     </listitem>

     <listitem>
      <para>
3318 3319 3320
       <literal>9</literal> specifies a value with the same number of 
       digits as there are <literal>9</literal>s. If a digit is
       not available use blank space.
3321 3322 3323 3324 3325
      </para>
     </listitem>

     <listitem>
      <para>
3326 3327
       <literal>TH</literal> does not convert values less than zero
       and does not convert decimal numbers.
3328 3329 3330 3331 3332
      </para>
     </listitem>

     <listitem>
      <para>
3333
       <literal>PL</literal>, <literal>SG</literal>, and
3334
       <literal>TH</literal> are <productname>PostgreSQL</productname>
3335
       extensions. 
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
      </para>
     </listitem>

     <listitem>
      <para>
       <literal>V</literal> effectively
       multiplies the input values by
       <literal>10^<replaceable>n</replaceable></literal>, where
       <replaceable>n</replaceable> is the number of digits following
       <literal>V</literal>. 
       <function>to_char</function> does not support the use of
3347 3348
       <literal>V</literal> combined with a decimal point.
       (E.g., <literal>99.9V99</literal> is not allowed.)
3349 3350 3351
      </para>
     </listitem>
    </itemizedlist>
3352
   </para>   
3353 3354

    <table tocentry="1">
3355
     <title><function>to_char</function> Examples</title>
3356 3357 3358 3359 3360 3361 3362 3363 3364
     <tgroup cols="2">
      <thead>
       <row>
	<entry>Input</entry>
	<entry>Output</entry>
       </row>
      </thead>
      <tbody>
       <row>
3365
        <entry><literal>to_char(now(),'Day, DD  HH12:MI:SS')</literal></entry>
3366
        <entry><literal>'Tuesday  , 06  05:39:18'</literal></entry>
3367
       </row>
3368
       <row>
3369
        <entry><literal>to_char(now(),'FMDay, FMDD  HH12:MI:SS')</literal></entry>
3370
        <entry><literal>'Tuesday, 6  05:39:18'</literal></entry>
3371 3372
       </row>          
       <row>
3373
        <entry><literal>to_char(-0.1,'99.99')</literal></entry>
3374
        <entry><literal>' -.10'</literal></entry>
3375 3376
       </row>
       <row>
3377
        <entry><literal>to_char(-0.1,'FM9.99')</literal></entry>
3378
        <entry><literal>'-.1'</literal></entry>
3379 3380
       </row>
       <row>
3381
        <entry><literal>to_char(0.1,'0.9')</literal></entry>
3382
        <entry><literal>' 0.1'</literal></entry>
3383 3384
       </row>
       <row>
3385
        <entry><literal>to_char(12,'9990999.9')</literal></entry>
3386
        <entry><literal>'    0012.0'</literal></entry>
3387 3388
       </row>
       <row>
3389
        <entry><literal>to_char(12,'FM9990999.9')</literal></entry>
3390
        <entry><literal>'0012'</literal></entry>
3391 3392
       </row>
       <row>
3393
        <entry><literal>to_char(485,'999')</literal></entry>
3394
        <entry><literal>' 485'</literal></entry>
3395 3396
       </row>
       <row>
3397
        <entry><literal>to_char(-485,'999')</literal></entry>
3398
        <entry><literal>'-485'</literal></entry>
3399 3400
       </row>
       <row>
3401
        <entry><literal>to_char(485,'9 9 9')</literal></entry>
3402
        <entry><literal>' 4 8 5'</literal></entry>
3403 3404
       </row>
       <row>
3405
        <entry><literal>to_char(1485,'9,999')</literal></entry>
3406
        <entry><literal>' 1,485'</literal></entry>
3407 3408
       </row>
       <row>
3409
        <entry><literal>to_char(1485,'9G999')</literal></entry>
3410
        <entry><literal>' 1 485'</literal></entry>
3411 3412
       </row>
       <row>
3413
        <entry><literal>to_char(148.5,'999.999')</literal></entry>
3414
        <entry><literal>' 148.500'</literal></entry>
3415 3416
       </row>
       <row>
3417
        <entry><literal>to_char(148.5,'999D999')</literal></entry>
3418
        <entry><literal>' 148,500'</literal></entry>	 
3419 3420
       </row>
       <row>
3421
        <entry><literal>to_char(3148.5,'9G999D999')</literal></entry>
3422
        <entry><literal>' 3 148,500'</literal></entry>
3423 3424
       </row>
       <row>
3425
        <entry><literal>to_char(-485,'999S')</literal></entry>
3426
        <entry><literal>'485-'</literal></entry>
3427 3428
       </row>
       <row>		
3429
        <entry><literal>to_char(-485,'999MI')</literal></entry>
3430
        <entry><literal>'485-'</literal></entry>	
3431 3432
       </row>
       <row>
3433
        <entry><literal>to_char(485,'999MI')</literal></entry>
3434
        <entry><literal>'485'</literal></entry>		
3435 3436
       </row>
       <row>
3437
        <entry><literal>to_char(485,'PL999')</literal></entry>
3438
        <entry><literal>'+485'</literal></entry>	
3439 3440
       </row>
       <row>		
3441
        <entry><literal>to_char(485,'SG999')</literal></entry>
3442
        <entry><literal>'+485'</literal></entry>	
3443 3444
       </row>
       <row>
3445
        <entry><literal>to_char(-485,'SG999')</literal></entry>
3446
        <entry><literal>'-485'</literal></entry>	
3447 3448
       </row>
       <row>
3449
        <entry><literal>to_char(-485,'9SG99')</literal></entry>
3450
        <entry><literal>'4-85'</literal></entry>	
3451 3452
       </row>
       <row>
3453
        <entry><literal>to_char(-485,'999PR')</literal></entry>
3454
        <entry><literal>'&lt;485&gt;'</literal></entry>		
3455 3456
       </row>
       <row>
3457
        <entry><literal>to_char(485,'L999')</literal></entry>
3458
        <entry><literal>'DM 485</literal></entry>	 
3459 3460
       </row>
       <row>
3461
        <entry><literal>to_char(485,'RN')</literal></entry>		
3462
        <entry><literal>'        CDLXXXV'</literal></entry>
3463 3464
       </row>
       <row>
3465
        <entry><literal>to_char(485,'FMRN')</literal></entry>	
3466
        <entry><literal>'CDLXXXV'</literal></entry>
3467 3468
       </row>
       <row>
3469
        <entry><literal>to_char(5.2,'FMRN')</literal></entry>
3470
        <entry><literal>V</literal></entry>		
3471 3472
       </row>
       <row>
3473
        <entry><literal>to_char(482,'999th')</literal></entry>
3474
        <entry><literal>' 482nd'</literal></entry>				
3475 3476
       </row>
       <row>
3477
        <entry><literal>to_char(485, '"Good number:"999')</literal></entry>
3478
        <entry><literal>'Good number: 485'</literal></entry>
3479 3480
       </row>
       <row>
3481
        <entry><literal>to_char(485.8,'"Pre:"999" Post:" .999')</literal></entry>
3482
        <entry><literal>'Pre: 485 Post: .800'</literal></entry>
3483 3484
       </row>
       <row>
3485
        <entry><literal>to_char(12,'99V999')</literal></entry>		
3486
        <entry><literal>' 12000'</literal></entry>
3487 3488
       </row>
       <row>
3489
        <entry><literal>to_char(12.4,'99V999')</literal></entry>
3490
        <entry><literal>' 12400'</literal></entry>
3491 3492
       </row>
       <row>		
3493
        <entry><literal>to_char(12.45, '99V9')</literal></entry>
3494
        <entry><literal>' 125'</literal></entry>
3495 3496 3497 3498
       </row>
      </tbody>
     </tgroup>
    </table>
3499

3500 3501 3502
  </sect1>


3503
  <sect1 id="functions-datetime">
3504
   <title>Date/Time Functions and Operators</title>
3505

3506
   <para>
3507
    <xref linkend="functions-datetime-table"> shows the available
3508 3509 3510 3511 3512
    functions for date/time value processing.  
    <xref linkend="operators-datetime-table"> illustrates the
    behaviors of the basic arithmetic
    operators (<literal>+</literal>, <literal>*</literal>, etc.).
    For formatting functions, refer to <xref
3513
                                             linkend="functions-formatting">.  You should be familiar with the
3514
    background information on date/time data types (see <xref
3515
                                                              linkend="datatype-datetime">).
3516
   </para>
3517

3518 3519 3520 3521 3522 3523 3524
   <para>
    The date/time operators described below behave similarly for types
    involving time zones as well as those without.

    <table id="operators-datetime-table">
     <title>Date/Time Operators</title>

3525
     <tgroup cols="3">
3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
      <thead>
       <row>
        <entry>Name</entry>
        <entry>Example</entry>
        <entry>Result</entry>
       </row>
      </thead>

      <tbody>
       <row>
3536
        <entry> <literal>+</literal> </entry>
3537 3538 3539 3540 3541
        <entry><type>timestamp</type> '2001-09-28 01:00' + <type>interval</type> '23 hours'</entry>
        <entry><type>timestamp</type> '2001-09-29 00:00'</entry>
       </row>

       <row>
3542
        <entry> <literal>+</literal> </entry>
3543 3544 3545 3546 3547
        <entry><type>date</type> '2001-09-28' + <type>interval</type> '1 hour'</entry>
        <entry><type>timestamp</type> '2001-09-28 01:00'</entry>
       </row>

       <row>
3548
        <entry> <literal>+</literal> </entry>
3549 3550 3551 3552 3553
        <entry><type>time</type> '01:00' + <type>interval</type> '3 hours'</entry>
        <entry><type>time</type> '04:00'</entry>
       </row>

       <row>
3554
        <entry> <literal>-</literal> </entry>
3555 3556 3557 3558 3559
        <entry><type>timestamp</type> '2001-09-28 23:00' - <type>interval</type> '23 hours'</entry>
        <entry><type>timestamp</type> '2001-09-28'</entry>
       </row>

       <row>
3560
        <entry> <literal>-</literal> </entry>
3561
        <entry><type>date</type> '2001-09-28' - <type>interval</type> '1 hour'</entry>
3562 3563 3564 3565
        <entry><type>timestamp</type> '2001-09-27 23:00'</entry>
       </row>

       <row>
3566
        <entry> <literal>-</literal> </entry>
3567
        <entry><type>time</type> '05:00' - <type>interval</type> '2 hours'</entry>
3568 3569 3570 3571
        <entry><type>time</type> '03:00'</entry>
       </row>

       <row>
3572
        <entry> <literal>-</literal> </entry>
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
        <entry><type>interval</type> '2 hours' - <type>time</type> '05:00'</entry>
        <entry><type>time</type> '03:00:00'</entry>
       </row>

       <row>
        <entry> <literal>*</literal> </entry>
        <entry><type>interval</type> '1 hour' * <type>int</type> '3'</entry>
        <entry><type>interval</type> '03:00'</entry>
       </row>

       <row>
        <entry> <literal>/</literal> </entry>
        <entry><type>interval</type> '1 hour' / <type>int</type> '3'</entry>
        <entry><type>interval</type> '00:20'</entry>
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
    The date/time functions are summarized below, with additional
    details in subsequent sections.

3597
    <table id="functions-datetime-table">
3598
     <title>Date/Time Functions</title>
3599
     <tgroup cols="5">
3600 3601
      <thead>
       <row>
3602 3603
	<entry>Name</entry>
	<entry>Return Type</entry>
3604 3605
	<entry>Description</entry>
	<entry>Example</entry>
3606
	<entry>Result</entry>
3607 3608
       </row>
      </thead>
3609

3610 3611
      <tbody>
       <row>
3612 3613
	<entry><function>age</function>(<type>timestamp</type>)</entry>
	<entry><type>interval</type></entry>
3614
	<entry>Subtract from today</entry>
3615 3616
	<entry><literal>age(timestamp '1957-06-13')</literal></entry>
	<entry><literal>43 years 8 mons 3 days</literal></entry>
3617
       </row>
3618

3619
       <row>
3620 3621
	<entry><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</entry>
	<entry><type>interval</type></entry>
3622
	<entry>Subtract arguments</entry>
3623 3624
	<entry><literal>age('2001-04-10', timestamp '1957-06-13')</literal></entry>
	<entry><literal>43 years 9 mons 27 days</literal></entry>
3625 3626 3627
       </row>

       <row>
3628 3629
	<entry><function>current_date</function></entry>
	<entry><type>date</type></entry>
3630
	<entry>Today's date; see <link linkend="functions-datetime-current">below</link>
3631 3632 3633
	</entry>
	<entry></entry>
	<entry></entry>
3634
       </row>
3635 3636

       <row>
3637 3638 3639 3640 3641 3642
        <entry><function>current_time</function></entry>
        <entry><type>time with time zone</type></entry>
        <entry>Time of day; see <link linkend="functions-datetime-current">below</link>
        </entry>
        <entry></entry>
        <entry></entry>
3643 3644
       </row>

3645
       <row>
3646
	<entry><function>current_timestamp</function></entry>
3647
	<entry><type>timestamp with time zone</type></entry>
3648
	<entry>Date and time; see <link linkend="functions-datetime-current">below</link>
3649 3650 3651 3652 3653 3654
	</entry>
	<entry></entry>
	<entry></entry>
       </row>

       <row>
3655 3656
	<entry><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</entry>
	<entry><type>double precision</type></entry>
3657
	<entry>Get subfield (equivalent to
3658
	 <function>extract</function>); see also <link
3659
                                                linkend="functions-datetime-datepart">below</link>
3660
        </entry>
3661 3662
	<entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
	<entry><literal>20</literal></entry>
3663
       </row>
3664

3665
       <row>
3666 3667
	<entry><function>date_part</function>(<type>text</type>, <type>interval</type>)</entry>
	<entry><type>double precision</type></entry>
3668
	<entry>Get subfield (equivalent to
3669
	 <function>extract</function>); see also <link
3670
                                                linkend="functions-datetime-datepart">below</link>
3671
        </entry>
3672 3673
	<entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
	<entry><literal>3</literal></entry>
3674
       </row>
3675

3676
       <row>
3677 3678
	<entry><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</entry>
	<entry><type>timestamp</type></entry>
3679
	<entry>Truncate to specified precision; see also <link
3680
                                                        linkend="functions-datetime-trunc">below</link>
3681
        </entry>
3682 3683
	<entry><literal>date_trunc('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
	<entry><literal>2001-02-16 20:00:00+00</literal></entry>
3684
       </row>
3685

3686
       <row>
3687 3688
	<entry><function>extract</function>(<parameter>field</parameter> from
         <type>timestamp</type>)</entry>
3689
	<entry><type>double precision</type></entry>
3690
	<entry>Get subfield; see also <link
3691
                                     linkend="functions-datetime-extract">below</link>
3692
        </entry>
3693 3694
	<entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
	<entry><literal>20</literal></entry>
3695
       </row>
3696 3697

       <row>
3698 3699
	<entry><function>extract</function>(<parameter>field</parameter> from
         <type>interval</type>)</entry>
3700
	<entry><type>double precision</type></entry>
3701
	<entry>Get subfield; see also <link
3702
                                     linkend="functions-datetime-extract">below</link>
3703
        </entry>
3704 3705
	<entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
	<entry><literal>3</literal></entry>
3706 3707
       </row>

3708
       <row>
3709 3710
	<entry><function>isfinite</function>(<type>timestamp</type>)</entry>
	<entry><type>boolean</type></entry>
3711
	<entry>Test for finite time stamp (neither invalid nor infinity)</entry>
3712 3713
	<entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
	<entry><literal>true</literal></entry>
3714
       </row>
3715

3716
       <row>
3717 3718
	<entry><function>isfinite</function>(<type>interval</type>)</entry>
	<entry><type>boolean</type></entry>
3719
	<entry>Test for finite interval</entry>
3720 3721
	<entry><literal>isfinite(interval '4 hours')</literal></entry>
	<entry><literal>true</literal></entry>
3722
       </row>
3723

3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
       <row>
        <entry><function>localtime</function></entry>
        <entry><type>time</type></entry>
        <entry>Time of day; see <link linkend="functions-datetime-current">below</link>
        </entry>
        <entry></entry>
        <entry></entry>
       </row>

       <row>
        <entry><function>localtimestamp</function></entry>
        <entry><type>timestamp</type></entry>
        <entry>Date and time; see <link linkend="functions-datetime-current">below</link>
        </entry>
        <entry></entry>
        <entry></entry>
       </row>

3742
       <row>
3743 3744
	<entry><function>now</function>()</entry>
	<entry><type>timestamp</type></entry>
3745
	<entry>Current date and time (equivalent to
3746
	 <function>current_timestamp</function>); see <link
3747
                                                     linkend="functions-datetime-current">below</link>
3748 3749 3750
	</entry>
	<entry></entry>
	<entry></entry>
3751
       </row>
3752

3753
       <row>
3754 3755
	<entry><function>timeofday()</function></entry>
	<entry><type>text</type></entry>
3756
	<entry>Current date and time; see <link
3757
                                         linkend="functions-datetime-current">below</link>
3758
	</entry>
3759 3760
	<entry><literal>timeofday()</literal></entry>
	<entry><literal>Wed Feb 21 17:01:13.000126 2001 EST</literal></entry>
3761 3762
       </row>

3763 3764 3765
      </tbody>
     </tgroup>
    </table>
3766
   </para>
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781

  <sect2 id="functions-datetime-extract">
   <title><function>EXTRACT</function>, <function>date_part</function></title>

<synopsis>
EXTRACT (<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
</synopsis>

   <para>
    The <function>extract</function> function retrieves sub-fields
    from date/time values, such as year or hour.
    <replaceable>source</replaceable> is a value expression that
    evaluates to type <type>timestamp</type> or <type>interval</type>.
    (Expressions of type <type>date</type> or <type>time</type> will
    be cast to <type>timestamp</type> and can therefore be used as
3782 3783
    well.)  <replaceable>field</replaceable> is an identifier or
    string that selects what field to extract from the source value.
3784 3785 3786 3787 3788 3789 3790
    The <function>extract</function> function returns values of type
    <type>double precision</type>.
    The following are valid values:

    <!-- alphabetical -->
    <variablelist>
     <varlistentry>
3791
      <term><literal>century</literal></term>
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
      <listitem>
       <para>
        The year field divided by 100
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
</screen>
       </informalexample>

       <para>
3805 3806 3807
        Note that the result for the century field is simply the year field
        divided by 100, and not the conventional definition which puts most
        years in the 1900's in the twentieth century.
3808 3809 3810 3811 3812
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
3813
      <term><literal>day</literal></term>
3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
      <listitem>
       <para>
        The day (of the month) field (1 - 31)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3829
      <term><literal>decade</literal></term>
3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844
      <listitem>
       <para>
        The year field divided by 10
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>200</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3845
      <term><literal>dow</literal></term>
3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
      <listitem>
       <para>
        The day of the week (0 - 6; Sunday is 0) (for
        <type>timestamp</type> values only)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>5</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3862
      <term><literal>doy</literal></term>
3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
      <listitem>
       <para>
        The day of the year (1 - 365/366) (for <type>timestamp</type> values only)
       </para>
       <informalexample>
<screen>
SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>47</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3877
      <term><literal>epoch</literal></term>
3878 3879 3880
      <listitem>
       <para>
        For <type>date</type> and <type>timestamp</type> values, the
3881
        number of seconds since 1970-01-01 00:00:00-00 (Result may be
3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898
        negative.); for <type>interval</type> values, the total number
        of seconds in the interval
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>982352320</computeroutput>

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
<lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3899
      <term><literal>hour</literal></term>
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
      <listitem>
       <para>
        The hour field (0 - 23)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3915
      <term><literal>microseconds</literal></term>
3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
      <listitem>
       <para>
        The seconds field, including fractional parts, multiplied by 1
        000 000.  Note that this includes full seconds.
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28500000</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3932
      <term><literal>millennium</literal></term>
3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945
      <listitem>
       <para>
        The year field divided by 1000
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
</screen>
       </informalexample>

       <para>
3946 3947 3948
        Note that the result for the millennium field is simply the year field
        divided by 1000, and not the conventional definition which puts
        years in the 1900's in the second millennium.
3949 3950 3951 3952 3953
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
3954
      <term><literal>milliseconds</literal></term>
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
      <listitem>
       <para>
        The seconds field, including fractional parts, multiplied by
        1000.  Note that this includes full seconds.
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28500</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3971
      <term><literal>minute</literal></term>
3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
      <listitem>
       <para>
        The minutes field (0 - 59)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>38</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
3987
      <term><literal>month</literal></term>
3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010
      <listitem>
       <para>
        For <type>timestamp</type> values, the number of the month
        within the year (1 - 12) ; for <type>interval</type> values
        the number of months, modulo 12 (0 - 11)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
4011
      <term><literal>quarter</literal></term>
4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027
      <listitem>
       <para>
        The quarter of the year (1 - 4) that the day is in (for
        <type>timestamp</type> values only)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
4028
      <term><literal>second</literal></term>
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
      <listitem>
       <para>
        The seconds field, including fractional parts (0 -
        59<footnote><simpara>60 if leap seconds are
        implemented by the operating system</simpara></footnote>)
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074
<!--
     <varlistentry>
      <term><literal>timezone</literal></term>
      <listitem>
       <para>
        The time zone offset. XXX But in what units?
       </para>
      </listitem>
     </varlistentry>
-->

     <varlistentry>
      <term><literal>timezone_hour</literal></term>
      <listitem>
       <para>
        The hour component of the time zone offset.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><literal>timezone_minute</literal></term>
      <listitem>
       <para>
        The minute component of the time zone offset.
       </para>
      </listitem>
     </varlistentry>
4075 4076

     <varlistentry>
4077
      <term><literal>week</literal></term>
4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
      <listitem>
       <para>
        From a <type>timestamp</type> value, calculate the number of
        the week of the year that the day is in.  By definition
        (<acronym>ISO</acronym> 8601), the first week of a year
        contains January 4 of that year.  (The <acronym>ISO</acronym>
        week starts on Monday.)  In other words, the first Thursday of
        a year is in week 1 of that year.
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>7</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

     <varlistentry>
4098
      <term><literal>year</literal></term>
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
      <listitem>
       <para>
        The year field
       </para>

       <informalexample>
<screen>
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2001</computeroutput>
</screen>
       </informalexample>
      </listitem>
     </varlistentry>

    </variablelist>

4115 4116 4117
   </para>

   <para>
4118 4119 4120
    The <function>extract</function> function is primarily intended
    for computational processing.  For formatting date/time values for
    display, see <xref linkend="functions-formatting">.
4121
   </para>
4122 4123 4124

   <anchor id="functions-datetime-datepart">
   <para>
4125 4126
    The <function>date_part</function> function is modeled on the traditional
    <productname>Ingres</productname> equivalent to the
4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141
    <acronym>SQL</acronym>-function <function>extract</function>:
<synopsis>
date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
</synopsis>
    Note that here the <replaceable>field</replaceable> value needs to
    be a string.  The valid field values for
    <function>date_part</function> are the same as for
    <function>extract</function>.
   </para>

   <informalexample>
<screen>
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>

T
Tatsuo Ishii 已提交
4142
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
</screen>
   </informalexample>

  </sect2>

  <sect2 id="functions-datetime-trunc">
   <title><function>date_trunc</function></title>

   <para>
    The function <function>date_trunc</function> is conceptually
    similar to the <function>trunc</function> function for numbers.
   </para>

   <para>
<synopsis>
date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
</synopsis>
    <replaceable>source</replaceable> is a value expression of type
    <type>timestamp</type> (values of type <type>date</type> and
    <type>time</type> are cast automatically).
    <replaceable>field</replaceable> selects to which precision to
    truncate the time stamp value.  The return value is of type
    <type>timestamp</type> with all fields that are less than the
    selected one set to zero (or one, for day and month).
   </para>

   <para>
    Valid values for <replaceable>field</replaceable> are:
    <simplelist>
     <member>microseconds</member>
     <member>milliseconds</member>
     <member>second</member>
     <member>minute</member>
     <member>hour</member>
     <member>day</member>
     <member>month</member>
     <member>year</member>
     <member>decade</member>
     <member>century</member>
     <member>millennium</member>
    </simplelist>
   </para>

   <informalexample>
    <para>
<screen>
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00+00</computeroutput>

SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00+00</computeroutput>
</screen>
    </para>
   </informalexample>
  </sect2>

  <sect2 id="functions-datetime-current">
   <title>Current Date/Time</title>

4203 4204 4205 4206 4207 4208 4209 4210 4211 4212
   <indexterm>
    <primary>date</primary>
    <secondary>current</secondary>
   </indexterm>

   <indexterm>
    <primary>time</primary>
    <secondary>current</secondary>
   </indexterm>

4213
   <para>
4214 4215
    The following functions are available to obtain the current date and/or
    time:
4216
     <synopsis>
4217
CURRENT_DATE
4218
CURRENT_TIME
4219
CURRENT_TIMESTAMP
4220 4221
CURRENT_TIME ( <replaceable>precision</replaceable> )
CURRENT_TIMESTAMP ( <replaceable>precision</replaceable> )
4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234
LOCALTIME
LOCALTIMESTAMP
LOCALTIME ( <replaceable>precision</replaceable> )
LOCALTIMESTAMP ( <replaceable>precision</replaceable> )
     </synopsis>
     <function>CURRENT_TIME</function>,
     <function>CURRENT_TIMESTAMP</function>,
     <function>LOCALTIME</function>, and
     <function>LOCALTIMESTAMP</function>
     can optionally be given
     a precision parameter, which causes the result to be rounded
     to that many fractional digits.  Without a precision parameter,
     the result is given to the full available precision.
4235 4236
    </para>

4237 4238 4239 4240 4241 4242 4243
    <note>
     <para>
      Prior to <productname>PostgreSQL</productname> 7.2, the precision
      parameters were unimplemented, and the result was always given
      in integer seconds.
     </para>
    </note>
4244

4245 4246
    <informalexample>
     <screen>
4247
SELECT CURRENT_TIME;
4248
<computeroutput>14:39:53.662522-05</computeroutput>
4249 4250

SELECT CURRENT_DATE;
4251
<computeroutput>2001-12-23</computeroutput>
4252 4253

SELECT CURRENT_TIMESTAMP;
4254 4255 4256 4257
<computeroutput>2001-12-23 14:39:53.662522-05</computeroutput>

SELECT CURRENT_TIMESTAMP(2);
<computeroutput>2001-12-23 14:39:53.66-05</computeroutput>
4258 4259 4260

SELECT LOCALTIMESTAMP;
<computeroutput>2001-12-23 14:39:53.662522</computeroutput>
4261 4262 4263 4264 4265
</screen>
   </informalexample>

   <para>
    The function <function>now()</function> is the traditional
4266
    <productname>PostgreSQL</productname> equivalent to
4267
    <function>CURRENT_TIMESTAMP</function>.
4268 4269 4270
   </para>

   <para>
4271 4272
    There is also <function>timeofday()</function>, which for historical
    reasons returns a text string rather than a <type>timestamp</type> value:
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290
   </para>

   <informalexample>
<screen>
SELECT timeofday();
 Sat Feb 17 19:07:32.000126 2001 EST
</screen>
   </informalexample>

   <para>
    It is quite important to realize that
    <function>CURRENT_TIMESTAMP</function> and related functions all return
    the time as of the start of the current transaction; their values do not
    increment while a transaction is running.  But
    <function>timeofday()</function> returns the actual current time.
   </para>

   <para>
4291
    All the date/time data types also accept the special literal value
4292
    <literal>now</literal> to specify the current date and time.  Thus,
4293
    the following three all return the same result:
4294 4295 4296 4297 4298 4299 4300 4301
<programlisting>
SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT TIMESTAMP 'now';
</programlisting>
    <note>
     <para>
      You do not want to use the third form when specifying a DEFAULT
4302
      value while creating a table.  The system will convert <literal>now</literal>
4303
      to a <type>timestamp</type> as soon as the constant is parsed, so that when
4304
      the default value is needed,
4305 4306
      the time of the table creation would be used!  The first two
      forms will not be evaluated until the default value is used,
4307 4308
      because they are function calls.  Thus they will give the desired
      behavior of defaulting to the time of row insertion.
4309 4310 4311 4312 4313
     </para>
    </note>
   </para>
  </sect2>
 </sect1>
4314 4315 4316 4317 4318 4319

  
 <sect1 id="functions-geometry">
   <title>Geometric Functions and Operators</title>

   <para>
4320 4321 4322 4323
    The geometric types <type>point</type>, <type>box</type>,
    <type>lseg</type>, <type>line</type>, <type>path</type>,
    <type>polygon</type>, and <type>circle</type> have a large set of
    native support functions and operators.
4324 4325 4326
   </para>

   <table>
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363
     <title>Geometric Operators</title>
     <tgroup cols="3">
      <thead>
       <row>
	<entry>Operator</entry>
	<entry>Description</entry>
	<entry>Usage</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> + </entry>
	<entry>Translation</entry>
	<entry><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></entry>
       </row>
       <row>
	<entry> - </entry>
	<entry>Translation</entry>
	<entry><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></entry>
       </row>
       <row>
	<entry> * </entry>
	<entry>Scaling/rotation</entry>
	<entry><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></entry>
       </row>
       <row>
	<entry> / </entry>
	<entry>Scaling/rotation</entry>
	<entry><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></entry>
       </row>
       <row>
	<entry> # </entry>
	<entry>Intersection</entry>
	<entry><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></entry>
       </row>
       <row>
	<entry> # </entry>
4364
	<entry>Number of points in path or polygon</entry>
4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
	<entry><literal># '((1,0),(0,1),(-1,0))'</literal></entry>
       </row>
       <row>
	<entry> ## </entry>
	<entry>Point of closest proximity</entry>
	<entry><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></entry>
       </row>
       <row>
	<entry> &amp;&amp; </entry>
	<entry>Overlaps?</entry>
	<entry><literal>box '((0,0),(1,1))' &amp;&amp; box '((0,0),(2,2))'</literal></entry>
       </row>
       <row>
	<entry> &amp;&lt; </entry>
	<entry>Overlaps to left?</entry>
	<entry><literal>box '((0,0),(1,1))' &amp;&lt; box '((0,0),(2,2))'</literal></entry>
       </row>
       <row>
	<entry> &amp;&gt; </entry>
	<entry>Overlaps to right?</entry>
	<entry><literal>box '((0,0),(3,3))' &amp;&gt; box '((0,0),(2,2))'</literal></entry>
       </row>
       <row>
	<entry> &lt;-&gt; </entry>
	<entry>Distance between</entry>
	<entry><literal>circle '((0,0),1)' &lt;-&gt; circle '((5,0),1)'</literal></entry>
       </row>
       <row>
	<entry> &lt;&lt; </entry>
	<entry>Left of?</entry>
	<entry><literal>circle '((0,0),1)' &lt;&lt; circle '((5,0),1)'</literal></entry>
       </row>
       <row>
	<entry> &lt;^ </entry>
	<entry>Is below?</entry>
	<entry><literal>circle '((0,0),1)' &lt;^ circle '((0,5),1)'</literal></entry>
       </row>
       <row>
	<entry> &gt;&gt; </entry>
	<entry>Is right of?</entry>
	<entry><literal>circle '((5,0),1)' &gt;&gt; circle '((0,0),1)'</literal></entry>
       </row>
       <row>
	<entry> &gt;^ </entry>
	<entry>Is above?</entry>
	<entry><literal>circle '((0,5),1)' >^ circle '((0,0),1)'</literal></entry>
       </row>
       <row>
	<entry> ?# </entry>
	<entry>Intersects or overlaps</entry>
	<entry><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></entry>
       </row>
       <row>
	<entry> ?- </entry>
	<entry>Is horizontal?</entry>
	<entry><literal>point '(1,0)' ?- point '(0,0)'</literal></entry>
       </row>
       <row>
	<entry> ?-| </entry>
	<entry>Is perpendicular?</entry>
	<entry><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></entry>
       </row>
       <row>
	<entry> @-@  </entry>
	<entry>Length or circumference</entry>
	<entry><literal>@-@ path '((0,0),(1,0))'</literal></entry>
       </row>
       <row>
	<entry> ?| </entry>
	<entry>Is vertical?</entry>
	<entry><literal>point '(0,1)' ?| point '(0,0)'</literal></entry>
       </row>
       <row>
	<entry> ?|| </entry>
	<entry>Is parallel?</entry>
	<entry><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></entry>
       </row>
       <row>
	<entry> @ </entry>
	<entry>Contained or on</entry>
	<entry><literal>point '(1,1)' @ circle '((0,0),2)'</literal></entry>
       </row>
       <row>
	<entry> @@ </entry>
	<entry>Center of</entry>
	<entry><literal>@@ circle '((0,0),10)'</literal></entry>
       </row>
       <row>
	<entry> ~= </entry>
	<entry>Same as</entry>
	<entry><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></entry>
       </row>
      </tbody>
     </tgroup>
   </table>
4460 4461

   <table>
T
Thomas G. Lockhart 已提交
4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473
     <title>Geometric Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
4474 4475
	<entry><function>area</function>(object)</entry>
	<entry><type>double precision</type></entry>
4476
	<entry>area of item</entry>
4477
	<entry><literal>area(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
4478 4479
       </row>
       <row>
4480 4481
	<entry><function>box</function>(box, box)</entry>
	<entry><type>box</type></entry>
4482
	<entry>intersection box</entry>
4483
	<entry><literal>box(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
4484 4485
       </row>
       <row>
4486 4487
	<entry><function>center</function>(object)</entry>
	<entry><type>point</type></entry>
4488
	<entry>center of item</entry>
4489
	<entry><literal>center(box '((0,0),(1,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
4490 4491
       </row>
       <row>
4492 4493
	<entry><function>diameter</function>(circle)</entry>
	<entry><type>double precision</type></entry>
4494
	<entry>diameter of circle</entry>
4495
	<entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4496 4497
       </row>
       <row>
4498 4499
	<entry><function>height</function>(box)</entry>
	<entry><type>double precision</type></entry>
4500
	<entry>vertical size of box</entry>
4501
	<entry><literal>height(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
4502 4503
       </row>
       <row>
4504 4505
	<entry><function>isclosed</function>(path)</entry>
	<entry><type>boolean</type></entry>
4506
	<entry>a closed path?</entry>
4507
	<entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4508 4509
       </row>
       <row>
4510 4511
	<entry><function>isopen</function>(path)</entry>
	<entry><type>boolean</type></entry>
4512
	<entry>an open path?</entry>
4513
	<entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
4514 4515
       </row>
       <row>
4516 4517
	<entry><function>length</function>(object)</entry>
	<entry><type>double precision</type></entry>
4518
	<entry>length of item</entry>
4519
	<entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4520
       </row>
4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
       <row>
	<entry><function>npoints</function>(path)</entry>
	<entry><type>integer</type></entry>
	<entry>number of points</entry>
	<entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
       </row>
       <row>
	<entry><function>npoints</function>(polygon)</entry>
	<entry><type>integer</type></entry>
	<entry>number of points</entry>
	<entry><literal>npoints(polygon '((1,1),(0,0))')</literal></entry>
       </row>
T
Thomas G. Lockhart 已提交
4533
       <row>
4534 4535
	<entry><function>pclose</function>(path)</entry>
	<entry><type>path</type></entry>
4536
	<entry>convert path to closed</entry>
4537
	<entry><literal>popen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
4538
       </row>
4539 4540
<![IGNORE[
<!-- Not defined by this name. Implements the intersection operator '#' -->
T
Thomas G. Lockhart 已提交
4541
       <row>
4542 4543
	<entry><function>point</function>(lseg,lseg)</entry>
	<entry><type>point</type></entry>
4544
	<entry>intersection</entry>
4545
	<entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
4546
       </row>
4547
]]>
T
Thomas G. Lockhart 已提交
4548
       <row>
4549 4550
	<entry><function>popen</function>(path)</entry>
	<entry><type>path</type></entry>
4551
	<entry>convert path to open path</entry>
4552
	<entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4553 4554
       </row>
       <row>
4555 4556
	<entry><function>radius</function>(circle)</entry>
	<entry><type>double precision</type></entry>
4557
	<entry>radius of circle</entry>
4558
	<entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4559 4560
       </row>
       <row>
4561 4562
	<entry><function>width</function>(box)</entry>
	<entry><type>double precision</type></entry>
4563
	<entry>horizontal size</entry>
4564
	<entry><literal>width(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
4565 4566 4567
       </row>
      </tbody>
     </tgroup>
4568
   </table>
4569

4570 4571

   <table>
T
Thomas G. Lockhart 已提交
4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583
     <title>Geometric Type Conversion Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
4584 4585
	<entry><function>box</function>(<type>circle</type>)</entry>
	<entry><type>box</type></entry>
4586
	<entry>circle to box</entry>
4587
	<entry><literal>box(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4588 4589
       </row>
       <row>
4590 4591
	<entry><function>box</function>(<type>point</type>, <type>point</type>)</entry>
	<entry><type>box</type></entry>
4592
	<entry>points to box</entry>
4593
	<entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
T
Thomas G. Lockhart 已提交
4594 4595
       </row>
       <row>
4596 4597
	<entry><function>box</function>(<type>polygon</type>)</entry>
	<entry><type>box</type></entry>
4598
	<entry>polygon to box</entry>
4599
	<entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4600 4601
       </row>
       <row>
4602 4603
	<entry><function>circle</function>(<type>box</type>)</entry>
	<entry><type>circle</type></entry>
4604
	<entry>to circle</entry>
4605
	<entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
4606 4607
       </row>
       <row>
4608 4609
	<entry><function>circle</function>(<type>point</type>, <type>double precision</type>)</entry>
	<entry><type>circle</type></entry>
4610
	<entry>point to circle</entry>
4611
	<entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
T
Thomas G. Lockhart 已提交
4612 4613
       </row>
       <row>
4614 4615
	<entry><function>lseg</function>(<type>box</type>)</entry>
	<entry><type>lseg</type></entry>
4616
	<entry>box diagonal to <type>lseg</type></entry>
4617
	<entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4618 4619
       </row>
       <row>
4620 4621
	<entry><function>lseg</function>(<type>point</type>, <type>point</type>)</entry>
	<entry><type>lseg</type></entry>
4622
	<entry>points to <type>lseg</type></entry>
4623
	<entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4624 4625
       </row>
       <row>
4626 4627
	<entry><function>path</function>(<type>polygon</type>)</entry>
	<entry><type>point</type></entry>
4628
	<entry>polygon to path</entry>
4629
	<entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4630 4631
       </row>
       <row>
4632 4633
	<entry><function>point</function>(<type>circle</type>)</entry>
	<entry><type>point</type></entry>
4634
	<entry>center</entry>
4635
	<entry><literal>point(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4636 4637
       </row>
       <row>
4638 4639
	<entry><function>point</function>(<type>lseg</type>, <type>lseg</type>)</entry>
	<entry><type>point</type></entry>
4640
	<entry>intersection</entry>
4641
	<entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
4642 4643
       </row>
       <row>
4644 4645
	<entry><function>point</function>(<type>polygon</type>)</entry>
	<entry><type>point</type></entry>
4646
	<entry>center</entry>
4647
	<entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4648 4649
       </row>
       <row>
4650 4651
	<entry><function>polygon</function>(<type>box</type>)</entry>
	<entry><type>polygon</type></entry>
4652
	<entry>4-point polygon</entry>
4653
	<entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
4654 4655
       </row>
       <row>
4656 4657
	<entry><function>polygon</function>(<type>circle</type>)</entry>
	<entry><type>polygon</type></entry>
4658
	<entry>12-point polygon</entry>
4659
	<entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4660 4661
       </row>
       <row>
4662 4663
	<entry><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</entry>
	<entry><type>polygon</type></entry>
4664
	<entry><replaceable class="parameter">npts</replaceable> polygon</entry>
4665
	<entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
4666 4667
       </row>
       <row>
4668 4669
	<entry><function>polygon</function>(<type>path</type>)</entry>
	<entry><type>polygon</type></entry>
4670
	<entry>path to polygon</entry>
4671
	<entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
4672 4673 4674
       </row>
      </tbody>
     </tgroup>
4675 4676
   </table>

4677 4678 4679 4680 4681 4682 4683 4684 4685 4686
    <para>
     It is possible to access the two component numbers of a <type>point</>
     as though it were an array with subscripts 0,1.  For example, if
     <literal>t.p</> is a <type>point</> column then
     <literal>SELECT p[0] FROM t</> retrieves the X coordinate;
     <literal>UPDATE t SET p[1] = ...</> changes the Y coordinate.
     In the same way, a <type>box</> or an <type>lseg</> may be treated
     as an array of two <type>point</>s.
    </para>

4687
  </sect1>
4688

4689 4690

  <sect1 id="functions-net">
4691
   <title>Network Address Type Functions</title>
4692

4693 4694

    <table tocentry="1" id="cidr-inet-operators-table">
4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757
     <title><type>cidr</type> and <type>inet</type> Operators</title>
     <tgroup cols="3">
      <thead>
       <row>
	<entry>Operator</entry>
	<entry>Description</entry>
	<entry>Usage</entry>
       </row>
      </thead>
      <tbody>
       <row>
	<entry> &lt; </entry>
	<entry>Less than</entry>
	<entry><literal>inet '192.168.1.5' &lt; inet '192.168.1.6'</literal></entry>
       </row>
       <row>
	<entry> &lt;= </entry>
	<entry>Less than or equal</entry>
	<entry><literal>inet '192.168.1.5' &lt;= inet '192.168.1.5'</literal></entry>
       </row>
       <row>
	<entry> = </entry>
	<entry>Equals</entry>
	<entry><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></entry>
       </row>
       <row>
	<entry> &gt;= </entry>
	<entry>Greater or equal</entry>
	<entry><literal>inet '192.168.1.5' &gt;= inet '192.168.1.5'</literal></entry>
       </row>
       <row>
	<entry> &gt; </entry>
	<entry>Greater</entry>
	<entry><literal>inet '192.168.1.5' &gt; inet '192.168.1.4'</literal></entry>
       </row>
       <row>
	<entry> &lt;&gt; </entry>
	<entry>Not equal</entry>
	<entry><literal>inet '192.168.1.5' &lt;&gt; inet '192.168.1.4'</literal></entry>
       </row>
       <row>
	<entry> &lt;&lt; </entry>
	<entry>is contained within</entry>
	<entry><literal>inet '192.168.1.5' &lt;&lt; inet '192.168.1/24'</literal></entry>
       </row>
       <row>
	<entry> &lt;&lt;= </entry>
	<entry>is contained within or equals</entry>
	<entry><literal>inet '192.168.1/24' &lt;&lt;= inet '192.168.1/24'</literal></entry>
       </row>
       <row>
	<entry> &gt;&gt; </entry>
	<entry>contains</entry>
	<entry><literal>inet'192.168.1/24' &gt;&gt; inet '192.168.1.5'</literal></entry>
       </row>
       <row>
	<entry> &gt;&gt;= </entry>
	<entry>contains or equals</entry>
	<entry><literal>inet '192.168.1/24' &gt;&gt;= inet '192.168.1/24'</literal></entry>
       </row>
      </tbody>
     </tgroup>
    </table>
4758 4759 4760 4761

    <para>
     All of the operators for <type>inet</type> can be applied to
     <type>cidr</type> values as well.  The operators
4762 4763
     <literal>&lt;&lt;</literal>, <literal>&lt;&lt;=</literal>,
     <literal>&gt;&gt;</literal>, <literal>&gt;&gt;=</literal>
4764 4765 4766 4767 4768 4769
     test for subnet inclusion: they consider only the network parts
     of the two addresses, ignoring any host part, and determine whether
     one network part is identical to or a subnet of the other.
    </para>


4770
    <table tocentry="1" id="cidr-inet-functions">
4771
     <title><type>cidr</type> and <type>inet</type> Functions</title>
4772
     <tgroup cols="5">
T
Thomas G. Lockhart 已提交
4773 4774 4775 4776 4777 4778
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
4779
	<entry>Result</entry>
T
Thomas G. Lockhart 已提交
4780 4781 4782 4783
       </row>
      </thead>
      <tbody>
       <row>
4784 4785
	<entry><function>broadcast</function>(<type>inet</type>)</entry>
	<entry><type>inet</type></entry>
4786
	<entry>broadcast address for network</entry>
4787 4788
	<entry><literal>broadcast('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.255/24</literal></entry>
T
Thomas G. Lockhart 已提交
4789 4790
       </row>
       <row>
4791 4792
	<entry><function>host</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
4793
	<entry>extract IP address as text</entry>
4794 4795
	<entry><literal>host('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.5</literal></entry>
T
Thomas G. Lockhart 已提交
4796 4797
       </row>
       <row>
4798 4799
	<entry><function>masklen</function>(<type>inet</type>)</entry>
	<entry><type>integer</type></entry>
4800
	<entry>extract netmask length</entry>
4801 4802
	<entry><literal>masklen('192.168.1.5/24')</literal></entry>
	<entry><literal>24</literal></entry>
T
Thomas G. Lockhart 已提交
4803
       </row>
B
Bruce Momjian 已提交
4804
       <row>
4805 4806
	<entry><function>set_masklen</function>(<type>inet</type>,<type>integer</type>)</entry>
	<entry><type>inet</type></entry>
4807
	<entry>set netmask length for <type>inet</type> value</entry>
4808 4809
	<entry><literal>set_masklen('192.168.1.5/24',16)</literal></entry>
	<entry><literal>192.168.1.5/16</literal></entry>
B
Bruce Momjian 已提交
4810
       </row>
T
Thomas G. Lockhart 已提交
4811
       <row>
4812 4813
	<entry><function>netmask</function>(<type>inet</type>)</entry>
	<entry><type>inet</type></entry>
4814
	<entry>construct netmask for network</entry>
4815 4816
	<entry><literal>netmask('192.168.1.5/24')</literal></entry>
	<entry><literal>255.255.255.0</literal></entry>
4817 4818
       </row>
       <row>
4819 4820
	<entry><function>network</function>(<type>inet</type>)</entry>
	<entry><type>cidr</type></entry>
4821
	<entry>extract network part of address</entry>
4822 4823
	<entry><literal>network('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.0/24</literal></entry>
4824 4825
       </row>
       <row>
4826 4827
	<entry><function>text</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
4828
	<entry>extract IP address and masklen as text</entry>
4829 4830
	<entry><literal>text(inet '192.168.1.5')</literal></entry>
	<entry><literal>192.168.1.5/32</literal></entry>
4831
       </row>
4832
       <row>
4833 4834
	<entry><function>abbrev</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
4835
	<entry>extract abbreviated display as text</entry>
4836 4837
	<entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
	<entry><literal>10.1/16</literal></entry>
4838
       </row>
4839 4840 4841 4842 4843 4844
      </tbody>
     </tgroup>
    </table>

   <para>
    All of the functions for <type>inet</type> can be applied to
4845 4846
    <type>cidr</type> values as well.  The <function>host</function>(),
    <function>text</function>(), and <function>abbrev</function>() functions are primarily
B
Bruce Momjian 已提交
4847
    intended to offer alternative display formats. You can cast a text
4848 4849
    field to inet using normal casting syntax: <literal>inet(expression)</literal> or 
    <literal>colname::inet</literal>.
4850 4851 4852
   </para>

    <table tocentry="1" id="macaddr-functions">
4853
     <title><type>macaddr</type> Functions</title>
4854 4855 4856 4857 4858 4859 4860 4861
     <tgroup cols="5">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
	<entry>Result</entry>
T
Thomas G. Lockhart 已提交
4862
       </row>
4863 4864
      </thead>
      <tbody>
4865
       <row>
4866 4867
	<entry><function>trunc</function>(<type>macaddr</type>)</entry>
	<entry><type>macaddr</type></entry>
4868
	<entry>set last 3 bytes to zero</entry>
4869 4870
	<entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
	<entry><literal>12:34:56:00:00:00</literal></entry>
4871
       </row>
T
Thomas G. Lockhart 已提交
4872 4873 4874
      </tbody>
     </tgroup>
    </table>
4875

4876
   <para>
4877
    The function <function>trunc</function>(<type>macaddr</type>) returns a MAC
4878 4879
    address with the last 3 bytes set to 0.  This can be used to
    associate the remaining prefix with a manufacturer.  The directory
4880
    <filename>contrib/mac</filename> in the source distribution contains some
4881
    utilities to create and maintain such an association table.
T
Thomas G. Lockhart 已提交
4882
   </para>
4883

4884
   <para>
4885 4886
    The <type>macaddr</type> type also supports the standard relational
    operators (<literal>&gt;</literal>, <literal>&lt;=</literal>, etc.) for
4887 4888 4889
    lexicographical ordering.
   </para>

T
Thomas G. Lockhart 已提交
4890
  </sect1>
4891

4892

4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909
 <sect1 id="functions-sequence">
  <title>Sequence-Manipulation Functions</title>

  <indexterm>
   <primary>sequences</primary>
  </indexterm>
  <indexterm>
   <primary>nextval</primary>
  </indexterm>
  <indexterm>
   <primary>currval</primary>
  </indexterm>
  <indexterm>
   <primary>setval</primary>
  </indexterm>

   <table>
4910
    <title>Sequence Functions</title>
4911 4912
    <tgroup cols="3">
     <thead>
4913
      <row><entry>Function</entry> <entry>Returns</entry> <entry>Description</entry></row>
4914 4915 4916 4917 4918 4919
     </thead>

     <tbody>
      <row>
	<entry><function>nextval</function>(<type>text</type>)</entry>
	<entry><type>bigint</type></entry>
4920
	<entry>Advance sequence and return new value</entry>
4921 4922 4923 4924
      </row>
      <row>
	<entry><function>currval</function>(<type>text</type>)</entry>
	<entry><type>bigint</type></entry>
4925
	<entry>Return value most recently obtained with <function>nextval</function></entry>
4926 4927 4928 4929
      </row>
      <row>
	<entry><function>setval</function>(<type>text</type>,<type>bigint</type>)</entry>
	<entry><type>bigint</type></entry>
4930
	<entry>Set sequence's current value</entry>
4931 4932
      </row>
      <row>
4933
	<entry><function>setval</function>(<type>text</type>,<type>bigint</type>,<type>boolean</type>)</entry>
4934
	<entry><type>bigint</type></entry>
4935
	<entry>Set sequence's current value and <literal>is_called</literal> flag</entry>
4936 4937 4938 4939 4940 4941
      </row>
     </tbody>
    </tgroup>
   </table>

  <para>
4942
   This section describes <productname>PostgreSQL</productname>'s functions
4943
   for operating on <firstterm>sequence objects</firstterm>.
4944 4945
   Sequence objects (also called sequence generators or
   just sequences) are special single-row tables created with
4946
   <command>CREATE SEQUENCE</command>.  A sequence object is usually used to
4947
   generate unique identifiers for rows of a table.  The sequence functions
P
Peter Eisentraut 已提交
4948
   provide simple, multiuser-safe methods for obtaining successive
4949 4950 4951 4952 4953 4954 4955 4956 4957 4958
   sequence values from sequence objects.
  </para>

  <para>
   For largely historical reasons, the sequence to be operated on by
   a sequence-function call is specified by a text-string argument.
   To achieve some compatibility with the handling of ordinary SQL
   names, the sequence functions convert their argument to lower case
   unless the string is double-quoted.  Thus
<programlisting>
4959 4960 4961
nextval('foo')      <lineannotation>operates on sequence </><literal>foo</literal>
nextval('FOO')      <lineannotation>operates on sequence </><literal>foo</literal>
nextval('"Foo"')    <lineannotation>operates on sequence </><literal>Foo</literal>
4962 4963 4964
</programlisting>
   The sequence name can be schema-qualified if necessary:
<programlisting>
4965 4966 4967 4968
nextval('myschema.foo') <lineannotation>operates on </><literal>myschema.foo</literal>
nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
nextval('foo')      <lineannotation>searches search path for
     </><literal>foo</literal>
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978
</programlisting>
   Of course, the text argument can be the result of an expression,
   not only a simple literal, which is occasionally useful.
  </para>

  <para>
   The available sequence functions are:

    <variablelist>
     <varlistentry>
4979
      <term><function>nextval</function></term>
4980 4981 4982 4983
      <listitem>
       <para>
        Advance the sequence object to its next value and return that
	value.  This is done atomically: even if multiple server processes
4984
	execute <function>nextval</function> concurrently, each will safely receive
4985 4986 4987 4988 4989 4990
	a distinct sequence value.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
4991
      <term><function>currval</function></term>
4992 4993
      <listitem>
       <para>
4994
        Return the value most recently obtained by <function>nextval</function>
4995
	for this sequence in the current server process.  (An error is
4996
	reported if <function>nextval</function> has never been called for this
4997 4998
	sequence in this process.)  Notice that because this is returning
	a process-local value, it gives a predictable answer even if other
4999
	server processes are executing <function>nextval</function> meanwhile.
5000 5001 5002 5003 5004
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
5005
      <term><function>setval</function></term>
5006 5007 5008
      <listitem>
       <para>
        Reset the sequence object's counter value.  The two-parameter
5009 5010 5011
	form sets the sequence's <literal>last_value</literal> field to the specified
	value and sets its <literal>is_called</literal> field to <literal>true</literal>,
	meaning that the next <function>nextval</function> will advance the sequence
5012
	before returning a value.  In the three-parameter form,
5013 5014 5015
	<literal>is_called</literal> may be set either <literal>true</literal> or
	<literal>false</literal>.  If it's set to <literal>false</literal>,
	the next <function>nextval</function> will return exactly the specified
5016
	value, and sequence advancement commences with the following
5017
	<function>nextval</function>.  For example,
5018 5019 5020 5021
       </para>

       <informalexample>
<screen>
5022 5023 5024
SELECT setval('foo', 42);           <lineannotation>Next nextval() will return 43</lineannotation>
SELECT setval('foo', 42, true);     <lineannotation>Same as above</lineannotation>
SELECT setval('foo', 42, false);    <lineannotation>Next nextval() will return 42</lineannotation>
5025 5026 5027 5028
</screen>
       </informalexample>

       <para>
5029
        The result returned by <function>setval</function> is just the value of its
5030 5031 5032 5033 5034 5035 5036 5037 5038 5039
	second argument.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
  </para>

  <important>
   <para>
    To avoid blocking of concurrent transactions that obtain numbers from the
5040
    same sequence, a <function>nextval</function> operation is never rolled back;
5041
    that is, once a value has been fetched it is considered used, even if the
5042
    transaction that did the <function>nextval</function> later aborts.  This means
5043
    that aborted transactions may leave unused <quote>holes</quote> in the
5044
    sequence of assigned values.  <function>setval</function> operations are never
5045 5046 5047 5048 5049 5050
    rolled back, either.
   </para>
  </important>

  <para>
   If a sequence object has been created with default parameters,
5051
   <function>nextval()</function> calls on it will return successive values
5052
   beginning with one.  Other behaviors can be obtained by using
5053
   special parameters in the <command>CREATE SEQUENCE</command> command;
5054 5055 5056 5057 5058 5059
   see its command reference page for more information.
  </para>

 </sect1>


5060 5061 5062
 <sect1 id="functions-conditional">
  <title>Conditional Expressions</title>

5063 5064 5065 5066 5067 5068 5069 5070
  <indexterm>
   <primary>case</primary>
  </indexterm>

  <indexterm>
   <primary>conditionals</primary>
  </indexterm>

5071
  <para>
5072
   This section describes the <acronym>SQL</acronym>-compliant conditional expressions
5073
   available in <productname>PostgreSQL</productname>.
5074 5075 5076 5077 5078 5079 5080 5081 5082 5083
  </para>

  <tip>
   <para>
    If your needs go beyond the capabilities of these conditional
    expressions you might want to consider writing a stored procedure
    in a more expressive programming language.
   </para>
  </tip>

P
Peter Eisentraut 已提交
5084
  <bridgehead renderas="sect2"><literal>CASE</></bridgehead>
5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095

<synopsis>
CASE WHEN <replaceable>condition</replaceable> THEN <replaceable>result</replaceable>
     <optional>WHEN ...</optional>
     <optional>ELSE <replaceable>result</replaceable></optional>
END
</synopsis>

  <para>
   The <acronym>SQL</acronym> <token>CASE</token> expression is a
   generic conditional expression, similar to if/else statements in
5096
   other languages.  <token>CASE</token> clauses can be used wherever
5097
   an expression is valid.  <replaceable>condition</replaceable> is an
5098
   expression that returns a <type>boolean</type> result.  If the result is true
5099 5100 5101 5102 5103 5104 5105
   then the value of the <token>CASE</token> expression is
   <replaceable>result</replaceable>.  If the result is false any
   subsequent <token>WHEN</token> clauses are searched in the same
   manner.  If no <token>WHEN</token>
   <replaceable>condition</replaceable> is true then the value of the
   case expression is the <replaceable>result</replaceable> in the
   <token>ELSE</token> clause.  If the <token>ELSE</token> clause is
P
Peter Eisentraut 已提交
5106
   omitted and no condition matches, the result is null.
5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121
  </para>

  <informalexample>
   <para>
    An example:
<screen>
<prompt>=&gt;</prompt> <userinput>SELECT * FROM test;</userinput>
<computeroutput>
 a
---
 1
 2
 3
</computeroutput>

5122 5123 5124 5125 5126 5127
<prompt>=&gt;</prompt> <userinput>SELECT a,
          CASE WHEN a=1 THEN 'one'
               WHEN a=2 THEN 'two'
               ELSE 'other'
          END
    FROM test;</userinput>
5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139
<computeroutput>
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</computeroutput>
</screen>
   </para>
  </informalexample>

  <para>
5140 5141 5142
   The data types of all the <replaceable>result</replaceable>
   expressions must be coercible to a single output type.
   See <xref linkend="typeconv-union-case"> for more detail.
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
  </para>

<synopsis>
CASE <replaceable>expression</replaceable>
    WHEN <replaceable>value</replaceable> THEN <replaceable>result</replaceable>
    <optional>WHEN ...</optional>
    <optional>ELSE <replaceable>result</replaceable></optional>
END
</synopsis>

  <para>
   This <quote>simple</quote> <token>CASE</token> expression is a
   specialized variant of the general form above.  The
   <replaceable>expression</replaceable> is computed and compared to
   all the <replaceable>value</replaceable>s in the
   <token>WHEN</token> clauses until one is found that is equal.  If
   no match is found, the <replaceable>result</replaceable> in the
P
Peter Eisentraut 已提交
5160
   <token>ELSE</token> clause (or a null value) is returned.  This is similar
5161 5162 5163 5164 5165 5166 5167 5168
   to the <function>switch</function> statement in C.
  </para>

  <informalexample>
   <para>
    The example above can be written using the simple
    <token>CASE</token> syntax:
<screen>
5169 5170 5171 5172 5173 5174
<prompt>=&gt;</prompt> <userinput>SELECT a,
          CASE a WHEN 1 THEN 'one'
                 WHEN 2 THEN 'two'
                 ELSE 'other'
          END
    FROM test;</userinput>
5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185
<computeroutput>
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</computeroutput>
</screen>
    </para>
   </informalexample>

P
Peter Eisentraut 已提交
5186
   <bridgehead renderas="sect2"><literal>COALESCE</></bridgehead>
5187 5188

<synopsis>
5189 5190
<function>COALESCE</function>(<replaceable>value</replaceable>
<optional>, ...</optional>)
5191 5192 5193 5194
</synopsis>

  <para>
   The <function>COALESCE</function> function returns the first of its
P
Peter Eisentraut 已提交
5195 5196
   arguments that is not null.  This is often useful to substitute a
   default value for null values when data is retrieved for display,
5197 5198 5199 5200 5201 5202
   for example:
<programlisting>
SELECT COALESCE(description, short_description, '(none)') ...
</programlisting>
  </para>

P
Peter Eisentraut 已提交
5203
 <bridgehead renderas="sect2"><literal>NULLIF</></bridgehead>
5204

5205 5206 5207 5208
  <indexterm>
   <primary>nullif</primary>
  </indexterm>

5209
<synopsis>
5210 5211
<function>NULLIF</function>(<replaceable>value1</replaceable>,
    <replaceable>value2</replaceable>)
5212 5213 5214
</synopsis>

  <para>
P
Peter Eisentraut 已提交
5215
   The <function>NULLIF</function> function returns a null value if and only
5216 5217 5218 5219 5220 5221 5222 5223 5224
   if <replaceable>value1</replaceable> and
   <replaceable>value2</replaceable> are equal.  Otherwise it returns
   <replaceable>value1</replaceable>.  This can be used to perform the
   inverse operation of the <function>COALESCE</function> example
   given above:
<programlisting>
SELECT NULLIF(value, '(none)') ...
</programlisting>
  </para>
5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237

  <tip>
   <para>
    <function>COALESCE</function> and <function>NULLIF</function> are
    just shorthand for <token>CASE</token> expressions.  They are actually
    converted into <token>CASE</token> expressions at a very early stage
    of processing, and subsequent processing thinks it is dealing with
    <token>CASE</token>.  Thus an incorrect <function>COALESCE</function> or
    <function>NULLIF</function> usage may draw an error message that
    refers to <token>CASE</token>.
   </para>
  </tip>

5238 5239 5240 5241
 </sect1>


  <sect1 id="functions-misc">
5242
   <title>Miscellaneous Functions</title>
5243 5244

   <table>
5245
    <title>Session Information Functions</title>
5246 5247
    <tgroup cols="3">
     <thead>
5248
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5249 5250 5251 5252
     </thead>

     <tbody>
      <row>
5253 5254
       <entry><function>current_user</function></entry>
       <entry><type>name</type></entry>
5255
       <entry>user name of current execution context</entry>
5256 5257
      </row>
      <row>
5258 5259
       <entry><function>session_user</function></entry>
       <entry><type>name</type></entry>
5260
       <entry>session user name</entry>
5261 5262
      </row>
      <row>
5263 5264 5265
       <entry><function>user</function></entry>
       <entry><type>name</type></entry>
       <entry>equivalent to <function>current_user</function></entry>
5266 5267
      </row>
      <row>
5268 5269
       <entry><function>current_schema()</function></entry>
       <entry><type>name</type></entry>
5270 5271 5272
       <entry>name of current schema</entry>
      </row>
      <row>
5273
       <entry><function>current_schemas(boolean)</function></entry>
5274
       <entry><type>name[]</type></entry>
5275
       <entry>names of schemas in search path optionally including implicit schemas</entry>
5276
      </row>
B
 
Bruce Momjian 已提交
5277 5278 5279 5280 5281
      <row>
       <entry><function>current_database()</function></entry>
       <entry><type>name</type></entry>
       <entry>name of current database</entry>
      </row>
5282 5283 5284 5285
     </tbody>
    </tgroup>
   </table>

5286 5287 5288 5289 5290
   <indexterm zone="functions-misc">
    <primary>user</primary>
    <secondary>current</secondary>
   </indexterm>

5291 5292 5293 5294 5295 5296 5297 5298 5299 5300
   <indexterm zone="functions-misc">
    <primary>schema</primary>
    <secondary>current</secondary>
   </indexterm>

   <indexterm zone="functions-misc">
    <primary>search path</primary>
    <secondary>current</secondary>
   </indexterm>

5301
   <para>
5302
    The <function>session_user</function> is the user that initiated a
5303
    database connection; it is fixed for the duration of that
5304
    connection. The <function>current_user</function> is the user identifier
5305 5306 5307
    that is applicable for permission checking. Normally, it is equal
    to the session user, but it changes during the execution of
    functions with the attribute <literal>SECURITY DEFINER</literal>.
5308 5309
    In Unix parlance, the session user is the <quote>real user</quote> and
    the current user is the <quote>effective user</quote>.
5310 5311
   </para>

5312 5313
   <note>
    <para>
5314 5315
     <function>current_user</function>, <function>session_user</function>, and
     <function>user</function> have special syntactic status in <acronym>SQL</acronym>:
5316 5317 5318
     they must be called without trailing parentheses.
    </para>
   </note>
5319 5320

   <note>
5321
    <title>Deprecated</title>
5322
    <para>
5323 5324
     The function <function>getpgusername()</function> is an obsolete equivalent
     of <function>current_user</function>.
5325 5326
    </para>
   </note>
5327

5328
   <para>
5329
    <function>current_schema</function> returns the name of the schema that is
P
Peter Eisentraut 已提交
5330
    at the front of the search path (or a null value if the search path is
5331 5332
    empty).  This is the schema that will be used for any tables or
    other named objects that are created without specifying a target schema.
5333 5334 5335 5336
    <function>current_schemas(boolean)</function> returns an array of the names of all
    schemas presently in the search path.  The boolean option determines whether or not
    implicitly included system schemas such as pg_catalog are included in the search 
    path returned.
5337 5338
   </para>

5339 5340 5341 5342 5343
   <para id=set-search-path xreflabel="SET SEARCH_PATH">
    <indexterm>
     <primary>search path</primary>
     <secondary>changing at runtime</secondary>
    </indexterm>
P
Peter Eisentraut 已提交
5344
    The search path may be altered by a run-time setting.  The
5345 5346 5347 5348 5349
    command to use is <command>
     SET SEARCH_PATH '<varname>schema</varname>'[,'<varname>schema</varname>']...
    </command>
   </para>

5350
   <table>
5351
    <title>System Information Functions</title>
5352 5353
    <tgroup cols="3">
     <thead>
5354
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5355 5356 5357 5358
     </thead>

     <tbody>
      <row>
5359 5360 5361
       <entry><function>version</function></entry>
       <entry><type>text</type></entry>
       <entry>PostgreSQL version information</entry>
5362 5363 5364 5365 5366 5367 5368 5369 5370 5371
      </row>
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
    <primary>version</primary>
   </indexterm>

   <para>
5372
    <function>version()</function> returns a string describing the PostgreSQL
5373 5374 5375
    server's version.
   </para>

5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436
   <table>
    <title>Configuration Settings Information Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>

     <tbody>
      <row>
       <entry>
        <function>current_setting</function>(<parameter>setting_name</parameter>)
       </entry>
       <entry><type>text</type></entry>
       <entry>value of current setting</entry>
      </row>
      <row>
       <entry>
        <function>set_config(<parameter>setting_name</parameter>,
                             <parameter>new_value</parameter>,
                             <parameter>is_local</parameter>)</function>
       </entry>
       <entry><type>text</type></entry>
       <entry>new value of current setting</entry>
      </row>
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
    <primary>setting</primary>
    <secondary>current</secondary>
   </indexterm>

   <indexterm zone="functions-misc">
    <primary>setting</primary>
    <secondary>set</secondary>
   </indexterm>

   <para>
    The <function>current_setting</function> is used to obtain the current
    value of the <parameter>setting_name</parameter> setting, as a query
    result. It is the equivalent to the SQL <command>SHOW</command> command.
    For example:
<programlisting>
select current_setting('DateStyle');
            current_setting
---------------------------------------
 ISO with US (NonEuropean) conventions
(1 row)
</programlisting>
   </para>

   <para>
    <function>set_config</function> allows the <parameter>setting_name
    </parameter> setting to be changed to <parameter>new_value</parameter>.
    If <parameter>is_local</parameter> is set to <literal>true</literal>,
    the new value will only apply to the current transaction. If you want
    the new value to apply for the current session, use
    <literal>false</literal> instead. It is the equivalent to the SQL
    <command>SET</command> command. For example:
<programlisting>
5437
select set_config('show_statement_stats','off','f');
5438 5439 5440 5441 5442 5443 5444
 set_config
------------
 off
(1 row)
</programlisting>
   </para>

5445
   <table>
5446
    <title>Access Privilege Inquiry Functions</title>
5447 5448
    <tgroup cols="3">
     <thead>
5449
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5450 5451 5452 5453
     </thead>

     <tbody>
      <row>
5454
       <entry><function>has_table_privilege</function>(<parameter>user</parameter>,
5455 5456 5457
                                  <parameter>table</parameter>,
                                  <parameter>access</parameter>)
       </entry>
5458 5459
       <entry><type>boolean</type></entry>
       <entry>does user have access to table</entry>
5460 5461
      </row>
      <row>
5462
       <entry><function>has_table_privilege</function>(<parameter>table</parameter>,
5463 5464
                                  <parameter>access</parameter>)
       </entry>
5465 5466
       <entry><type>boolean</type></entry>
       <entry>does current user have access to table</entry>
5467
      </row>
5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527
      <row>
       <entry><function>has_database_privilege</function>(<parameter>user</parameter>,
                                  <parameter>database</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have access to database</entry>
      </row>
      <row>
       <entry><function>has_database_privilege</function>(<parameter>database</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have access to database</entry>
      </row>
      <row>
       <entry><function>has_function_privilege</function>(<parameter>user</parameter>,
                                  <parameter>function</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have access to function</entry>
      </row>
      <row>
       <entry><function>has_function_privilege</function>(<parameter>function</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have access to function</entry>
      </row>
      <row>
       <entry><function>has_language_privilege</function>(<parameter>user</parameter>,
                                  <parameter>language</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have access to language</entry>
      </row>
      <row>
       <entry><function>has_language_privilege</function>(<parameter>language</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have access to language</entry>
      </row>
      <row>
       <entry><function>has_schema_privilege</function>(<parameter>user</parameter>,
                                  <parameter>schema</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have access to schema</entry>
      </row>
      <row>
       <entry><function>has_schema_privilege</function>(<parameter>schema</parameter>,
                                  <parameter>access</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have access to schema</entry>
      </row>
5528 5529 5530 5531 5532 5533 5534
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
    <primary>has_table_privilege</primary>
   </indexterm>
5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546
   <indexterm zone="functions-misc">
    <primary>has_database_privilege</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>has_function_privilege</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>has_language_privilege</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>has_schema_privilege</primary>
   </indexterm>
5547 5548

   <para>
5549
    <function>has_table_privilege</function> checks whether a user
5550
    can access a table in a particular way.  The user can be
5551
    specified by name or by ID
5552
    (<classname>pg_user</classname>.<structfield>usesysid</structfield>), or if the argument is
5553
    omitted
5554
    <function>current_user</function> is assumed.  The table can be specified
5555
    by name or by OID.  (Thus, there are actually six variants of
5556
    <function>has_table_privilege</function>, which can be distinguished by
5557 5558 5559
    the number and types of their arguments.)  When specifying by name,
    the name can be schema-qualified if necessary.
    The desired access type
5560
    is specified by a text string, which must evaluate to one of the
5561 5562 5563
    values <literal>SELECT</literal>, <literal>INSERT</literal>, <literal>UPDATE</literal>,
    <literal>DELETE</literal>, <literal>RULE</literal>, <literal>REFERENCES</literal>, or
    <literal>TRIGGER</literal>.  (Case of the string is not significant, however.)
5564 5565 5566 5567
    An example is:
<programlisting>
SELECT has_table_privilege('myschema.mytable', 'select');
</programlisting>
5568 5569
   </para>

5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585
   <para>
    <function>has_database_privilege</function> checks whether a user
    can access a database in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    The desired access type must evaluate to
    <literal>CREATE</literal>,
    <literal>TEMPORARY</literal>, or
    <literal>TEMP</literal> (which is equivalent to
    <literal>TEMPORARY</literal>).
   </para>

   <para>
    <function>has_function_privilege</function> checks whether a user
    can access a function in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    When specifying a function by a text string rather than by OID,
P
Peter Eisentraut 已提交
5586
    the allowed input is the same as for the <type>regprocedure</> data type.
5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687
    The desired access type must currently evaluate to
    <literal>EXECUTE</literal>.
   </para>

   <para>
    <function>has_language_privilege</function> checks whether a user
    can access a procedural language in a particular way.  The possibilities
    for its arguments are analogous to <function>has_table_privilege</function>.
    The desired access type must currently evaluate to
    <literal>USAGE</literal>.
   </para>

   <para>
    <function>has_schema_privilege</function> checks whether a user
    can access a schema in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    The desired access type must evaluate to
    <literal>CREATE</literal> or
    <literal>USAGE</literal>.
   </para>

   <table>
    <title>Schema Visibility Inquiry Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>

     <tbody>
      <row>
       <entry><function>pg_table_is_visible</function>(<parameter>tableOID</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is table visible in search path</entry>
      </row>
      <row>
       <entry><function>pg_type_is_visible</function>(<parameter>typeOID</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is type visible in search path</entry>
      </row>
      <row>
       <entry><function>pg_function_is_visible</function>(<parameter>functionOID</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is function visible in search path</entry>
      </row>
      <row>
       <entry><function>pg_operator_is_visible</function>(<parameter>operatorOID</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is operator visible in search path</entry>
      </row>
      <row>
       <entry><function>pg_opclass_is_visible</function>(<parameter>opclassOID</parameter>)
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is operator class visible in search path</entry>
      </row>
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
    <primary>pg_table_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>pg_type_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>pg_function_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>pg_operator_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-misc">
    <primary>pg_opclass_is_visible</primary>
   </indexterm>

   <para>
    <function>pg_table_is_visible</function> checks whether a table
    (or view, or any other kind of <structname>pg_class</> entry) is
    <firstterm>visible</> in the current schema search path.  A table
    is said to be visible if its containing schema is in the search path
    and no table of the same name appears earlier in the search path.
    This is equivalent to the statement that the table can be referenced
    by name without explicit schema qualification.
    For example, to list the names of all visible tables:
<programlisting>
SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);
</programlisting>
   </para>

   <para>
    <function>pg_type_is_visible</function>,
    <function>pg_function_is_visible</function>,
    <function>pg_operator_is_visible</function>, and
    <function>pg_opclass_is_visible</function> perform the same sort of
    visibility check for types, functions, operators, and operator classes,
    respectively.  For functions and operators, an object in the search path
    is visible if there is no object of the same name <emphasis>and argument
P
Peter Eisentraut 已提交
5688
    data type(s)</> earlier in the path.  For operator classes,
5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703
    both name and associated index access method are considered.
   </para>

   <para>
    All these functions require object OIDs to identify the object to be
    checked.  If you want to test an object by name, it is convenient to use
    the OID alias types (<type>regclass</>, <type>regtype</>,
    <type>regprocedure</>, or <type>regoperator</>), for example
<programlisting>
SELECT pg_type_is_visible('myschema.widget'::regtype);
</programlisting>
    Note that it would not make much sense to test an unqualified name in
    this way --- if the name can be recognized at all, it must be visible.
   </para>

5704
   <table>
5705
    <title>Catalog Information Functions</title>
5706 5707
    <tgroup cols="3">
     <thead>
5708
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5709 5710 5711 5712
     </thead>

     <tbody>
      <row>
5713 5714
       <entry><function>pg_get_viewdef</function>(<parameter>viewname</parameter>)</entry>
       <entry><type>text</type></entry>
5715
       <entry>Get CREATE VIEW command for view (<emphasis>deprecated</emphasis>)</entry>
5716 5717
      </row>
      <row>
5718 5719 5720
       <entry><function>pg_get_viewdef</function>(<parameter>viewOID</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get CREATE VIEW command for view</entry>
5721 5722
      </row>
      <row>
5723 5724 5725
       <entry><function>pg_get_ruledef</function>(<parameter>ruleOID</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get CREATE RULE command for rule</entry>
5726 5727
      </row>
      <row>
5728 5729 5730
       <entry><function>pg_get_indexdef</function>(<parameter>indexOID</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get CREATE INDEX command for index</entry>
5731
      </row>
5732 5733 5734 5735 5736
      <row>
       <entry><function>pg_get_constraintdef</function>(<parameter>constraintOID</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get definition of a constraint</entry>
      </row>
5737
      <row>
5738 5739 5740
       <entry><function>pg_get_userbyid</function>(<parameter>userid</parameter>)</entry>
       <entry><type>name</type></entry>
       <entry>Get user name given ID</entry>
5741 5742 5743 5744 5745 5746
      </row>
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757
    <primary>pg_get_viewdef</primary>
   </indexterm>

   <indexterm zone="functions-misc">
    <primary>pg_get_ruledef</primary>
   </indexterm>

   <indexterm zone="functions-misc">
    <primary>pg_get_indexdef</primary>
   </indexterm>

5758 5759 5760 5761
   <indexterm zone="functions-misc">
    <primary>pg_get_constraintdef</primary>
   </indexterm>

5762 5763
   <indexterm zone="functions-misc">
    <primary>pg_get_userbyid</primary>
5764 5765 5766
   </indexterm>

   <para>
5767
    These functions extract information from the system catalogs.
5768 5769 5770 5771 5772 5773
    <function>pg_get_viewdef()</function>,
    <function>pg_get_ruledef()</function>,
    <function>pg_get_indexdef()</function>, and
    <function>pg_get_constraintdef()</function> respectively reconstruct the
    creating command for a view, rule, index, or constraint.
    (Note that this is a decompiled
5774
    reconstruction, not the verbatim text of the command.)
5775 5776
    At present <function>pg_get_constraintdef()</function> only works for
    foreign-key constraints.
5777 5778
    <function>pg_get_userbyid()</function> extracts a user's name given a
    <structfield>usesysid</structfield> value.
5779 5780
   </para>

5781
   <table>
5782
    <title>Comment Information Functions</title>
5783 5784
    <tgroup cols="3">
     <thead>
5785
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5786 5787 5788 5789
     </thead>

     <tbody>
      <row>
5790 5791 5792
       <entry><function>obj_description</function>(<parameter>objectOID</parameter>, <parameter>tablename</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get comment for a database object</entry>
5793 5794
      </row>
      <row>
5795 5796 5797
       <entry><function>obj_description</function>(<parameter>objectOID</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get comment for a database object (<emphasis>deprecated</emphasis>)</entry>
5798 5799
      </row>
      <row>
5800 5801 5802
       <entry><function>col_description</function>(<parameter>tableOID</parameter>, <parameter>columnnumber</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>Get comment for a table column</entry>
5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817
      </row>
     </tbody>
    </tgroup>
   </table>

   <indexterm zone="functions-misc">
    <primary>obj_description</primary>
   </indexterm>

   <indexterm zone="functions-misc">
    <primary>col_description</primary>
   </indexterm>

   <para>
    These functions extract comments previously stored with the
P
Peter Eisentraut 已提交
5818
    <command>COMMENT</command> command.  A null value is returned if
5819 5820 5821 5822
    no comment can be found matching the specified parameters.
   </para>

   <para>
5823
    The two-parameter form of <function>obj_description()</function> returns the
5824 5825
    comment for a database object specified by its OID and the name of the
    containing system catalog.  For example,
5826
    <literal>obj_description(123456,'pg_class')</literal>
5827
    would retrieve the comment for a table with OID 123456.
5828
    The one-parameter form of <function>obj_description()</function> requires only
5829 5830 5831 5832 5833 5834
    the object OID.  It is now deprecated since there is no guarantee that
    OIDs are unique across different system catalogs; therefore, the wrong
    comment could be returned.
   </para>

   <para>
5835
    <function>col_description()</function> returns the comment for a table column,
5836
    which is specified by the OID of its table and its column number.
5837
    <function>obj_description()</function> cannot be used for table columns since
5838 5839 5840
    columns do not have OIDs of their own.
   </para>

5841 5842
  </sect1>

5843

5844 5845
 <sect1 id="functions-aggregate">
  <title>Aggregate Functions</title>
5846

5847 5848
  <note>
   <title>Author</title>
5849
   <para>
5850
    Written by Isaac Wilcox <email>isaac@azartmedia.com</email> on 2000-06-16
5851
   </para>
5852
  </note>
5853

5854 5855 5856 5857
  <para>
   <firstterm>Aggregate functions</firstterm> compute a single result
   value from a set of input values.  The special syntax
   considerations for aggregate functions are explained in <xref
5858
                                                                 linkend="syntax-aggregates">.  Consult the <citetitle>PostgreSQL
5859 5860
   Tutorial</citetitle> for additional introductory information.
  </para>
5861

5862 5863
  <table tocentry="1">
   <title>Aggregate Functions</title>
5864

5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875
   <tgroup cols="3">
    <thead>
     <row>
      <entry>Function</entry>
      <entry>Description</entry>
      <entry>Notes</entry>
     </row>
    </thead>

    <tbody>
     <row>
5876
      <entry>avg(<replaceable class="parameter">expression</replaceable>)</entry>
5877 5878
      <entry>the average (arithmetic mean) of all input values</entry>
      <entry>
5879 5880 5881 5882
       <indexterm>
        <primary>average</primary>
        <secondary>function</secondary>
       </indexterm>
5883 5884 5885 5886 5887
       Finding the average value is available on the following data
       types: <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, <type>numeric</type>, <type>interval</type>.
       The result is of type <type>numeric</type> for any integer type
P
Peter Eisentraut 已提交
5888
       input, <type>double precision</type> for floating-point input,
5889 5890 5891 5892 5893
       otherwise the same as the input data type.
      </entry>
     </row>

     <row>
5894
      <entry><function>count</function>(*)</entry>
5895
      <entry>number of input values</entry>
5896
      <entry>The return value is of type <type>bigint</type>.</entry>
5897 5898 5899
     </row>

     <row>
5900
      <entry><function>count</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5901 5902
      <entry>
       Counts the input values for which the value of <replaceable
P
Peter Eisentraut 已提交
5903
                                                                   class="parameter">expression</replaceable> is not null.
5904
      </entry>
5905
      <entry>The return value is of type <type>bigint</type>.</entry>
5906 5907 5908
     </row>

     <row>
5909
      <entry><function>max</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5910 5911 5912 5913 5914 5915 5916 5917
      <entry>the maximum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
      <entry>
       Available for all numeric, string, and date/time types.  The
       result has the same type as the input expression.
      </entry>
     </row>

     <row>
5918
      <entry><function>min</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5919 5920 5921 5922 5923 5924 5925 5926
      <entry>the minimum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
      <entry>
       Available for all numeric, string, and date/time types.  The
       result has the same type as the input expression.
      </entry>
     </row>

     <row>
5927 5928
      <entry><function>stddev</function>(<replaceable
                                                      class="parameter">expression</replaceable>)</entry>
5929 5930
      <entry>the sample standard deviation of the input values</entry>
      <entry>
5931 5932 5933
       <indexterm>
        <primary>standard deviation</primary>
       </indexterm>
5934 5935 5936 5937
       Finding the standard deviation is available on the following
       data types: <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, <type>numeric</type>.  The result is of type
P
Peter Eisentraut 已提交
5938
       <type>double precision</type> for floating-point input,
5939 5940 5941 5942 5943
       otherwise <type>numeric</type>.
      </entry>
     </row>

     <row>
5944
      <entry><function>sum</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5945 5946 5947 5948 5949 5950
      <entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
      <entry>
       Summation is available on the following data types:
       <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, <type>numeric</type>, <type>interval</type>.
5951 5952 5953
       The result is of type <type>bigint</type> for <type>smallint</type>
       or <type>integer</type> input, <type>numeric</type> for
       <type>bigint</type> 
P
Peter Eisentraut 已提交
5954
       input, <type>double precision</type> for floating-point input,
5955 5956 5957 5958 5959
       otherwise the same as the input data type.
      </entry>
     </row>

     <row>
5960 5961
      <entry><function>variance</function>(<replaceable
                                                        class="parameter">expression</replaceable>)</entry>
5962 5963
      <entry>the sample variance of the input values</entry>
      <entry>
5964 5965 5966
       <indexterm>
        <primary>variance</primary>
       </indexterm>
5967
       The variance is the square of the standard deviation.  The
5968 5969
       supported data types and result types are the same as for
       standard deviation.
5970 5971 5972 5973 5974 5975
      </entry>
     </row>

    </tbody>
   </tgroup>
  </table>
5976

5977 5978
  <para>
   It should be noted that except for <function>COUNT</function>,
P
Peter Eisentraut 已提交
5979 5980
   these functions return a null value when no rows are selected.  In
   particular, <function>SUM</function> of no rows returns null, not
5981
   zero as one might expect.  <function>COALESCE</function> may be
P
Peter Eisentraut 已提交
5982
   used to substitute zero for null when necessary.
5983
  </para>
5984

5985
 </sect1>
5986

5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032

 <sect1 id="functions-subquery">
  <title>Subquery Expressions</title>

  <indexterm>
   <primary>exists</primary>
  </indexterm>

  <indexterm>
   <primary>in</primary>
  </indexterm>

  <indexterm>
   <primary>not in</primary>
  </indexterm>

  <indexterm>
   <primary>any</primary>
  </indexterm>

  <indexterm>
   <primary>all</primary>
  </indexterm>

  <indexterm>
   <primary>some</primary>
  </indexterm>

  <indexterm>
   <primary>subqueries</primary>
  </indexterm>

  <para>
   This section describes the <acronym>SQL</acronym>-compliant subquery
   expressions available in <productname>PostgreSQL</productname>.
   All of the expression forms documented in this section return
   Boolean (true/false) results.
  </para>

  <bridgehead renderas="sect2">EXISTS</bridgehead>

<synopsis>
EXISTS ( <replaceable>subquery</replaceable> )
</synopsis>

  <para>
6033 6034
   The argument of <token>EXISTS</token> is an arbitrary SELECT statement,
   or <firstterm>subquery</firstterm>.  The
6035
   subquery is evaluated to determine whether it returns any rows.
6036 6037
   If it returns at least one row, the result of <token>EXISTS</token> is
   TRUE; if the subquery returns no rows, the result of <token>EXISTS</token> 
6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058
   is FALSE.
  </para>

  <para>
   The subquery can refer to variables from the surrounding query,
   which will act as constants during any one evaluation of the subquery.
  </para>

  <para>
   The subquery will generally only be executed far enough to determine
   whether at least one row is returned, not all the way to completion.
   It is unwise to write a subquery that has any side-effects (such as
   calling sequence functions); whether the side-effects occur or not
   may be difficult to predict.
  </para>

  <para>
   Since the result depends only on whether any rows are returned,
   and not on the contents of those rows, the output list of the
   subquery is normally uninteresting.  A common coding convention is
   to write all EXISTS tests in the form
6059 6060
   <literal>EXISTS(SELECT 1 WHERE ...)</literal>.  There are exceptions to
   this rule however, such as subqueries that use <token>INTERSECT</token>.
6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075
  </para>

  <para>
   This simple example is like an inner join on col2, but it produces at
   most one output row for each tab1 row, even if there are multiple matching
   tab2 rows:
<screen>
SELECT col1 FROM tab1
    WHERE EXISTS(SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
</screen>
  </para>

   <bridgehead renderas="sect2">IN (scalar form)</bridgehead>

<synopsis>
6076
<replaceable>expression</replaceable> IN
6077
 <replaceable>value</replaceable><optional>, ...</optional>)
6078 6079 6080
</synopsis>

  <para>
6081
   The right-hand side of this form of <token>IN</token> is a parenthesized list
6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095
   of scalar expressions.  The result is TRUE if the left-hand expression's
   result is equal to any of the right-hand expressions.  This is a shorthand
   notation for

<synopsis>
<replaceable>expression</replaceable> = <replaceable>value1</replaceable>
OR
<replaceable>expression</replaceable> = <replaceable>value2</replaceable>
OR
...
</synopsis>

   Note that if the left-hand expression yields NULL, or if there are
   no equal right-hand values and at least one right-hand expression yields
6096
   NULL, the result of the <token>IN</token> construct will be NULL, not FALSE.
6097 6098 6099 6100 6101 6102
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <note>
  <para>
6103 6104
   This form of <token>IN</token> is not truly a subquery expression, but it
   seems best to document it in the same place as subquery <token>IN</token>.
6105 6106 6107 6108 6109 6110 6111 6112 6113 6114
  </para>
  </note>

   <bridgehead renderas="sect2">IN (subquery form)</bridgehead>

<synopsis>
<replaceable>expression</replaceable> IN (<replaceable>subquery</replaceable>)
</synopsis>

  <para>
6115
   The right-hand side of this form of <token>IN</token> is a parenthesized
6116 6117
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result.
6118
   The result of <token>IN</token> is TRUE if any equal subquery row is found.
6119 6120 6121 6122 6123 6124 6125
   The result is FALSE if no equal row is found (including the special
   case where the subquery returns no rows).
  </para>

  <para>
   Note that if the left-hand expression yields NULL, or if there are
   no equal right-hand values and at least one right-hand row yields
6126
   NULL, the result of the <token>IN</token> construct will be NULL, not FALSE.
6127 6128 6129 6130 6131
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <para>
6132
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6133 6134 6135 6136
   be evaluated completely.
  </para>

<synopsis>
6137
(<replaceable>expression</replaceable>,
6138
<replaceable>expres</replaceable><optional>,</optional>)
6139
    IN (<replaceable>subquery</replaceable>)
6140 6141 6142
</synopsis>

  <para>
6143
   The right-hand side of this form of <token>IN</token> is a parenthesized
6144 6145 6146
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand list.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result.
6147
   The result of <token>IN</token> is TRUE if any equal subquery row is found.
6148 6149 6150 6151 6152
   The result is FALSE if no equal row is found (including the special
   case where the subquery returns no rows).
  </para>

  <para>
P
Peter Eisentraut 已提交
6153
   As usual, null values in the expressions or subquery rows are combined per
P
Peter Eisentraut 已提交
6154
   the normal rules of SQL Boolean expressions.  Two rows are considered
6155 6156 6157 6158
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (NULL).
   If all the row results are either unequal or NULL, with at least one NULL,
6159
   then the result of <token>IN</token> is NULL.
6160 6161 6162 6163 6164
  </para>

   <bridgehead renderas="sect2">NOT IN (scalar form)</bridgehead>

<synopsis>
6165
<replaceable>expression</replaceable> NOT IN
6166
 <replaceable>value</replaceable><optional>, ...</optional>)
6167 6168 6169
</synopsis>

  <para>
6170
   The right-hand side of this form of <token>NOT IN</token> is a parenthesized list
6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
   of scalar expressions.  The result is TRUE if the left-hand expression's
   result is unequal to all of the right-hand expressions.  This is a shorthand
   notation for

<synopsis>
<replaceable>expression</replaceable> &lt;&gt; <replaceable>value1</replaceable>
AND
<replaceable>expression</replaceable> &lt;&gt; <replaceable>value2</replaceable>
AND
...
</synopsis>

   Note that if the left-hand expression yields NULL, or if there are
   no equal right-hand values and at least one right-hand expression yields
6185
   NULL, the result of the <token>NOT IN</token> construct will be NULL, not TRUE
6186 6187 6188 6189 6190 6191 6192
   as one might naively expect.
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <tip>
  <para>
6193
   <literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
P
Peter Eisentraut 已提交
6194
   cases.  However, null values are much more likely to trip up the novice when
6195
   working with <token>NOT IN</token> than when working with <token>IN</token>.
6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206
   It's best to express your condition positively if possible.
  </para>
  </tip>

   <bridgehead renderas="sect2">NOT IN (subquery form)</bridgehead>

<synopsis>
<replaceable>expression</replaceable> NOT IN (<replaceable>subquery</replaceable>)
</synopsis>

  <para>
6207
   The right-hand side of this form of <token>NOT IN</token> is a parenthesized
6208 6209
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result.
6210
   The result of <token>NOT IN</token> is TRUE if only unequal subquery rows
6211 6212 6213 6214 6215 6216 6217
   are found (including the special case where the subquery returns no rows).
   The result is FALSE if any equal row is found.
  </para>

  <para>
   Note that if the left-hand expression yields NULL, or if there are
   no equal right-hand values and at least one right-hand row yields
6218
   NULL, the result of the <token>NOT IN</token> construct will be NULL, not TRUE.
6219 6220 6221 6222 6223
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <para>
6224
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6225 6226 6227 6228
   be evaluated completely.
  </para>

<synopsis>
6229
(<replaceable>expression</replaceable>,
6230
<replaceable>expres</replaceable><optional>,</optional>)
6231
    NOT IN (<replaceable>subquery</replaceable>)
6232 6233 6234
</synopsis>

  <para>
6235
   The right-hand side of this form of <token>NOT IN</token> is a parenthesized
6236 6237 6238
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand list.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result.
6239
   The result of <token>NOT IN</token> is TRUE if only unequal subquery rows
6240 6241 6242 6243 6244
   are found (including the special case where the subquery returns no rows).
   The result is FALSE if any equal row is found.
  </para>

  <para>
P
Peter Eisentraut 已提交
6245
   As usual, null values in the expressions or subquery rows are combined per
P
Peter Eisentraut 已提交
6246
   the normal rules of SQL Boolean expressions.  Two rows are considered
6247 6248 6249 6250
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (NULL).
   If all the row results are either unequal or NULL, with at least one NULL,
6251
   then the result of <token>NOT IN</token> is NULL.
6252 6253 6254 6255 6256
  </para>

   <bridgehead renderas="sect2">ANY</bridgehead>

<synopsis>
6257
<replaceable>expression</replaceable>
6258
<replaceable>oper</replaceable> ANY (<replaceable>subquery</replaceable>)
6259
<replaceable>expression</replaceable>
6260
<replaceable>oper</replaceable> SOME (<replaceable>subquery</replaceable>)
6261 6262 6263
</synopsis>

  <para>
6264
   The right-hand side of this form of <token>ANY</token> is a parenthesized
6265 6266
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result using the
P
Peter Eisentraut 已提交
6267
   given <replaceable>operator</replaceable>, which must yield a Boolean
6268
   result.
6269
   The result of <token>ANY</token> is TRUE if any true result is obtained.
6270 6271 6272 6273 6274
   The result is FALSE if no true result is found (including the special
   case where the subquery returns no rows).
  </para>

  <para>
6275 6276
   <token>SOME</token> is a synonym for <token>ANY</token>.
   <token>IN</token> is equivalent to <literal>= ANY</literal>.
6277 6278 6279 6280
  </para>

  <para>
   Note that if there are no successes and at least one right-hand row yields
6281
   NULL for the operator's result, the result of the <token>ANY</token> construct
6282 6283 6284 6285 6286 6287
   will be NULL, not FALSE.
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <para>
6288
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6289 6290 6291 6292
   be evaluated completely.
  </para>

<synopsis>
6293
(<replaceable>expression</replaceable>,
6294
<replaceable>expres</replaceable><optional>,</optional>optiona<replaceable></replaceable> ANY (<replaceable>subquery</replaceable>)
6295
(<replaceable>expression</replaceable>,
6296
<replaceable>expres</replaceable><optional>,</optional>optiona<replaceable></replaceable> SOME (<replaceable>subquery</replaceable>)
6297 6298 6299
</synopsis>

  <para>
6300
   The right-hand side of this form of <token>ANY</token> is a parenthesized
6301 6302 6303 6304
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand list.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result,
   using the given <replaceable>operator</replaceable>.  Presently,
6305 6306 6307
   only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise <token>ANY</token> queries.
   The result of <token>ANY</token> is TRUE if any equal or unequal row is
6308 6309 6310 6311 6312 6313
   found, respectively.
   The result is FALSE if no such row is found (including the special
   case where the subquery returns no rows).
  </para>

  <para>
P
Peter Eisentraut 已提交
6314
   As usual, null values in the expressions or subquery rows are combined per
P
Peter Eisentraut 已提交
6315
   the normal rules of SQL Boolean expressions.  Two rows are considered
6316 6317 6318
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (NULL).
6319
   If there is at least one NULL row result, then the result of <token>ANY</token>
6320 6321 6322 6323 6324 6325
   cannot be FALSE; it will be TRUE or NULL. 
  </para>

   <bridgehead renderas="sect2">ALL</bridgehead>

<synopsis>
6326
<replaceable>expression</replaceable>
6327
<replaceable>oper</replaceable> ALL (<replaceable>subquery</replaceable>)
6328 6329 6330
</synopsis>

  <para>
6331
   The right-hand side of this form of <token>ALL</token> is a parenthesized
6332 6333
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result using the
P
Peter Eisentraut 已提交
6334
   given <replaceable>operator</replaceable>, which must yield a Boolean
6335
   result.
6336
   The result of <token>ALL</token> is TRUE if all rows yield TRUE
6337 6338 6339 6340 6341
   (including the special case where the subquery returns no rows).
   The result is FALSE if any false result is found.
  </para>

  <para>
6342
   <token>NOT IN</token> is equivalent to <literal>&lt;&gt; ALL</literal>.
6343 6344 6345 6346
  </para>

  <para>
   Note that if there are no failures but at least one right-hand row yields
6347
   NULL for the operator's result, the result of the <token>ALL</token> construct
6348 6349 6350 6351 6352 6353
   will be NULL, not TRUE.
   This is in accordance with SQL's normal rules for Boolean combinations
   of NULL values.
  </para>

  <para>
6354
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6355 6356 6357
   be evaluated completely.
  </para>

6358
   <synopsis>
6359
(<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
6360
   </synopsis>
6361 6362

  <para>
6363
   The right-hand side of this form of <token>ALL</token> is a parenthesized
6364 6365 6366 6367
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand list.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result,
   using the given <replaceable>operator</replaceable>.  Presently,
6368 6369 6370
   only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise <token>ALL</token> queries.
   The result of <token>ALL</token> is TRUE if all subquery rows are equal
6371 6372 6373 6374 6375 6376 6377
   or unequal, respectively (including the special
   case where the subquery returns no rows).
   The result is FALSE if any row is found to be unequal or equal,
   respectively.
  </para>

  <para>
P
Peter Eisentraut 已提交
6378
   As usual, null values in the expressions or subquery rows are combined per
P
Peter Eisentraut 已提交
6379
   the normal rules of SQL Boolean expressions.  Two rows are considered
6380 6381 6382
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (NULL).
6383
   If there is at least one NULL row result, then the result of <token>ALL</token>
6384 6385 6386 6387 6388
   cannot be TRUE; it will be FALSE or NULL. 
  </para>

   <bridgehead renderas="sect2">Row-wise comparison</bridgehead>

6389
   <synopsis>
6390
(<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> (<replaceable>subquery</replaceable>)
6391 6392
(<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> (<replaceable>expression</replaceable> <replaceable>expression</replaceable><optional>, ...</optional>)
   </synopsis>
6393 6394 6395 6396 6397 6398 6399 6400 6401

  <para>
   The left-hand side is a list of scalar expressions.  The right-hand side
   can be either a list of scalar expressions of the same length, or a
   parenthesized subquery, which must return exactly as many columns as there
   are expressions on the left-hand side.  Furthermore, the subquery cannot
   return more than one row.  (If it returns zero rows, the result is taken to
   be NULL.)  The left-hand side is evaluated and compared row-wise to the
   single subquery result row, or to the right-hand expression list.
6402
   Presently, only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
6403 6404 6405 6406 6407
   in row-wise comparisons.
   The result is TRUE if the two rows are equal or unequal, respectively.
  </para>

  <para>
P
Peter Eisentraut 已提交
6408
   As usual, null values in the expressions or subquery rows are combined per
P
Peter Eisentraut 已提交
6409
   the normal rules of SQL Boolean expressions.  Two rows are considered
6410 6411 6412 6413 6414 6415 6416
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of the row comparison is unknown (NULL).
  </para>

 </sect1>

6417
</chapter>
6418

6419 6420
<!-- Keep this comment at the end of the file
Local variables:
T
Thomas G. Lockhart 已提交
6421
mode:sgml
6422
sgml-omittag:nil
6423 6424 6425 6426 6427 6428 6429 6430
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
T
Thomas G. Lockhart 已提交
6431
sgml-local-catalogs:("/usr/lib/sgml/catalog")
6432 6433 6434
sgml-local-ecat-files:nil
End:
-->