func.sgml 130.7 KB
Newer Older
1
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.76 2001/10/09 18:46:00 petere Exp $ -->
2 3 4 5

<chapter id="functions">
 <title>Functions and Operators</title>

6 7 8 9 10 11 12 13
 <indexterm zone="functions">
  <primary>functions</primary>
 </indexterm>

 <indexterm zone="functions">
  <primary>operators</primary>
 </indexterm>

14 15 16 17 18 19 20
 <para>
  <productname>Postgres</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
21
  available functions and operators, respectively.
22 23 24 25 26 27
 </para>

 <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
28
  and some explicitly marked functions, are not specified by the <acronym>SQL</acronym>
29 30 31
  standard. Some of this extended functionality is present in other
  <acronym>RDBMS</acronym> products, and in many cases this
  functionality is compatible and consistant between various products.
32 33
 </para>

34 35 36 37

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

38 39 40 41 42 43 44 45 46 47 48
  <indexterm zone="functions-logical">
   <primary>operators</primary>
   <secondary>logical</secondary>
  </indexterm>

  <indexterm>
   <primary>Boolean</primary>
   <secondary>operators</secondary>
   <see>operators, logical</see>
  </indexterm>

49 50 51
  <para>
   The usual logical operators are available:

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   <indexterm>
    <primary>and</primary>
    <secondary>operator</secondary>
   </indexterm>

   <indexterm>
    <primary>or</primary>
    <secondary>operator</secondary>
   </indexterm>

   <indexterm>
    <primary>not</primary>
    <secondary>operator</secondary>
   </indexterm>

67 68 69 70 71 72
   <simplelist>
    <member>AND</member>
    <member>OR</member>
    <member>NOT</member>
   </simplelist>

73
   <acronym>SQL</acronym> uses a three-valued Boolean logic where NULL represents
74 75 76 77 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
   <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>
122 123 124 125 126 127 128

      <row>
       <entry>NULL</entry>
       <entry>NULL</entry>
       <entry>NULL</entry>
       <entry>NULL</entry>
      </row>
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
     </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>

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

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

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

166 167 168 169 170
  <indexterm zone="functions-comparison">
   <primary>comparison</primary>
   <secondary>operators</secondary>
  </indexterm>

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  <table>
   <title>Comparison Operators</TITLE>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>Operator</entry>
      <entry>Description</entry>
     </row>
    </thead>

    <tbody>
     <row>
      <entry> <literal>&lt;</literal> </entry>
      <entry>less than</entry>
     </row>

     <row>
      <entry> <literal>&gt;</literal> </entry>
      <entry>greater than</entry>
     </row>

     <row>
      <entry> <literal>&lt;=</literal> </entry>
      <entry>less than or equal to</entry>
     </row>

     <row>
      <entry> <literal>&gt;=</literal> </entry>
      <entry>greater than or equal to</entry>
     </row>

     <row>
      <entry> <literal>=</literal> </entry>
      <entry>equal</entry>
     </row>

     <row>
      <entry> <literal>&lt;&gt;</literal> or <literal>!=</literal> </entry>
      <entry>not equal</entry>
     </row>
    </tbody>
   </tgroup>
  </table>

  <note>
T
Thomas G. Lockhart 已提交
216
   <para>
217 218 219 220
    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.
T
Thomas G. Lockhart 已提交
221
   </para>
222 223 224 225 226
  </note>

  <para>
   Comparison operators are available for all data types where this
   makes sense.  All comparison operators are binary operators that
227 228 229
   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
230 231
   <literal>3</literal>).
  </para>
232

P
Peter Eisentraut 已提交
233
  <para>
234 235 236
   <indexterm>
    <primary>between</primary>
   </indexterm>
P
Peter Eisentraut 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
   In addition to the comparison operators, the special
   <token>BETWEEN</token> construct is available.
<synopsis>
<replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
</synopsis>
   is equivalent to
<synopsis>
<replaceable>a</replaceable> &gt;= <replaceable>x</replaceable> AND <replaceable>a</replaceable> &lt;= <replaceable>y</replaceable>
</synopsis>
   Similarly,
<synopsis>
<replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
</synopsis>
   is equivalent to
<synopsis>
<replaceable>a</replaceable> &lt; <replaceable>x</replaceable> OR <replaceable>a</replaceable> &gt; <replaceable>y</replaceable>
</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>

259 260 261 262 263 264 265 266 267
  <para>
   To check whether a value is or is not NULL, use the constructs
<synopsis>
<replaceable>expression</replaceable> IS NULL
<replaceable>expression</replaceable> IS NOT NULL
</synopsis>
   Do <emphasis>not</emphasis> use
   <literal><replaceable>expression</replaceable> = NULL</literal>
   because NULL is not <quote>equal to</quote> NULL.  (NULL represents
268 269
   an unknown value, and it is not known whether two unknown values are
   equal.)
270
  </para>
271 272

  <para>
273 274 275 276 277 278 279 280 281 282 283 284 285
   Some applications may (incorrectly) require that
   <literal><replaceable>expression</replaceable> = NULL</literal>
   returns true if <replaceable>expression</replaceable> evaluates to
   the NULL value.  To support these applications, the run-time option
   <varname>transform_null_equals</varname> can be turned on (e.g.,
   <literal>SET transform_null_equals TO ON;</literal>).
   <productname>PostgreSQL</productname> would 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>

  <para>
   Boolean values can also be tested using the constructs
286 287 288 289 290 291 292 293 294 295 296 297
<synopsis>
<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
</synopsis>
   These are similar to <literal>IS NULL</literal> in that they will
   always return TRUE or FALSE, never NULL, even when the operand is NULL.
   A NULL input is treated as the logical value UNKNOWN.
  </para>
298 299 300 301 302 303
 </sect1>


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

304 305 306 307 308 309 310 311
  <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>

312
  <table>
313
   <title>Mathematical Operators</title>
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

   <tgroup cols="4">
    <thead>
     <row>
      <entry>Name</entry>
      <entry>Description</entry>
      <entry>Example</entry>
      <entry>Result</entry>
     </row>
    </thead>

    <tbody>
     <row>
      <entry> <literal>+</literal> </entry>
      <entry>Addition</entry>
      <entry>2 + 3</entry>
      <entry>5</entry>
     </row>

     <row>
      <entry> <literal>-</literal> </entry>
      <entry>Subtraction</entry>
      <entry>2 - 3</entry>
      <entry>-1</entry>
     </row>

     <row>
      <entry> <literal>*</literal> </entry>
      <entry>Multiplication</entry>
      <entry>2 * 3</entry>
      <entry>6</entry>
     </row>

     <row>
      <entry> <literal>/</literal> </entry>
      <entry>Division (integer division truncates results)</entry>
      <entry>4 / 2</entry>
      <entry>2</entry>
     </row>

     <row>
      <entry> <literal>%</literal> </entry>
      <entry>Modulo (remainder)</entry>
      <entry>5 % 4</entry>
      <entry>1</entry>
     </row>

     <row>
      <entry> <literal>^</literal> </entry>
      <entry>Exponentiation</entry>
      <entry>2.0 ^ 3.0</entry>
365
      <entry>8</entry>
366 367 368 369 370 371
     </row>

     <row>
      <entry> <literal>|/</literal> </entry>
      <entry>Square root</entry>
      <entry>|/ 25.0</entry>
372
      <entry>5</entry>
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
     </row>

     <row>
      <entry> <literal>||/</literal> </entry>
      <entry>Cube root</entry>
      <entry>||/ 27.0</entry>
      <entry>3</entry>
     </row>

     <row>
      <entry> <literal>!</literal> </entry>
      <entry>Factorial</entry>
      <entry>5 !</entry>
      <entry>120</entry>
     </row>

     <row>
      <entry> <literal>!!</literal> </entry>
391
      <entry>Factorial (prefix operator)</entry>
392 393 394 395 396 397 398 399
      <entry>!! 5</entry>
      <entry>120</entry>
     </row>

     <row>
      <entry> <literal>@</literal> </entry>
      <entry>Absolute value</entry>
      <entry>@ -5.0</entry>
400
      <entry>5</entry>
401
     </row>
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

     <row>
      <entry> <literal>&amp;</literal> </entry>
      <entry>Binary AND</entry>
      <entry>91 & 15</entry>
      <entry>11</entry>
     </row>

     <row>
      <entry> <literal>|</literal> </entry>
      <entry>Binary OR</entry>
      <entry>32 | 3</entry>
      <entry>35</entry>
     </row>

     <row>
      <entry> <literal>#</literal> </entry>
      <entry>Binary XOR</entry>
      <entry>17 # 5</entry>
      <entry>20</entry>
     </row>

     <row>
      <entry> <literal>~</literal> </entry>
      <entry>Binary NOT</entry>
      <entry>~1</entry>
      <entry>-2</entry>
     </row>

     <row>
      <entry> &lt;&lt; </entry>
      <entry>Binary shift left</entry>
      <entry>1 &lt;&lt; 4</entry>
      <entry>16</entry>
     </row>

     <row>
      <entry> &gt;&gt; </entry>
      <entry>Binary shift right</entry>
      <entry>8 &gt;&gt; 2</entry>
      <entry>2</entry>
     </row>

445 446 447 448
    </tbody>
   </tgroup>
  </table>

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  <para>
   The <quote>binary</quote> operators are also available for the bit
   string types <type>BIT</type> and <type>BIT VARYING</type>.

   <table>
    <title>Bit String Binary Operators</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <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>

   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>
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

  <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>
514
      <entry><function>abs</function>(<replaceable>x</replaceable>)</entry>
515
      <entry>(same as x)</entry>
516
      <entry>absolute value</entry>
517
      <entry><literal>abs(-17.4)</literal></entry>
518 519 520 521
      <entry>17.4</entry>
     </row>

     <row>
522
      <entry><function>cbrt</function>(<type>dp</type>)</entry>
523
      <entry><type>dp</type></entry>
524
      <entry>cube root</entry>
525
      <entry><literal>cbrt(27.0)</literal></entry>
526
      <entry>3</entry>
527 528 529
     </row>

     <row>
530
      <entry><function>ceil</function>(<type>numeric</type>)</entry>
531 532
      <entry><type>numeric</type></entry>
      <entry>smallest integer not less than argument</entry>
533
      <entry><literal>ceil(-42.8)</literal></entry>
534 535 536 537
      <entry>-42</entry>
     </row>

     <row>
538
      <entry><function>degrees</function>(<type>dp</type>)</entry>
539 540
      <entry><type>dp</type></entry>
      <entry>radians to degrees</entry>
541
      <entry><literal>degrees(0.5)</literal></entry>
542 543 544 545
      <entry>28.6478897565412</entry>
     </row>

     <row>
546
      <entry><function>exp</function>(<type>dp</type>)</entry>
547 548
      <entry><type>dp</type></entry>
      <entry>exponential</entry>
549
      <entry><literal>exp(1.0)</literal></entry>
550 551 552 553
      <entry>2.71828182845905</entry>
     </row>

     <row>
554
      <entry><function>floor</function>(<type>numeric</type>)</entry>
555 556
      <entry><type>numeric</type></entry>
      <entry>largest integer not greater than argument</entry>
557
      <entry><literal>floor(-42.8)</literal></entry>
558
      <entry>-43</entry>
559 560 561
     </row>

     <row>
562
      <entry><function>ln</function>(<type>dp</type>)</entry>
563
      <entry><type>dp</type></entry>
564
      <entry>natural logarithm</entry>
565
      <entry><literal>ln(2.0)</literal></entry>
566 567 568 569
      <entry>0.693147180559945</entry>
     </row>

     <row>
570
      <entry><function>log</function>(<type>dp</type>)</entry>
571
      <entry><type>dp</type></entry>
572
      <entry>base 10 logarithm</entry>
573
      <entry><literal>log(100.0)</literal></entry>
574
      <entry>2</entry>
575 576 577
     </row>

     <row>
578
      <entry><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
579
       <parameter>x</parameter> <type>numeric</type>)</entry>
580
      <entry><type>numeric</type></entry>
581
      <entry>logarithm to base <parameter>b</parameter></entry>
582
      <entry><literal>log(2.0, 64.0)</literal></entry>
583
      <entry>6.0000000000</entry>
584 585 586
     </row>

     <row>
587
      <entry><function>mod</function>(<parameter>y</parameter>, <parameter>x</parameter>)</entry>
588
      <entry>(same as argument types)</entry>
589
      <entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
590
      <entry><literal>mod(9,4)</literal></entry>
591 592 593 594
      <entry>1</entry>
     </row>

     <row>
595
      <entry><function>pi</function>()</entry>
596
      <entry><type>dp</type></entry>
597
      <entry><quote>Pi</quote> constant</entry>
598
      <entry><literal>pi()</literal></entry>
599 600 601 602
      <entry>3.14159265358979</entry>
     </row>

     <row>
603
      <entry><function>pow</function>(<parameter>e</parameter> <type>dp</type>,
604 605 606
       <parameter>n</parameter> <type>dp</type>)</entry>
      <entry><type>dp</type></entry>
      <entry>raise a number to exponent <parameter>e</parameter></entry>
607
      <entry><literal>pow(9.0, 3.0)</literal></entry>
608
      <entry>729</entry>
609 610 611
     </row>

     <row>
612
      <entry><function>radians</function>(<type>dp</type>)</entry>
613 614
      <entry><type>dp</type></entry>
      <entry>degrees to radians</entry>
615
      <entry><literal>radians(45.0)</literal></entry>
616 617 618 619
      <entry>0.785398163397448</entry>
     </row>

     <row>
620
      <entry><function>random</function>()</entry>
621 622
      <entry><type>dp</type></entry>
      <entry>value between 0.0 to 1.0</entry>
623
      <entry><literal>random()</literal></entry>
624 625 626 627
      <entry></entry>
     </row>

     <row>
628
      <entry><function>round</function>(<type>dp</type>)</entry>
629
      <entry><type>dp</type></entry>
630
      <entry>round to nearest integer</entry>
631
      <entry><literal>round(42.4)</literal></entry>
632 633 634 635
      <entry>42</entry>
     </row>

     <row>
636
      <entry><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
637
      <entry><type>numeric</type></entry>
638
      <entry>round to <parameter>s</parameter> decimal places</entry>
639
      <entry><literal>round(42.4382, 2)</literal></entry>
640 641 642 643
      <entry>42.44</entry>
     </row>
<!--
     <row>
644
      <entry><function>setseed</function>(<replaceable>new-seed</replaceable>)</entry>
645
      <entry>set seed for subsequent random() calls</entry>
646
      <entry><literal>setseed(0.54823)</literal></entry>
647 648 649
      <entry></entry>
     </row>
-->
P
Peter Eisentraut 已提交
650 651 652 653 654 655 656 657
     <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>

658
     <row>
659
      <entry><function>sqrt</function>(<type>dp</type>)</entry>
660
      <entry><type>dp</type></entry>
661
      <entry>square root</entry>
662
      <entry><literal>sqrt(2.0)</literal></entry>
663 664 665 666
      <entry>1.4142135623731</entry>
     </row>

     <row>
667
      <entry><function>trunc</function>(<type>dp</type>)</entry>
668 669
      <entry><type>dp</type></entry>
      <entry>truncate toward zero</entry>
670
      <entry><literal>trunc(42.8)</literal></entry>
671 672 673 674
      <entry>42</entry>
     </row>

     <row>
675
      <entry><function>trunc</function>(<type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
676
      <entry><type>numeric</type></entry>
677
      <entry>truncate to <parameter>s</parameter> decimal places</entry>
678
      <entry><literal>trunc(42.4382, 2)</literal></entry>
679 680 681 682 683 684 685 686
      <entry>42.43</entry>
     </row>

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

  <para>
687
   In the table above, <literal>dp</literal> indicates <type>double precision</type>.
688 689 690 691 692
   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
693 694 695 696
   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
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
   of the host system's C library and behavior in boundary cases could
   therefore vary depending on the operating system.
  </para>

  <table>
   <title>Trigonometric Functions</title>

   <tgroup cols="2">
    <thead>
     <row>
      <entry>Function</entry>
      <entry>Description</entry>
     </row>
    </thead>

    <tbody>
     <row>
714
      <entry><function>acos</function>(<replaceable>x</replaceable>)</entry>
715 716 717 718
      <entry>inverse cosine</entry>
     </row>

     <row>
719
      <entry><function>asin</function>(<replaceable>x</replaceable>)</entry>
720 721 722 723
      <entry>inverse sine</entry>
     </row>

     <row>
724
      <entry><function>atan</function>(<replaceable>x</replaceable>)</entry>
725 726 727 728
      <entry>inverse tangent</entry>
     </row>

     <row>
729
      <entry><function>atan2</function>(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</entry>
730 731 732 733
      <entry>inverse tangent of <replaceable>y</replaceable>/<replaceable>x</replaceable></entry>
     </row>

     <row>
734
      <entry><function>cos</function>(<replaceable>x</replaceable>)</entry>
735 736 737 738
      <entry>cosine</entry>
     </row>

     <row>
739
      <entry><function>cot</function>(<replaceable>x</replaceable>)</entry>
740 741 742 743
      <entry>cotangent</entry>
     </row>

     <row>
744
      <entry><function>sin</function>(<replaceable>x</replaceable>)</entry>
745 746 747 748
      <entry>sine</entry>
     </row>

     <row>
749
      <entry><function>tan</function>(<replaceable>x</replaceable>)</entry>
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
      <entry>tangent</entry>
     </row>
    </tbody>
   </tgroup>
  </table>

  <para>
   All trigonometric functions have arguments and return values of
   type <type>double precision</type>.
  </para>

 </sect1>


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

  <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
776 777
   to a string representation first.  Some functions also exist
   natively for bit string types.
778 779 780
  </para>

  <para>
781
   <acronym>SQL</acronym> defines some string functions with a special syntax where
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
   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>

  <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>

    <tbody>
     <row>
      <entry> <parameter>string</parameter> <literal>||</literal> <parameter>string</parameter> </entry>
      <entry> <type>text</type> </entry>
805 806 807 808 809 810 811
      <entry>
       string concatenation
       <indexterm>
        <primary>character strings</primary>
	<secondary>concatenation</secondary>
       </indexterm>
      </entry>
812 813
      <entry><literal>'Postgre' || 'SQL'</></entry>
      <entry><literal>PostgreSQL</></entry>
814 815
     </row>

P
Peter Eisentraut 已提交
816
     <row>
817
      <entry><function>bit_length</function>(<parameter>string</parameter>)</entry>
P
Peter Eisentraut 已提交
818 819
      <entry><type>integer</type></entry>
      <entry>number of bits in string</entry>
820 821
      <entry><literal>bit_length('jose')</literal></entry>
      <entry><literal>32</literal></entry>
P
Peter Eisentraut 已提交
822 823
     </row>

824
     <row>
825
      <entry><function>char_length</function>(<parameter>string</parameter>) or character_length(<parameter>string</parameter>)</entry>
826
      <entry><type>integer</type></entry>
827 828 829 830 831 832 833 834 835 836 837 838
      <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>
839 840
      <entry><literal>char_length('jose')</></entry>
      <entry><literal>4</></entry>
841 842 843
     </row>

     <row>
844
      <entry><function>lower</function>(<parameter>string</parameter>)</entry>
845 846
      <entry><type>text</type></entry>
      <entry>Convert string to lower case.</entry>
847 848
      <entry><literal>lower('TOM')</literal></entry>
      <entry><literal>tom</literal></entry>
849 850 851
     </row>

     <row>
852
      <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
853 854
      <entry><type>integer</type></entry>
      <entry>number of bytes in string</entry>
855 856
      <entry><literal>octet_length('jose')</literal></entry>
      <entry><literal>4</literal></entry>
857 858 859
     </row>

     <row>
860
      <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
861 862
      <entry><type>integer</type></entry>
      <entry>location of specified substring</entry>
863 864
      <entry><literal>position('om' in 'Thomas')</literal></entry>
      <entry><literal>3</literal></entry>
865 866 867
     </row>

     <row>
868
      <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
869
      <entry><type>text</type></entry>
870 871 872 873 874 875
      <entry>
       extract substring
       <indexterm>
        <primary>substring</primary>
       </indexterm>
      </entry>
876 877
      <entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
      <entry><literal>hom</literal></entry>
878 879 880 881
     </row>

     <row>
      <entry>
882
       <function>trim</function>(<optional>leading | trailing | both</optional>
883 884 885 886 887 888 889 890 891
       <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>
892 893
      <entry><literal>trim(both 'x' from 'xTomx')</literal></entry>
      <entry><literal>Tom</literal></entry>
894 895 896
     </row>

     <row>
897
      <entry><function>upper</function>(<parameter>string</parameter>)</entry>
898 899
      <entry><type>text</type></entry>
      <entry>Convert string to upper case.</entry>
900 901
      <entry><literal>upper('tom')</literal></entry>
      <entry><literal>TOM</literal></entry>
902 903 904 905
     </row>
    </tbody>
   </tgroup>
  </table>
906

T
Thomas G. Lockhart 已提交
907
  <para>
908 909
   Additional string manipulation functions are available and are
   listed below.  Some of them are used internally to implement the
910
   <acronym>SQL</acronym> string functions listed above.
T
Thomas G. Lockhart 已提交
911
  </para>
912

913 914 915 916 917 918
  <table id="functions-string-other">
   <title>Other String Functions</title>
   <tgroup cols="5">
    <thead>
     <row>
      <entry>Function</entry>
919
      <entry>Return Type</entry>
920 921 922 923 924 925 926 927
      <entry>Description</entry>
      <entry>Example</entry>
      <entry>Result</entry>
     </row>
    </thead>

    <tbody>
     <row>
928
      <entry><function>ascii</function>(<type>text</type>)</entry>
929 930
      <entry>integer</entry>
      <entry>Returns the <acronym>ASCII</acronym> code of the first character of the argument.</entry>
931 932
      <entry><literal>ascii('x')</literal></entry>
      <entry><literal>120</literal></entry>
933 934 935
     </row>

     <row>
936
      <entry><function>btrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
937 938 939 940 941 942
      <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>
943 944
      <entry><literal>btrim('xyxtrimyyx','xy')</literal></entry>
      <entry><literal>trim</literal></entry>
945 946 947
     </row>

     <row>
948
      <entry><function>chr</function>(<type>integer</type>)</entry>
949 950
      <entry><type>text</type></entry>
      <entry>Returns the character with the given <acronym>ASCII</acronym> code.</entry>
951 952
      <entry><literal>chr(65)</literal></entry>
      <entry><literal>A</literal></entry>
953 954
     </row>

T
Tatsuo Ishii 已提交
955
     <row>
956 957 958 959 960
      <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>
T
Tatsuo Ishii 已提交
961
      <entry><type>text</type></entry>
962 963 964 965 966 967
      <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.
T
Tatsuo Ishii 已提交
968
      </entry>
969 970
      <entry><literal>convert('text_in_unicode', 'UNICODE', 'LATIN1')</literal></entry>
      <entry><literal>text_in_unicode</literal> represented in ISO 8859-1</entry>
T
Tatsuo Ishii 已提交
971 972
     </row>

973
     <row>
974
      <entry><function>initcap</function>(<type>text</type>)</entry>
975 976
      <entry><type>text</type></entry>
      <entry>Converts first letter of each word (whitespace separated) to upper case.</entry>
977 978
      <entry><literal>initcap('hi thomas')</literal></entry>
      <entry><literal>Hi Thomas</literal></entry>
979 980 981 982
     </row>

     <row>
      <entry>
983
       <function>lpad</function>(<parameter>string</parameter> <type>text</type>,
984 985 986 987 988 989 990 991 992 993 994 995
       <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>
996 997
      <entry><literal>lpad('hi', 5, 'xy')</literal></entry>
      <entry><literal>xyxhi</literal></entry>
998 999 1000
     </row>

     <row>
1001
      <entry><function>ltrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
1002 1003 1004 1005 1006
      <entry><type>text</type></entry>
      <entry>
       Removes the longest string containing only characters from
       <parameter>trim</parameter> from the start of the string.
      </entry>
1007 1008
      <entry><literal>ltrim('zzzytrim','xyz')</literal></entry>
      <entry><literal>trim</literal></entry>
1009 1010 1011
     </row>

     <row>
1012
      <entry><function>repeat</function>(<type>text</type>, <type>integer</type>)</entry>
1013 1014
      <entry><type>text</type></entry>
      <entry>Repeat text a number of times.</entry>
1015 1016
      <entry><literal>repeat('Pg', 4)</literal></entry>
      <entry><literal>PgPgPgPg</literal></entry>
1017 1018 1019 1020
     </row>

     <row>
      <entry>
1021
       <function>rpad</function>(<parameter>string</parameter> <type>text</type>,
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
       <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>
1033 1034
      <entry><literal>rpad('hi', 5, 'xy')</literal></entry>
      <entry><literal>hixyx</literal></entry>
1035 1036 1037
     </row>

     <row>
1038
      <entry><function>rtrim</function>(<parameter>string</parameter> text, <parameter>trim</parameter> text)</entry>
1039 1040 1041 1042 1043
      <entry><type>text</type></entry>
      <entry>
       Removes the longest string containing only characters from
       <parameter>trim</parameter> from the end of the string.
      </entry>
1044 1045
      <entry><literal>rtrim('trimxxxx','x')</literal></entry>
      <entry><literal>trim</literal></entry>
1046 1047 1048
     </row>

     <row>
1049
      <entry><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</entry>
1050 1051 1052 1053 1054 1055 1056
      <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>
1057 1058
      <entry><literal>strpos('high','ig')</literal></entry>
      <entry><literal>2</literal></entry>
1059 1060 1061
     </row>

     <row>
1062
      <entry><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</entry>
1063 1064 1065 1066
      <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>
1067 1068
      <entry><literal>substr('alphabet', 3, 2)</literal></entry>
      <entry><literal>ph</literal></entry>
1069 1070 1071
     </row>

     <row>
1072
      <entry><function>to_ascii</function>(<type>text</type> <optional>, <parameter>encoding</parameter></optional>)</entry>
1073 1074
      <entry><type>text</type></entry>
      <entry>Converts text from multibyte encoding to <acronym>ASCII</acronym>.</entry>
1075 1076
      <entry><literal>to_ascii('Karel')</literal></entry>
      <entry><literal>Karel</literal></entry>
1077 1078 1079 1080
     </row>

     <row>
      <entry>
1081
       <function>translate</function>(<parameter>string</parameter> <type>text</type>,
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
       <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>
1092 1093
      <entry><literal>translate('12345', '14', 'ax')</literal></entry>
      <entry><literal>a23x5</literal></entry>
1094
     </row>       
1095 1096 1097
     
     <row>
      <entry>
1098
       <function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
1099 1100 1101 1102
              <parameter>type</parameter> <type>text</type>)
      </entry>
      <entry><type>text</type></entry>
      <entry>
1103
       Encodes binary data to <acronym>ASCII</acronym>-only representation.  Supported
1104 1105
       types are: 'base64', 'hex'.
      </entry>
1106 1107
      <entry><literal>encode('123\\000\\001', 'base64')</literal></entry>
      <entry><literal>MTIzAAE=</literal></entry>
1108 1109 1110 1111
     </row>       

     <row>
      <entry>
1112
       <function>decode</function>(<parameter>string</parameter> <type>text</type>,
1113 1114 1115 1116 1117 1118 1119
              <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>
1120 1121
      <entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
      <entry><literal>123\000\001</literal></entry>
1122
     </row>       
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

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

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


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

  <para>
   There are two separate approaches to pattern matching provided by
1140
   <productname>Postgres</productname>:  the <acronym>SQL</acronym>
1141 1142 1143
   <function>LIKE</function> operator and
   <acronym>POSIX</acronym>-style regular expressions.
  </para>
1144

1145
  <tip>
1146
   <para>
1147 1148 1149
    If you have pattern matching needs that go beyond this, or want to
    make pattern-driven substitutions or translations, consider
    writing a user-defined function in Perl or Tcl.
1150
   </para>
1151 1152 1153 1154 1155
  </tip>

  <sect2 id="functions-like">
   <title>Pattern Matching with <function>LIKE</function></title>

1156 1157 1158 1159
   <indexterm>
    <primary>like</primary>
   </indexterm>

1160 1161 1162 1163
<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>
1164 1165

   <para>
1166 1167 1168 1169 1170 1171 1172 1173 1174
    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>.)
1175 1176
   </para>

1177 1178
   <para>
    If <replaceable>pattern</replaceable> does not contain percent
1179
    signs or underscore, then the pattern only represents the string
1180 1181 1182
    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
1183 1184
    character; a percent sign (<literal>%</literal>) matches any string
    of zero or more characters.
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
   </para>

   <informalexample>
    <para>
     Some examples:
<programlisting>
'abc' LIKE 'abc'    <lineannotation>true</lineannotation>
'abc' LIKE 'a%'     <lineannotation>true</lineannotation>
'abc' LIKE '_b_'    <lineannotation>true</lineannotation>
'abc' LIKE 'c'      <lineannotation>false</lineannotation>
</programlisting>
    </para>
   </informalexample>
1198

T
Thomas G. Lockhart 已提交
1199
   <para>
1200
    <function>LIKE</function> pattern matches always cover the entire
1201
    string.  To match a pattern anywhere within a string, the
1202
    pattern must therefore start and end with a percent sign.
T
Thomas G. Lockhart 已提交
1203
   </para>
1204 1205

   <para>
1206 1207 1208 1209
    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
1210
    character is the backslash but a different one may be selected by
1211 1212 1213 1214 1215 1216 1217 1218 1219
    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
    you must write two backslashes in the query.  You can avoid this by
    selecting a different escape character with <literal>ESCAPE</literal>.
1220
   </para>
1221 1222

   <para>
1223 1224
    The keyword <token>ILIKE</token> can be used instead of
    <token>LIKE</token> to make the match case insensitive according
1225
    to the active locale.  This is not in the <acronym>SQL</acronym> standard but is a
1226
    <productname>Postgres</productname> extension.
T
Thomas G. Lockhart 已提交
1227
   </para>
1228

1229 1230
   <para>
    The operator <literal>~~</literal> is equivalent to
1231 1232 1233
    <function>LIKE</function>, and <literal>~~*</literal> corresponds to
    <function>ILIKE</function>.  There are also
    <literal>!~~</literal> and <literal>!~~*</literal> operators that
1234 1235 1236 1237 1238
    represent <function>NOT LIKE</function> and <function>NOT
    ILIKE</function>.  All of these are also
    <productname>Postgres</productname>-specific.
   </para>
  </sect2>
1239

1240 1241

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

1244 1245 1246 1247 1248
   <indexterm zone="functions-regexp">
    <primary>regular expressions</primary>
    <seealso>pattern matching</seealso>
   </indexterm>

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
   <table>
    <title>Regular Expression Match Operators</title>

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

     <tbody>
       <ROW>
	<ENTRY> <literal>~</literal> </ENTRY>
	<ENTRY>Matches regular expression, case sensitive</ENTRY>
1265
	<ENTRY><literal>'thomas' ~ '.*thomas.*'</literal></ENTRY>
1266 1267 1268 1269
       </ROW>
       <ROW>
	<ENTRY> <literal>~*</literal> </ENTRY>
	<ENTRY>Matches regular expression, case insensitive</ENTRY>
1270
	<ENTRY><literal>'thomas' ~* '.*Thomas.*'</literal></ENTRY>
1271 1272 1273 1274
       </ROW>
       <ROW>
	<ENTRY> <literal>!~</literal> </ENTRY>
	<ENTRY>Does not match regular expression, case sensitive</ENTRY>
1275
	<ENTRY><literal>'thomas' !~ '.*Thomas.*'</literal></ENTRY>
1276 1277 1278 1279
       </ROW>
       <ROW>
	<ENTRY> <literal>!~*</literal> </ENTRY>
	<ENTRY>Does not match regular expression, case insensitive</ENTRY>
1280
	<ENTRY><literal>'thomas' !~* '.*vadim.*'</literal></ENTRY>
1281 1282 1283 1284 1285
       </ROW>
     </tbody>
    </tgroup>
   </table>

1286
   <para>
1287
    <acronym>POSIX</acronym> regular expressions provide a more powerful means for
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    pattern matching than the <function>LIKE</function> function.
    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>

1309 1310

<!-- derived from the re_format.7 man page -->
T
Thomas G. Lockhart 已提交
1311
   <para>
1312
    Regular expressions (<quote>RE</quote>s), as defined in <acronym>POSIX</acronym>
1313 1314 1315
    1003.2, come in two forms: modern REs (roughly those of
    <command>egrep</command>; 1003.2 calls these
    <quote>extended</quote> REs) and obsolete REs (roughly those of
1316 1317
    <command>ed</command>; 1003.2 <quote>basic</quote> REs).
    <productname>Postgres</productname> implements the modern form.
T
Thomas G. Lockhart 已提交
1318
   </para>
1319

T
Thomas G. Lockhart 已提交
1320
   <para>
1321 1322 1323 1324
    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 已提交
1325
   </para>
1326

1327
   <para>
1328 1329 1330
    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.
1331
   </para>
1332

B
Bruce Momjian 已提交
1333
   <para>
1334 1335 1336 1337 1338 1339 1340 1341
    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 已提交
1342
   </para>
1343

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
   <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>
1372

1373
   <para>
1374 1375 1376 1377 1378
    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),
1379 1380 1381
    <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
1382 1383 1384 1385 1386 1387 1388 1389 1390
    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>.
1391
   </para>
1392

1393 1394 1395 1396 1397 1398 1399
   <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 已提交
1400
   <para>
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    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 已提交
1414
   </para>
1415

T
Thomas G. Lockhart 已提交
1416
   <para>
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    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
1432
    multiple-character sequence that collates as if it were a single
1433 1434 1435 1436
    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
1437
    expression containing a multiple-character collating element can thus
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    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
1484
    specified by <acronym>POSIX</acronym> 1003.2, and should be used with caution in
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
    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>
    There is no particular limit on the length of REs, except insofar
    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
1536 1537 1538 1539 1540 1541 1542 1543
    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 已提交
1544
   </para>
1545 1546
<!-- end re_format.7 man page -->
  </sect2>
1547

1548 1549 1550 1551
 </sect1>


  <sect1 id="functions-formatting">
1552
   <title>Data Type Formatting Functions</title>
1553

1554 1555 1556 1557
   <indexterm zone="functions-formatting">
    <primary>formatting</primary>
   </indexterm>

1558 1559 1560
   <note>
    <title>Author</title>
    <para>
1561
     Written by Karel Zak (<email>zakkr@zf.jcu.cz</email>) on 2000-01-24
1562 1563
    </para>
   </note>
1564

1565
   <para>
1566 1567 1568
    The <productname>Postgres</productname> formatting functions
    provide a powerful set of tools for converting various data types
    (date/time, integer, floating point, numeric) to formatted strings
1569
    and for converting from formatted strings to specific data types.
1570
    These functions all follow a common calling convention: the first
1571
    argument is the value to be formatted and the second argument is a
1572
    template that defines the output or input format.
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
   </para>

   <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>
1589 1590 1591 1592
	<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>
1593
       </row>
B
Bruce Momjian 已提交
1594
       <row>
1595 1596
	<entry><function>to_char</function>(<type>interval</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
B
Bruce Momjian 已提交
1597
	<entry>convert interval to string</entry>
1598
	<entry><literal>to_char(interval '15h 2m 12s','HH24:MI:SS')</literal></entry>
B
Bruce Momjian 已提交
1599
       </row>
1600
       <row>
1601 1602
	<entry><function>to_char</function>(<type>int</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
1603
	<entry>convert int4/int8 to string</entry>
1604
	<entry><literal>to_char(125, '999')</literal></entry>
1605 1606
       </row>
       <row>
1607 1608
	<entry><function>to_char</function>(<type>double precision</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
1609
	<entry>convert real/double precision to string</entry>
1610
	<entry><literal>to_char(125.8, '999D9')</literal></entry>
1611 1612
       </row>
       <row>
1613 1614
	<entry><function>to_char</function>(<type>numeric</type>, <type>text</type>)</entry>
	<entry><type>text</type></entry>
1615
	<entry>convert numeric to string</entry>
1616
	<entry><literal>to_char(numeric '-125.8', '999D99S')</literal></entry>
1617 1618
       </row>
       <row>
1619 1620
	<entry><function>to_date</function>(<type>text</type>, <type>text</type>)</entry>
	<entry><type>date</type></entry>
1621
	<entry>convert string to date</entry>
1622
	<entry><literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
1623 1624
       </row>
       <row>
1625 1626 1627 1628
	<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>
1629 1630
       </row>
       <row>
1631 1632
	<entry><function>to_number</function>(<type>text</type>, <type>text</type>)</entry>
	<entry><type>numeric</type></entry>
1633
	<entry>convert string to numeric</entry>
1634
	<entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
1635 1636 1637 1638 1639 1640
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

1641 1642 1643 1644 1645 1646 1647 1648 1649
   <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
    copied verbatim.  Similarly, in an input template string template patterns
    identify the parts of the input data string to be looked at and the
    values to be found there.
   </para>

1650 1651
   <para>
    <table tocentry="1">
1652
     <title>Template patterns for date/time conversions</title>
1653 1654 1655
     <tgroup cols="2">
      <thead>
       <row>
1656
	<entry>Pattern</entry>
1657 1658 1659 1660 1661
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
1662
	<entry><literal>HH</literal></entry>
1663
	<entry>hour of day (01-12)</entry>
1664 1665
       </row>
       <row>
1666
	<entry><literal>HH12</literal></entry>
1667
	<entry>hour of day (01-12)</entry>
1668
       </row>       
1669
       <row>
1670
	<entry><literal>HH24</literal></entry>
1671 1672
	<entry>hour of day (00-23)</entry>
       </row>       
1673
       <row>
1674
	<entry><literal>MI</literal></entry>
1675
	<entry>minute (00-59)</entry>
1676 1677
       </row>   
       <row>
1678
	<entry><literal>SS</literal></entry>
1679
	<entry>second (00-59)</entry>
1680
       </row>
B
Bruce Momjian 已提交
1681
       <row>
1682
	<entry><literal>MS</literal></entry>
B
Bruce Momjian 已提交
1683 1684 1685
	<entry>millisecond (000-999)</entry>
       </row>
       <row>
1686
	<entry><literal>US</literal></entry>
B
Bruce Momjian 已提交
1687 1688
	<entry>microsecond (000000-999999)</entry>
       </row>
1689
       <row>
1690
	<entry><literal>SSSS</literal></entry>
1691
	<entry>seconds past midnight (0-86399)</entry>
1692
       </row>
B
Bruce Momjian 已提交
1693
       <row>
1694
	<entry><literal>AM</literal> or <literal>A.M.</literal> or <literal>PM</literal> or <literal>P.M.</literal></entry>
B
Bruce Momjian 已提交
1695 1696 1697
	<entry>meridian indicator (upper case)</entry>
       </row>
       <row>
1698
	<entry><literal>am</literal> or <literal>a.m.</literal> or <literal>pm</literal> or <literal>p.m.</literal></entry>
B
Bruce Momjian 已提交
1699 1700
	<entry>meridian indicator (lower case)</entry>
       </row>
1701
       <row>
1702
	<entry><literal>Y,YYY</literal></entry>
1703
	<entry>year (4 and more digits) with comma</entry>
1704 1705
       </row>
       <row>
1706
	<entry><literal>YYYY</literal></entry>
1707
	<entry>year (4 and more digits)</entry>
1708 1709
       </row>
       <row>
1710
	<entry><literal>YYY</literal></entry>
1711
	<entry>last 3 digits of year</entry>
1712 1713
       </row>
       <row>
1714
	<entry><literal>YY</literal></entry>
1715
	<entry>last 2 digits of year</entry>
1716 1717
       </row>
       <row>
1718
	<entry><literal>Y</literal></entry>
1719
	<entry>last digit of year</entry>
1720
       </row>
B
Bruce Momjian 已提交
1721
       <row>
1722 1723
	<entry><literal>BC</literal> or <literal>B.C.</literal> or <literal>AD</literal> or <literal>A.D.</literal></entry>
	<entry>era indicator (upper case)</entry>
B
Bruce Momjian 已提交
1724 1725
       </row>
       <row>
1726 1727
	<entry><literal>bc</literal> or <literal>b.c.</literal> or <literal>ad</literal> or <literal>a.d.</literal></entry>
	<entry>era indicator (lower case)</entry>
B
Bruce Momjian 已提交
1728
       </row>
1729
       <row>
1730
	<entry><literal>MONTH</literal></entry>
1731
	<entry>full upper case month name (blank-padded to 9 chars)</entry>
1732 1733
       </row>
       <row>
1734
	<entry><literal>Month</literal></entry>
1735
	<entry>full mixed case month name (blank-padded to 9 chars)</entry>
1736 1737
       </row>
       <row>
1738
	<entry><literal>month</literal></entry>
1739
	<entry>full lower case month name (blank-padded to 9 chars)</entry>
1740 1741
       </row>
       <row>
1742
	<entry><literal>MON</literal></entry>
1743
	<entry>abbreviated upper case month name (3 chars)</entry>
1744 1745
       </row>
       <row>
1746
	<entry><literal>Mon</literal></entry>
1747
	<entry>abbreviated mixed case month name (3 chars)</entry>
1748 1749
       </row>
       <row>
1750
	<entry><literal>mon</literal></entry>
1751
	<entry>abbreviated lower case month name (3 chars)</entry>
1752 1753
       </row>
       <row>
1754
	<entry><literal>MM</literal></entry>
1755
	<entry>month number (01-12)</entry>
1756 1757
       </row>
       <row>
1758
	<entry><literal>DAY</literal></entry>
1759
	<entry>full upper case day name (blank-padded to 9 chars)</entry>
1760 1761
       </row>
       <row>
1762
	<entry><literal>Day</literal></entry>
1763
	<entry>full mixed case day name (blank-padded to 9 chars)</entry>
1764 1765
       </row>
       <row>
1766
	<entry><literal>day</literal></entry>
1767
	<entry>full lower case day name (blank-padded to 9 chars)</entry>
1768 1769
       </row>
       <row>
1770
	<entry><literal>DY</literal></entry>
1771
	<entry>abbreviated upper case day name (3 chars)</entry>
1772 1773
       </row>
       <row>
1774
	<entry><literal>Dy</literal></entry>
1775
	<entry>abbreviated mixed case day name (3 chars)</entry>
1776 1777
       </row>
       <row>
1778
	<entry><literal>dy</literal></entry>
1779
	<entry>abbreviated lower case day name (3 chars)</entry>
1780 1781
       </row>
       <row>
1782
	<entry><literal>DDD</literal></entry>
1783
	<entry>day of year (001-366)</entry>
1784 1785
       </row>
       <row>
1786
	<entry><literal>DD</literal></entry>
1787
	<entry>day of month (01-31)</entry>
1788 1789
       </row>
       <row>
1790
	<entry><literal>D</literal></entry>
1791
	<entry>day of week (1-7; SUN=1)</entry>
1792 1793
       </row>
       <row>
1794
	<entry><literal>W</literal></entry>
1795
	<entry>week of month (1-5) where first week start on the first day of the month</entry>
1796 1797
       </row> 
       <row>
1798
	<entry><literal>WW</literal></entry>
1799
	<entry>week number of year (1-53) where first week start on the first day of the year</entry>
1800
       </row>
B
Bruce Momjian 已提交
1801
       <row>
1802
	<entry><literal>IW</literal></entry>
1803
	<entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
B
Bruce Momjian 已提交
1804
       </row>
1805
       <row>
1806
	<entry><literal>CC</literal></entry>
1807
	<entry>century (2 digits)</entry>
1808 1809
       </row>
       <row>
1810
	<entry><literal>J</literal></entry>
1811
	<entry>Julian Day (days since January 1, 4712 BC)</entry>
1812 1813
       </row>
       <row>
1814
	<entry><literal>Q</literal></entry>
1815
	<entry>quarter</entry>
1816 1817
       </row>
       <row>
1818
	<entry><literal>RM</literal></entry>
1819
	<entry>month in Roman Numerals (I-XII; I=January) - upper case</entry>
B
Bruce Momjian 已提交
1820 1821
       </row>
       <row>
1822
	<entry><literal>rm</literal></entry>
1823
	<entry>month in Roman Numerals (I-XII; I=January) - lower case</entry>
1824
       </row>
1825
       <row>
1826
	<entry><literal>TZ</literal></entry>
1827
	<entry>timezone name - upper case</entry>
1828 1829
       </row>
       <row>
1830
	<entry><literal>tz</literal></entry>
1831
	<entry>timezone name - lower case</entry>
1832
       </row>
1833 1834 1835 1836 1837 1838
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
1839 1840 1841 1842
    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.
1843 1844 1845 1846
   </para>

   <para>
    <table tocentry="1">
1847
     <title>Template pattern modifiers for date/time conversions</title>
1848 1849 1850
     <tgroup cols="3">
      <thead>
       <row>
1851
	<entry>Modifier</entry>
1852 1853 1854 1855 1856 1857
	<entry>Description</entry>
	<entry>Example</entry>
       </row>
      </thead>
      <tbody>
       <row>
1858 1859
	<entry><literal>FM</literal> prefix</entry>
	<entry>fill mode (suppress padding blanks and zeroes)</entry>
1860
	<entry><literal>FMMonth</literal></entry>
1861 1862
       </row>
       <row>
1863 1864
	<entry><literal>TH</literal> suffix</entry>
	<entry>add upper-case ordinal number suffix</entry>
1865
	<entry><literal>DDTH</literal></entry>
1866 1867
       </row>	
       <row>
1868 1869
	<entry><literal>th</literal> suffix</entry>
	<entry>add lower-case ordinal number suffix</entry>
1870
	<entry><literal>DDth</literal></entry>
1871 1872
       </row>
       <row>
1873
	<entry><literal>FX</literal> prefix</entry>
1874 1875
	<entry>Fixed format global option (see below)</entry>
	<entry><literal>FX Month DD Day</literal></entry>
1876 1877
       </row>	
       <row>
1878
	<entry><literal>SP</literal> suffix</entry>
1879
	<entry>spell mode (not yet implemented)</entry>
1880
	<entry><literal>DDSP</literal></entry>
1881 1882 1883 1884 1885 1886 1887
       </row>       
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
1888 1889 1890
    Usage notes:

    <itemizedlist>
1891 1892 1893 1894 1895 1896 1897 1898
     <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>

1899 1900 1901
     <listitem>
      <para>
       <function>to_timestamp</function> and <function>to_date</function>
1902
       skip multiple blank spaces in the input string if the <literal>FX</literal> option 
1903 1904 1905
       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
1906 1907
       <literal>to_timestamp('2000    JUN','FXYYYY MON')</literal> returns an error,
       because <function>to_timestamp</function> expects one blank space only.
1908 1909 1910 1911 1912
      </para>
     </listitem>

     <listitem>
      <para>
1913 1914 1915 1916 1917
       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
       any string constant in <productname>Postgres</productname>.
1918 1919 1920 1921 1922
      </para>
     </listitem>

     <listitem>
      <para>
1923
       Ordinary text is allowed in <function>to_char</function>
1924 1925 1926 1927 1928 1929
       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.
1930 1931 1932 1933 1934
      </para>
     </listitem>

     <listitem>
      <para>
1935 1936 1937
       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 :-) -->
1938 1939
      </para>
     </listitem>
1940 1941 1942

     <listitem>
      <para>
1943 1944
       <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 已提交
1945
       use some non-digit character or template after <literal>YYYY</literal>,
1946
       otherwise the year is always interpreted as 4 digits. For example
P
Peter Eisentraut 已提交
1947 1948
       (with year 20000):
       <literal>to_date('200001131', 'YYYYMMDD')</literal> will be 
1949
       interpreted as a 4-digit year; better is to use a non-digit 
P
Peter Eisentraut 已提交
1950 1951 1952
       separator after the year, like
       <literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
       <literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
1953 1954
      </para>
     </listitem>
B
Bruce Momjian 已提交
1955 1956 1957

     <listitem>
      <para>
1958 1959
       Millisecond <literal>MS</literal> and microsecond <literal>US</literal>
       values are in conversion from string to time stamp used as part of
B
Bruce Momjian 已提交
1960 1961 1962 1963
       second after decimal point. For example 
       <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
       but 300, because the conversion count it as <literal>12 + 0.3</literal>.
       It means for format 'SS:MS' is '12:3' or '12:30' or '12:300' same
1964 1965
       number of milliseconds. For the three milliseconds must be used
       '12:003' that the conversion count as
B
Bruce Momjian 已提交
1966 1967 1968 1969 1970 1971
       <literal> 12 + 0.003 = 12.003 seconds </literal>. Here is a more 
       complex example: 
       <literal>to_timestamp('15:12:02.020.001230','HH:MI:SS.MS.US')</literal>
       is 15 hours, 12 minutes, 2.021230 seconds. 
      </para>
     </listitem>
1972
    </itemizedlist>
1973
   </para>
1974

1975 1976
   <para>
    <table tocentry="1">
1977
     <title>Template patterns for numeric conversions</title>
1978 1979 1980
     <tgroup cols="2">
      <thead>
       <row>
1981
	<entry>Pattern</entry>
1982 1983 1984 1985 1986
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
1987
	<entry><literal>9</literal></entry>
1988
	<entry>value with the specified number of digits</entry>
1989 1990
       </row>
       <row>
1991
	<entry><literal>0</literal></entry>
1992
	<entry>value with leading zeros</entry>
1993 1994
       </row>
       <row>
1995
	<entry><literal>.</literal> (period)</entry>
1996
	<entry>decimal point</entry>
1997 1998
       </row>       
       <row>
1999
	<entry><literal>,</literal> (comma)</entry>
2000
	<entry>group (thousand) separator</entry>
2001 2002
       </row>
       <row>
2003
	<entry><literal>PR</literal></entry>
2004
	<entry>negative value in angle brackets</entry>
2005 2006
       </row>
       <row>
2007
	<entry><literal>S</literal></entry>
2008
	<entry>negative value with minus sign (uses locale)</entry>
2009 2010
       </row>
       <row>
2011
	<entry><literal>L</literal></entry>
2012
	<entry>currency symbol (uses locale)</entry>
2013 2014
       </row>
       <row>
2015
	<entry><literal>D</literal></entry>
2016
	<entry>decimal point (uses locale)</entry>
2017 2018
       </row>
       <row>
2019
	<entry><literal>G</literal></entry>
2020
	<entry>group separator (uses locale)</entry>
2021 2022
       </row>
       <row>
2023
	<entry><literal>MI</literal></entry>
2024
	<entry>minus sign in specified position (if number < 0)</entry>
2025 2026
       </row>
       <row>
2027
	<entry><literal>PL</literal></entry>
2028
	<entry>plus sign in specified position (if number > 0)</entry>
2029 2030
       </row>
       <row>
2031
	<entry><literal>SG</literal></entry>
2032
	<entry>plus/minus sign in specified position</entry>
2033 2034
       </row>
       <row>
2035
	<entry><literal>RN</literal></entry>
2036
	<entry>roman numeral (input between 1 and 3999)</entry>
2037 2038
       </row>
       <row>
2039
	<entry><literal>TH</literal> or <literal>th</literal></entry>
2040
	<entry>convert to ordinal number</entry>
2041 2042
       </row>
       <row>
2043
	<entry><literal>V</literal></entry>
2044
	<entry>shift <replaceable>n</replaceable> digits (see
2045
	 notes)</entry>
2046 2047
       </row>
       <row>
2048
	<entry><literal>EEEE</literal></entry>
2049
	<entry>scientific numbers (not supported yet)</entry>
2050 2051 2052 2053 2054 2055 2056
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>

   <para>
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    Usage notes:

    <itemizedlist>
     <listitem>
      <para>
       A sign formatted using 'SG', 'PL' or 'MI' is not an anchor in
       the number; for example,
       to_char(-12, 'S9999') produces <literal>'  -12'</literal>,
       but to_char(-12, 'MI9999') produces <literal>'-  12'</literal>.
       The Oracle implementation does not allow the use of
       <literal>MI</literal> ahead of <literal>9</literal>, but rather
2068
       requires that <literal>9</literal> precede
2069 2070 2071 2072 2073 2074
       <literal>MI</literal>.
      </para>
     </listitem>

     <listitem>
      <para>
2075 2076 2077
       <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.
2078 2079 2080 2081 2082
      </para>
     </listitem>

     <listitem>
      <para>
2083 2084
       <literal>TH</literal> does not convert values less than zero
       and does not convert decimal numbers.
2085 2086 2087 2088 2089
      </para>
     </listitem>

     <listitem>
      <para>
2090 2091 2092
       <literal>PL</literal>, <literal>SG</literal>, and
       <literal>TH</literal> are <productname>Postgres</productname>
       extensions. 
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
      </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
2104 2105
       <literal>V</literal> combined with a decimal point.
       (E.g., <literal>99.9V99</literal> is not allowed.)
2106 2107 2108
      </para>
     </listitem>
    </itemizedlist>
2109
   </para>   
2110 2111 2112

   <para>
    <table tocentry="1">
2113
     <title><function>to_char</function> Examples</title>
2114 2115 2116 2117 2118 2119 2120 2121 2122
     <tgroup cols="2">
      <thead>
       <row>
	<entry>Input</entry>
	<entry>Output</entry>
       </row>
      </thead>
      <tbody>
       <row>
2123
        <entry><literal>to_char(now(),'Day, DD  HH12:MI:SS')</literal></entry>
2124
        <entry><literal>'Tuesday  , 06  05:39:18'</literal></entry>
2125
       </row>
2126
       <row>
2127
        <entry><literal>to_char(now(),'FMDay, FMDD  HH12:MI:SS')</literal></entry>
2128
        <entry><literal>'Tuesday, 6  05:39:18'</literal></entry>
2129 2130
       </row>          
       <row>
2131
        <entry><literal>to_char(-0.1,'99.99')</literal></entry>
2132
        <entry><literal>' -.10'</literal></entry>
2133 2134
       </row>
       <row>
2135
        <entry><literal>to_char(-0.1,'FM9.99')</literal></entry>
2136
        <entry><literal>'-.1'</literal></entry>
2137 2138
       </row>
       <row>
2139
        <entry><literal>to_char(0.1,'0.9')</literal></entry>
2140
        <entry><literal>' 0.1'</literal></entry>
2141 2142
       </row>
       <row>
2143
        <entry><literal>to_char(12,'9990999.9')</literal></entry>
2144
        <entry><literal>'    0012.0'</literal></entry>
2145 2146
       </row>
       <row>
2147
        <entry><literal>to_char(12,'FM9990999.9')</literal></entry>
2148
        <entry><literal>'0012'</literal></entry>
2149 2150
       </row>
       <row>
2151
        <entry><literal>to_char(485,'999')</literal></entry>
2152
        <entry><literal>' 485'</literal></entry>
2153 2154
       </row>
       <row>
2155
        <entry><literal>to_char(-485,'999')</literal></entry>
2156
        <entry><literal>'-485'</literal></entry>
2157 2158
       </row>
       <row>
2159
        <entry><literal>to_char(485,'9 9 9')</literal></entry>
2160
        <entry><literal>' 4 8 5'</literal></entry>
2161 2162
       </row>
       <row>
2163
        <entry><literal>to_char(1485,'9,999')</literal></entry>
2164
        <entry><literal>' 1,485'</literal></entry>
2165 2166
       </row>
       <row>
2167
        <entry><literal>to_char(1485,'9G999')</literal></entry>
2168
        <entry><literal>' 1 485'</literal></entry>
2169 2170
       </row>
       <row>
2171
        <entry><literal>to_char(148.5,'999.999')</literal></entry>
2172
        <entry><literal>' 148.500'</literal></entry>
2173 2174
       </row>
       <row>
2175
        <entry><literal>to_char(148.5,'999D999')</literal></entry>
2176
        <entry><literal>' 148,500'</literal></entry>	 
2177 2178
       </row>
       <row>
2179
        <entry><literal>to_char(3148.5,'9G999D999')</literal></entry>
2180
        <entry><literal>' 3 148,500'</literal></entry>
2181 2182
       </row>
       <row>
2183
        <entry><literal>to_char(-485,'999S')</literal></entry>
2184
        <entry><literal>'485-'</literal></entry>
2185 2186
       </row>
       <row>		
2187
        <entry><literal>to_char(-485,'999MI')</literal></entry>
2188
        <entry><literal>'485-'</literal></entry>	
2189 2190
       </row>
       <row>
2191
        <entry><literal>to_char(485,'999MI')</literal></entry>
2192
        <entry><literal>'485'</literal></entry>		
2193 2194
       </row>
       <row>
2195
        <entry><literal>to_char(485,'PL999')</literal></entry>
2196
        <entry><literal>'+485'</literal></entry>	
2197 2198
       </row>
       <row>		
2199
        <entry><literal>to_char(485,'SG999')</literal></entry>
2200
        <entry><literal>'+485'</literal></entry>	
2201 2202
       </row>
       <row>
2203
        <entry><literal>to_char(-485,'SG999')</literal></entry>
2204
        <entry><literal>'-485'</literal></entry>	
2205 2206
       </row>
       <row>
2207
        <entry><literal>to_char(-485,'9SG99')</literal></entry>
2208
        <entry><literal>'4-85'</literal></entry>	
2209 2210
       </row>
       <row>
2211
        <entry><literal>to_char(-485,'999PR')</literal></entry>
2212
        <entry><literal>'&lt;485&gt;'</literal></entry>		
2213 2214
       </row>
       <row>
2215
        <entry><literal>to_char(485,'L999')</literal></entry>
2216
        <entry><literal>'DM 485</literal></entry>	 
2217 2218
       </row>
       <row>
2219
        <entry><literal>to_char(485,'RN')</literal></entry>		
2220
        <entry><literal>'        CDLXXXV'</literal></entry>
2221 2222
       </row>
       <row>
2223
        <entry><literal>to_char(485,'FMRN')</literal></entry>	
2224
        <entry><literal>'CDLXXXV'</literal></entry>
2225 2226
       </row>
       <row>
2227
        <entry><literal>to_char(5.2,'FMRN')</literal></entry>
2228
        <entry><literal>V</literal></entry>		
2229 2230
       </row>
       <row>
2231
        <entry><literal>to_char(482,'999th')</literal></entry>
2232
        <entry><literal>' 482nd'</literal></entry>				
2233 2234
       </row>
       <row>
2235
        <entry><literal>to_char(485, '"Good number:"999')</literal></entry>
2236
        <entry><literal>'Good number: 485'</literal></entry>
2237 2238
       </row>
       <row>
2239
        <entry><literal>to_char(485.8,'"Pre:"999" Post:" .999')</literal></entry>
2240
        <entry><literal>'Pre: 485 Post: .800'</literal></entry>
2241 2242
       </row>
       <row>
2243
        <entry><literal>to_char(12,'99V999')</literal></entry>		
2244
        <entry><literal>' 12000'</literal></entry>
2245 2246
       </row>
       <row>
2247
        <entry><literal>to_char(12.4,'99V999')</literal></entry>
2248
        <entry><literal>' 12400'</literal></entry>
2249 2250
       </row>
       <row>		
2251
        <entry><literal>to_char(12.45, '99V9')</literal></entry>
2252
        <entry><literal>' 125'</literal></entry>
2253 2254 2255 2256 2257 2258 2259 2260
       </row>
      </tbody>
     </tgroup>
    </table>
   </para>
  </sect1>


2261
  <sect1 id="functions-datetime">
2262
   <title>Date/Time Functions and Operators</title>
2263

2264
   <para>
2265
    <xref linkend="functions-datetime-table"> shows the available
2266 2267 2268 2269 2270
    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
2271 2272 2273
    linkend="functions-formatting">.  You should be familiar with the
    background information on date/time data types (see <xref
    linkend="datatype-datetime">).
2274
   </para>
2275

2276 2277 2278 2279 2280 2281 2282
   <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>

2283
     <tgroup cols="3">
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
      <thead>
       <row>
        <entry>Name</entry>
        <entry>Example</entry>
        <entry>Result</entry>
       </row>
      </thead>

      <tbody>
       <row>
2294
        <entry> <literal>+</literal> </entry>
2295 2296 2297 2298 2299
        <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>
2300
        <entry> <literal>+</literal> </entry>
2301 2302 2303 2304 2305
        <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>
2306
        <entry> <literal>+</literal> </entry>
2307 2308 2309 2310 2311
        <entry><type>time</type> '01:00' + <type>interval</type> '3 hours'</entry>
        <entry><type>time</type> '04:00'</entry>
       </row>

       <row>
2312
        <entry> <literal>-</literal> </entry>
2313 2314 2315 2316 2317
        <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>
2318
        <entry> <literal>-</literal> </entry>
2319 2320 2321 2322 2323
        <entry><type>date</type> '2001-09-28' + <type>interval</type> '1 hour'</entry>
        <entry><type>timestamp</type> '2001-09-27 23:00'</entry>
       </row>

       <row>
2324
        <entry> <literal>-</literal> </entry>
2325 2326 2327 2328 2329
        <entry><type>time</type> '05:00' + <type>interval</type> '2 hours'</entry>
        <entry><type>time</type> '03:00'</entry>
       </row>

       <row>
2330
        <entry> <literal>-</literal> </entry>
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
        <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.

2355
    <table id="functions-datetime-table">
2356
     <title>Date/Time Functions</title>
2357
     <tgroup cols="5">
2358 2359
      <thead>
       <row>
2360 2361
	<entry>Name</entry>
	<entry>Return Type</entry>
2362 2363
	<entry>Description</entry>
	<entry>Example</entry>
2364
	<entry>Result</entry>
2365 2366
       </row>
      </thead>
2367

2368 2369
      <tbody>
       <row>
2370 2371
	<entry><function>age</function>(<type>timestamp</type>)</entry>
	<entry><type>interval</type></entry>
2372
	<entry>Subtract from today</entry>
2373 2374
	<entry><literal>age(timestamp '1957-06-13')</literal></entry>
	<entry><literal>43 years 8 mons 3 days</literal></entry>
2375
       </row>
2376

2377
       <row>
2378 2379
	<entry><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</entry>
	<entry><type>interval</type></entry>
2380
	<entry>Subtract arguments</entry>
2381 2382
	<entry><literal>age('2001-04-10', timestamp '1957-06-13')</literal></entry>
	<entry><literal>43 years 9 mons 27 days</literal></entry>
2383 2384 2385
       </row>

       <row>
2386 2387
	<entry><function>current_date</function></entry>
	<entry><type>date</type></entry>
2388
	<entry>Today's date; see <link
2389 2390 2391 2392
	 linkend="functions-datetime-current">below</link>
	</entry>
	<entry></entry>
	<entry></entry>
2393
       </row>
2394 2395

       <row>
2396 2397
	<entry><function>current_time</function></entry>
	<entry><type>time</type></entry>
2398
	<entry>Time of day; see <link
2399 2400 2401 2402 2403 2404
	 linkend="functions-datetime-current">below</link>
	</entry>
	<entry></entry>
	<entry></entry>
       </row>

2405
       <row>
2406 2407
	<entry><function>current_timestamp</function></entry>
	<entry><type>timestamp</type></entry>
2408
	<entry>date and time; see also <link
2409 2410 2411 2412 2413 2414 2415
	 linkend="functions-datetime-current">below</link>
	</entry>
	<entry></entry>
	<entry></entry>
       </row>

       <row>
2416 2417
	<entry><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</entry>
	<entry><type>double precision</type></entry>
2418
	<entry>Get subfield (equivalent to
2419 2420 2421
	 <function>extract</function>); see also <link
	 linkend="functions-datetime-datepart">below</link>
        </entry>
2422 2423
	<entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
	<entry><literal>20</literal></entry>
2424
       </row>
2425

2426
       <row>
2427 2428
	<entry><function>date_part</function>(<type>text</type>, <type>interval</type>)</entry>
	<entry><type>double precision</type></entry>
2429
	<entry>Get subfield (equivalent to
2430 2431 2432
	 <function>extract</function>); see also <link
	 linkend="functions-datetime-datepart">below</link>
        </entry>
2433 2434
	<entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
	<entry><literal>3</literal></entry>
2435
       </row>
2436

2437
       <row>
2438 2439
	<entry><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</entry>
	<entry><type>timestamp</type></entry>
2440
	<entry>Truncate to specified precision; see also <link
2441 2442
         linkend="functions-datetime-trunc">below</link>
        </entry>
2443 2444
	<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>
2445
       </row>
2446

2447
       <row>
2448 2449
	<entry><function>extract</function>(<parameter>field</parameter> from <type>timestamp</type>)</entry>
	<entry><type>double precision</type></entry>
2450
	<entry>Get subfield; see also <link
2451 2452
         linkend="functions-datetime-extract">below</link>
        </entry>
2453 2454
	<entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
	<entry><literal>20</literal></entry>
2455
       </row>
2456 2457

       <row>
2458 2459
	<entry><function>extract</function>(<parameter>field</parameter> from <type>interval</type>)</entry>
	<entry><type>double precision</type></entry>
2460
	<entry>Get subfield; see also <link
2461 2462
         linkend="functions-datetime-extract">below</link>
        </entry>
2463 2464
	<entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
	<entry><literal>3</literal></entry>
2465 2466
       </row>

2467
       <row>
2468 2469
	<entry><function>isfinite</function>(<type>timestamp</type>)</entry>
	<entry><type>boolean</type></entry>
2470
	<entry>Test for finite time stamp (neither invalid nor infinity)</entry>
2471 2472
	<entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
	<entry><literal>true</literal></entry>
2473
       </row>
2474

2475
       <row>
2476 2477
	<entry><function>isfinite</function>(<type>interval</type>)</entry>
	<entry><type>boolean</type></entry>
2478
	<entry>Test for finite interval</entry>
2479 2480
	<entry><literal>isfinite(interval '4 hours')</literal></entry>
	<entry><literal>true</literal></entry>
2481
       </row>
2482

2483
       <row>
2484 2485
	<entry><function>now</function>()</entry>
	<entry><type>timestamp</type></entry>
2486
	<entry>Current date and time (equivalent to
2487 2488 2489 2490 2491
	 <function>current_timestamp</function>); see also <link
	 linkend="functions-datetime-current">below</link>
	</entry>
	<entry></entry>
	<entry></entry>
2492
       </row>
2493

2494
       <row>
2495 2496
	<entry><function>timeofday()</function></entry>
	<entry><type>text</type></entry>
2497
	<entry>High-precision date and time; see also <link
2498 2499
	 linkend="functions-datetime-current">below</link>
	</entry>
2500 2501
	<entry><literal>timeofday()</literal></entry>
	<entry><literal>Wed Feb 21 17:01:13.000126 2001 EST</literal></entry>
2502 2503
       </row>

2504
       <row>
2505 2506 2507 2508 2509
	<entry><function>timestamp</function>(<type>date</type>)</entry>
	<entry><type>timestamp</type></entry>
	<entry><type>date</type> to <type>timestamp</type></entry>
	<entry><literal>timestamp(date '2000-12-25')</literal></entry>
	<entry><literal>2000-12-25 00:00:00</literal></entry>
2510
       </row>
2511

2512
       <row>
2513 2514 2515 2516 2517
	<entry><function>timestamp</function>(<type>date</type>, <type>time</type>)</entry>
	<entry><type>timestamp</type></entry>
	<entry><type>date</type> and <type>time</type> to <type>timestamp</type></entry>
	<entry><literal>timestamp(date '1998-02-24',time '23:07')</literal></entry>
	<entry><literal>1998-02-24 23:07:00</literal></entry>
2518 2519 2520 2521
       </row>
      </tbody>
     </tgroup>
    </table>
2522
   </para>
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537

  <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
2538 2539
    well.)  <replaceable>field</replaceable> is an identifier or
    string that selects what field to extract from the source value.
2540 2541 2542 2543 2544 2545 2546
    The <function>extract</function> function returns values of type
    <type>double precision</type>.
    The following are valid values:

    <!-- alphabetical -->
    <variablelist>
     <varlistentry>
2547
      <term><literal>century</literal></term>
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560
      <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>
2561 2562 2563
        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.
2564 2565 2566 2567 2568
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
2569
      <term><literal>day</literal></term>
2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584
      <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>
2585
      <term><literal>decade</literal></term>
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
      <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>
2601
      <term><literal>dow</literal></term>
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
      <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>
2618
      <term><literal>doy</literal></term>
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
      <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>
2633
      <term><literal>epoch</literal></term>
2634 2635 2636
      <listitem>
       <para>
        For <type>date</type> and <type>timestamp</type> values, the
2637
        number of seconds since 1970-01-01 00:00:00-00 (Result may be
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
        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>
2655
      <term><literal>hour</literal></term>
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
      <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>
2671
      <term><literal>microseconds</literal></term>
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
      <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>
2688
      <term><literal>millennium</literal></term>
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
      <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>
2702 2703 2704
        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.
2705 2706 2707 2708 2709
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
2710
      <term><literal>milliseconds</literal></term>
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
      <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>
2727
      <term><literal>minute</literal></term>
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
      <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>
2743
      <term><literal>month</literal></term>
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
      <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>
2767
      <term><literal>quarter</literal></term>
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
      <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>
2784
      <term><literal>second</literal></term>
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
      <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>

     <varlistentry>
2805
      <term><literal>week</literal></term>
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
      <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>
2826
      <term><literal>year</literal></term>
2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
      <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>

<!--
tz
tz_hour
tz_minute
-->

    </variablelist>

2849 2850 2851
   </para>

   <para>
2852 2853 2854
    The <function>extract</function> function is primarily intended
    for computational processing.  For formatting date/time values for
    display, see <xref linkend="functions-formatting">.
2855
   </para>
2856 2857 2858

   <anchor id="functions-datetime-datepart">
   <para>
2859 2860
    The <function>date_part</function> function is modeled on the traditional
    <productname>Ingres</productname> equivalent to the
2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
    <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 已提交
2876
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
<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>

2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
   <indexterm>
    <primary>date</primary>
    <secondary>current</secondary>
   </indexterm>

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

2947
   <para>
2948 2949
    The following functions are available to obtain the current date and/or
    time:
2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
<synopsis>
CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP
</synopsis>
    Note that because of the requirements of the
    <acronym>SQL</acronym> standard, these functions must not be
    called with trailing parentheses.
   </para>

   <informalexample>
<screen>
SELECT CURRENT_TIME;
2963
<computeroutput>19:07:32</computeroutput>
2964 2965 2966 2967 2968

SELECT CURRENT_DATE;
<computeroutput>2001-02-17</computeroutput>

SELECT CURRENT_TIMESTAMP;
2969
<computeroutput>2001-02-17 19:07:32-05</computeroutput>
2970 2971 2972 2973 2974 2975 2976
</screen>
   </informalexample>

   <para>
    The function <function>now()</function> is the traditional
    <productname>Postgres</productname> equivalent to
    <function>CURRENT_TIMESTAMP</function>.
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
   </para>

   <para>
    There is also <function>timeofday()</function>, which returns current
    time to higher precision than the <function>CURRENT_TIMESTAMP</function>
    family does:
   </para>

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

   <para>
    <function>timeofday()</function> uses the operating system call
    <function>gettimeofday(2)</function>, which may have resolution as
    good as microseconds (depending on your platform); the other functions
    rely on <function>time(2)</function> which is restricted to one-second
    resolution.  For historical reasons, <function>timeofday()</function>
2998
    returns its result as a text string rather than a <type>timestamp</type> value.
2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
   </para>

   <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>
3010
    All the date/time data types also accept the special literal value
3011 3012
    <literal>now</> to specify the current date and time.  Thus,
    the following three all return the same result:
3013 3014 3015 3016 3017 3018 3019 3020
<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
3021
      value while creating a table.  The system will convert <literal>now</>
3022
      to a <type>timestamp</type> as soon as the constant is parsed, so that when
3023
      the default value is needed,
3024 3025
      the time of the table creation would be used!  The first two
      forms will not be evaluated until the default value is used,
3026 3027
      because they are function calls.  Thus they will give the desired
      behavior of defaulting to the time of row insertion.
3028 3029 3030 3031 3032
     </para>
    </note>
   </para>
  </sect2>
 </sect1>
3033 3034 3035 3036 3037 3038

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

   <para>
3039 3040 3041 3042
    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.
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
   </para>

   <table>
     <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>
3059
	<ENTRY><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></ENTRY>
3060 3061 3062 3063
       </ROW>
       <ROW>
	<ENTRY> - </ENTRY>
	<ENTRY>Translation</ENTRY>
3064
	<ENTRY><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></ENTRY>
3065 3066 3067 3068
       </ROW>
       <ROW>
	<ENTRY> * </ENTRY>
	<ENTRY>Scaling/rotation</ENTRY>
3069
	<ENTRY><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></ENTRY>
3070 3071 3072 3073
       </ROW>
       <ROW>
	<ENTRY> / </ENTRY>
	<ENTRY>Scaling/rotation</ENTRY>
3074
	<ENTRY><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></ENTRY>
3075 3076 3077 3078
       </ROW>
       <ROW>
	<ENTRY> # </ENTRY>
	<ENTRY>Intersection</ENTRY>
3079
	<ENTRY><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></ENTRY>
3080 3081 3082 3083
       </ROW>
       <ROW>
	<ENTRY> # </ENTRY>
	<ENTRY>Number of points in polygon</ENTRY>
3084
	<ENTRY><literal># '((1,0),(0,1),(-1,0))'</literal></ENTRY>
3085 3086 3087 3088
       </ROW>
       <ROW>
	<ENTRY> ## </ENTRY>
	<ENTRY>Point of closest proximity</ENTRY>
3089
	<ENTRY><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></ENTRY>
3090 3091 3092 3093
       </ROW>
       <ROW>
	<ENTRY> &amp;&amp; </ENTRY>
	<ENTRY>Overlaps?</ENTRY>
3094
	<ENTRY><literal>box '((0,0),(1,1))' &amp;&amp; box '((0,0),(2,2))'</literal></ENTRY>
3095 3096 3097 3098
       </ROW>
       <ROW>
	<ENTRY> &amp;&lt; </ENTRY>
	<ENTRY>Overlaps to left?</ENTRY>
3099
	<ENTRY><literal>box '((0,0),(1,1))' &amp;&lt; box '((0,0),(2,2))'</literal></ENTRY>
3100 3101 3102 3103
       </ROW>
       <ROW>
	<ENTRY> &amp;&gt; </ENTRY>
	<ENTRY>Overlaps to right?</ENTRY>
3104
	<ENTRY><literal>box '((0,0),(3,3))' &amp;&gt; box '((0,0),(2,2))'</literal></ENTRY>
3105 3106 3107 3108
       </ROW>
       <ROW>
	<ENTRY> &lt;-&gt; </ENTRY>
	<ENTRY>Distance between</ENTRY>
3109
	<ENTRY><literal>circle '((0,0),1)' &lt;-&gt; circle '((5,0),1)'</literal></ENTRY>
3110 3111 3112 3113
       </ROW>
       <ROW>
	<ENTRY> &lt;&lt; </ENTRY>
	<ENTRY>Left of?</ENTRY>
3114
	<ENTRY><literal>circle '((0,0),1)' &lt;&lt; circle '((5,0),1)'</literal></ENTRY>
3115 3116 3117 3118
       </ROW>
       <ROW>
	<ENTRY> &lt;^ </ENTRY>
	<ENTRY>Is below?</ENTRY>
3119
	<ENTRY><literal>circle '((0,0),1)' &lt;^ circle '((0,5),1)'</literal></ENTRY>
3120 3121 3122 3123
       </ROW>
       <ROW>
	<ENTRY> &gt;&gt; </ENTRY>
	<ENTRY>Is right of?</ENTRY>
3124
	<ENTRY><literal>circle '((5,0),1)' &gt;&gt; circle '((0,0),1)'</literal></ENTRY>
3125 3126 3127 3128
       </ROW>
       <ROW>
	<ENTRY> &gt;^ </ENTRY>
	<ENTRY>Is above?</ENTRY>
3129
	<ENTRY><literal>circle '((0,5),1)' >^ circle '((0,0),1)'</literal></ENTRY>
3130 3131 3132 3133
       </ROW>
       <ROW>
	<ENTRY> ?# </ENTRY>
	<ENTRY>Intersects or overlaps</ENTRY>
3134
	<ENTRY><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></ENTRY>
3135 3136 3137 3138
       </ROW>
       <ROW>
	<ENTRY> ?- </ENTRY>
	<ENTRY>Is horizontal?</ENTRY>
3139
	<ENTRY><literal>point '(1,0)' ?- point '(0,0)'</literal></ENTRY>
3140 3141 3142 3143
       </ROW>
       <ROW>
	<ENTRY> ?-| </ENTRY>
	<ENTRY>Is perpendicular?</ENTRY>
3144
	<ENTRY><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></ENTRY>
3145 3146 3147 3148
       </ROW>
       <ROW>
	<ENTRY> @-@  </ENTRY>
	<ENTRY>Length or circumference</ENTRY>
3149
	<ENTRY><literal>@-@ path '((0,0),(1,0))'</literal></ENTRY>
3150 3151 3152 3153
       </ROW>
       <ROW>
	<ENTRY> ?| </ENTRY>
	<ENTRY>Is vertical?</ENTRY>
3154
	<ENTRY><literal>point '(0,1)' ?| point '(0,0)'</literal></ENTRY>
3155 3156 3157 3158
       </ROW>
       <ROW>
	<ENTRY> ?|| </ENTRY>
	<ENTRY>Is parallel?</ENTRY>
3159
	<ENTRY><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></ENTRY>
3160 3161 3162 3163
       </ROW>
       <ROW>
	<ENTRY> @ </ENTRY>
	<ENTRY>Contained or on</ENTRY>
3164
	<ENTRY><literal>point '(1,1)' @ circle '((0,0),2)'</literal></ENTRY>
3165 3166 3167 3168
       </ROW>
       <ROW>
	<ENTRY> @@ </ENTRY>
	<ENTRY>Center of</ENTRY>
3169
	<ENTRY><literal>@@ circle '((0,0),10)'</literal></ENTRY>
3170 3171 3172 3173
       </ROW>
       <ROW>
	<ENTRY> ~= </ENTRY>
	<ENTRY>Same as</ENTRY>
3174
	<ENTRY><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></ENTRY>
3175 3176 3177 3178 3179 3180
       </ROW>
      </TBODY>
     </TGROUP>
   </TABLE>

   <table>
T
Thomas G. Lockhart 已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192
     <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>
3193 3194
	<entry><function>area</function>(object)</entry>
	<entry><type>double precision</type></entry>
3195
	<entry>area of item</entry>
3196
	<entry><literal>area(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
3197 3198
       </row>
       <row>
3199 3200
	<entry><function>box</function>(box, box)</entry>
	<entry><type>box</type></entry>
3201
	<entry>intersection box</entry>
3202
	<entry><literal>box(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
3203 3204
       </row>
       <row>
3205 3206
	<entry><function>center</function>(object)</entry>
	<entry><type>point</type></entry>
3207
	<entry>center of item</entry>
3208
	<entry><literal>center(box '((0,0),(1,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
3209 3210
       </row>
       <row>
3211 3212
	<entry><function>diameter</function>(circle)</entry>
	<entry><type>double precision</type></entry>
3213
	<entry>diameter of circle</entry>
3214
	<entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3215 3216
       </row>
       <row>
3217 3218
	<entry><function>height</function>(box)</entry>
	<entry><type>double precision</type></entry>
3219
	<entry>vertical size of box</entry>
3220
	<entry><literal>height(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
3221 3222
       </row>
       <row>
3223 3224
	<entry><function>isclosed</function>(path)</entry>
	<entry><type>boolean</type></entry>
3225
	<entry>a closed path?</entry>
3226
	<entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3227 3228
       </row>
       <row>
3229 3230
	<entry><function>isopen</function>(path)</entry>
	<entry><type>boolean</type></entry>
3231
	<entry>an open path?</entry>
3232
	<entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
3233 3234
       </row>
       <row>
3235 3236
	<entry><function>length</function>(object)</entry>
	<entry><type>double precision</type></entry>
3237
	<entry>length of item</entry>
3238
	<entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3239 3240
       </row>
       <row>
3241 3242
	<entry><function>pclose</function>(path)</entry>
	<entry><type>path</type></entry>
3243
	<entry>convert path to closed</entry>
3244
	<entry><literal>popen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
3245
       </row>
3246 3247
<![IGNORE[
<!-- Not defined by this name. Implements the intersection operator '#' -->
T
Thomas G. Lockhart 已提交
3248
       <row>
3249 3250
	<entry><function>point</function>(lseg,lseg)</entry>
	<entry><type>point</type></entry>
3251
	<entry>intersection</entry>
3252
	<entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
3253
       </row>
3254
]]>
T
Thomas G. Lockhart 已提交
3255
       <row>
3256 3257
	<entry><function>npoint</function>(path)</entry>
	<entry><type>integer</type></entry>
3258
	<entry>number of points</entry>
3259
	<entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
3260 3261
       </row>
       <row>
3262 3263
	<entry><function>popen</function>(path)</entry>
	<entry><type>path</type></entry>
3264
	<entry>convert path to open path</entry>
3265
	<entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3266 3267
       </row>
       <row>
3268 3269
	<entry><function>radius</function>(circle)</entry>
	<entry><type>double precision</type></entry>
3270
	<entry>radius of circle</entry>
3271
	<entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3272 3273
       </row>
       <row>
3274 3275
	<entry><function>width</function>(box)</entry>
	<entry><type>double precision</type></entry>
3276
	<entry>horizontal size</entry>
3277
	<entry><literal>width(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
3278 3279 3280
       </row>
      </tbody>
     </tgroup>
3281
   </table>
3282

3283 3284

   <table>
T
Thomas G. Lockhart 已提交
3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296
     <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>
3297 3298
	<entry><function>box</function>(<type>circle</type>)</entry>
	<entry><type>box</type></entry>
3299
	<entry>circle to box</entry>
3300
	<entry><literal>box(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3301 3302
       </row>
       <row>
3303 3304
	<entry><function>box</function>(<type>point</type>, <type>point</type>)</entry>
	<entry><type>box</type></entry>
3305
	<entry>points to box</entry>
3306
	<entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
T
Thomas G. Lockhart 已提交
3307 3308
       </row>
       <row>
3309 3310
	<entry><function>box</function>(<type>polygon</type>)</entry>
	<entry><type>box</type></entry>
3311
	<entry>polygon to box</entry>
3312
	<entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3313 3314
       </row>
       <row>
3315 3316
	<entry><function>circle</function>(<type>box</type>)</entry>
	<entry><type>circle</type></entry>
3317
	<entry>to circle</entry>
3318
	<entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
3319 3320
       </row>
       <row>
3321 3322
	<entry><function>circle</function>(<type>point</type>, <type>double precision</type>)</entry>
	<entry><type>circle</type></entry>
3323
	<entry>point to circle</entry>
3324
	<entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
T
Thomas G. Lockhart 已提交
3325 3326
       </row>
       <row>
3327 3328
	<entry><function>lseg</function>(<type>box</type>)</entry>
	<entry><type>lseg</type></entry>
3329
	<entry>box diagonal to lseg</entry>
3330
	<entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3331 3332
       </row>
       <row>
3333 3334
	<entry><function>lseg</function>(<type>point</type>, <type>point</type>)</entry>
	<entry><type>lseg</type></entry>
3335
	<entry>points to lseg</entry>
3336
	<entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3337 3338
       </row>
       <row>
3339 3340
	<entry><function>path</function>(<type>polygon</type>)</entry>
	<entry><type>point</type></entry>
3341
	<entry>polygon to path</entry>
3342
	<entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3343 3344
       </row>
       <row>
3345 3346
	<entry><function>point</function>(<type>circle</type>)</entry>
	<entry><type>point</type></entry>
3347
	<entry>center</entry>
3348
	<entry><literal>point(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3349 3350
       </row>
       <row>
3351 3352
	<entry><function>point</function>(<type>lseg</type>, <type>lseg</type>)</entry>
	<entry><type>point</type></entry>
3353
	<entry>intersection</entry>
3354
	<entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
3355 3356
       </row>
       <row>
3357 3358
	<entry><function>point</function>(<type>polygon</type>)</entry>
	<entry><type>point</type></entry>
3359
	<entry>center</entry>
3360
	<entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3361 3362
       </row>
       <row>
3363 3364
	<entry><function>polygon</function>(<type>box</type>)</entry>
	<entry><type>polygon</type></entry>
3365
	<entry>12 point polygon</entry>
3366
	<entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
3367 3368
       </row>
       <row>
3369 3370
	<entry><function>polygon</function>(<type>circle</type>)</entry>
	<entry><type>polygon</type></entry>
3371
	<entry>12-point polygon</entry>
3372
	<entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3373 3374
       </row>
       <row>
3375 3376
	<entry><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</entry>
	<entry><type>polygon</type></entry>
3377
	<entry><replaceable class="parameter">npts</replaceable> polygon</entry>
3378
	<entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
3379 3380
       </row>
       <row>
3381 3382
	<entry><function>polygon</function>(<type>path</type>)</entry>
	<entry><type>polygon</type></entry>
3383
	<entry>path to polygon</entry>
3384
	<entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
3385 3386 3387
       </row>
      </tbody>
     </tgroup>
3388 3389
   </table>

3390
  </sect1>
3391

3392 3393

  <sect1 id="functions-net">
3394
   <title>Network Address Type Functions</title>
3395

3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410

    <table tocentry="1" id="cidr-inet-operators-table">
     <title><type>cidr</> and <type>inet</> 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>
3411
	<ENTRY><literal>inet '192.168.1.5' &lt; inet '192.168.1.6'</literal></ENTRY>
3412 3413 3414 3415
       </ROW>
       <ROW>
	<ENTRY> &lt;= </ENTRY>
	<ENTRY>Less than or equal</ENTRY>
3416
	<ENTRY><literal>inet '192.168.1.5' &lt;= inet '192.168.1.5'</literal></ENTRY>
3417 3418 3419 3420
       </ROW>
       <ROW>
	<ENTRY> = </ENTRY>
	<ENTRY>Equals</ENTRY>
3421
	<ENTRY><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></ENTRY>
3422 3423 3424 3425
       </ROW>
       <ROW>
	<ENTRY> &gt;= </ENTRY>
	<ENTRY>Greater or equal</ENTRY>
3426
	<ENTRY><literal>inet '192.168.1.5' &gt;= inet '192.168.1.5'</literal></ENTRY>
3427 3428 3429 3430
       </ROW>
       <ROW>
	<ENTRY> &gt; </ENTRY>
	<ENTRY>Greater</ENTRY>
3431
	<ENTRY><literal>inet '192.168.1.5' &gt; inet '192.168.1.4'</literal></ENTRY>
3432 3433 3434 3435
       </ROW>
       <ROW>
	<ENTRY> &lt;&gt; </ENTRY>
	<ENTRY>Not equal</ENTRY>
3436
	<ENTRY><literal>inet '192.168.1.5' &lt;&gt; inet '192.168.1.4'</literal></ENTRY>
3437 3438 3439 3440
       </ROW>
       <ROW>
	<ENTRY> &lt;&lt; </ENTRY>
	<ENTRY>is contained within</ENTRY>
3441
	<ENTRY><literal>inet '192.168.1.5' &lt;&lt; inet '192.168.1/24'</literal></ENTRY>
3442 3443 3444 3445
       </ROW>
       <ROW>
	<ENTRY> &lt;&lt;= </ENTRY>
	<ENTRY>is contained within or equals</ENTRY>
3446
	<ENTRY><literal>inet '192.168.1/24' &lt;&lt;= inet '192.168.1/24'</literal></ENTRY>
3447 3448 3449 3450
       </ROW>
       <ROW>
	<ENTRY> &gt;&gt; </ENTRY>
	<ENTRY>contains</ENTRY>
3451
	<ENTRY><literal>inet'192.168.1/24' &gt;&gt; inet '192.168.1.5'</literal></ENTRY>
3452 3453 3454 3455
       </ROW>
       <ROW>
	<ENTRY> &gt;&gt;= </ENTRY>
	<ENTRY>contains or equals</ENTRY>
3456
	<ENTRY><literal>inet '192.168.1/24' &gt;&gt;= inet '192.168.1/24'</literal></ENTRY>
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
       </ROW>
      </TBODY>
     </TGROUP>
    </TABLE>

    <para>
     All of the operators for <type>inet</type> can be applied to
     <type>cidr</type> values as well.  The operators
     <literal>&lt;&lt;</>, <literal>&lt;&lt;=</>,
     <literal>&gt;&gt;</>, <literal>&gt;&gt;=</>
     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>


3473 3474
    <table tocentry="1" id="cidr-inet-functions">
     <title><type>cidr</> and <type>inet</> Functions</title>
3475
     <tgroup cols="5">
T
Thomas G. Lockhart 已提交
3476 3477 3478 3479 3480 3481
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
3482
	<entry>Result</entry>
T
Thomas G. Lockhart 已提交
3483 3484 3485 3486
       </row>
      </thead>
      <tbody>
       <row>
3487 3488
	<entry><function>broadcast</function>(<type>inet</type>)</entry>
	<entry><type>inet</type></entry>
3489
	<entry>broadcast address for network</entry>
3490 3491
	<entry><literal>broadcast('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.255/24</literal></entry>
T
Thomas G. Lockhart 已提交
3492 3493
       </row>
       <row>
3494 3495
	<entry><function>host</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
3496
	<entry>extract IP address as text</entry>
3497 3498
	<entry><literal>host('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.5</literal></entry>
T
Thomas G. Lockhart 已提交
3499 3500
       </row>
       <row>
3501 3502
	<entry><function>masklen</function>(<type>inet</type>)</entry>
	<entry><type>integer</type></entry>
3503
	<entry>extract netmask length</entry>
3504 3505
	<entry><literal>masklen('192.168.1.5/24')</literal></entry>
	<entry><literal>24</literal></entry>
T
Thomas G. Lockhart 已提交
3506
       </row>
B
Bruce Momjian 已提交
3507
       <row>
3508 3509
	<entry><function>set_masklen</function>(<type>inet</type>,<type>integer</type>)</entry>
	<entry><type>inet</type></entry>
B
Bruce Momjian 已提交
3510
	<entry>set netmask length for inet value</entry>
3511 3512
	<entry><literal>set_masklen('192.168.1.5/24',16)</literal></entry>
	<entry><literal>192.168.1.5/16</literal></entry>
B
Bruce Momjian 已提交
3513
       </row>
T
Thomas G. Lockhart 已提交
3514
       <row>
3515 3516
	<entry><function>netmask</function>(<type>inet</type>)</entry>
	<entry><type>inet</type></entry>
3517
	<entry>construct netmask for network</entry>
3518 3519
	<entry><literal>netmask('192.168.1.5/24')</literal></entry>
	<entry><literal>255.255.255.0</literal></entry>
3520 3521
       </row>
       <row>
3522 3523
	<entry><function>network</function>(<type>inet</type>)</entry>
	<entry><type>cidr</type></entry>
3524
	<entry>extract network part of address</entry>
3525 3526
	<entry><literal>network('192.168.1.5/24')</literal></entry>
	<entry><literal>192.168.1.0/24</literal></entry>
3527 3528
       </row>
       <row>
3529 3530
	<entry><function>text</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
3531
	<entry>extract IP address and masklen as text</entry>
3532 3533
	<entry><literal>text(inet '192.168.1.5')</literal></entry>
	<entry><literal>192.168.1.5/32</literal></entry>
3534
       </row>
3535
       <row>
3536 3537
	<entry><function>abbrev</function>(<type>inet</type>)</entry>
	<entry><type>text</type></entry>
3538
	<entry>extract abbreviated display as text</entry>
3539 3540
	<entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
	<entry><literal>10.1/16</literal></entry>
3541
       </row>
3542 3543 3544 3545 3546 3547
      </tbody>
     </tgroup>
    </table>

   <para>
    All of the functions for <type>inet</type> can be applied to
3548 3549
    <type>cidr</type> values as well.  The <function>host</>(),
    <function>text</>(), and <function>abbrev</>() functions are primarily
B
Bruce Momjian 已提交
3550
    intended to offer alternative display formats. You can cast a text
3551 3552
    field to inet using normal casting syntax: <literal>inet(expression)</literal> or 
    <literal>colname::inet</literal>.
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
   </para>

   <para>
    <table tocentry="1" id="macaddr-functions">
     <title><type>macaddr</> Functions</title>
     <tgroup cols="5">
      <thead>
       <row>
	<entry>Function</entry>
	<entry>Returns</entry>
	<entry>Description</entry>
	<entry>Example</entry>
	<entry>Result</entry>
T
Thomas G. Lockhart 已提交
3566
       </row>
3567 3568
      </thead>
      <tbody>
3569
       <row>
3570 3571
	<entry><function>trunc</function>(<type>macaddr</type>)</entry>
	<entry><type>macaddr</type></entry>
3572
	<entry>set last 3 bytes to zero</entry>
3573 3574
	<entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
	<entry><literal>12:34:56:00:00:00</literal></entry>
3575
       </row>
T
Thomas G. Lockhart 已提交
3576 3577 3578
      </tbody>
     </tgroup>
    </table>
3579
   </para>
3580

3581
   <para>
3582 3583 3584 3585 3586
    The function <function>trunc</>(<type>macaddr</>) returns a MAC
    address with the last 3 bytes set to 0.  This can be used to
    associate the remaining prefix with a manufacturer.  The directory
    <filename>contrib/mac</> in the source distribution contains some
    utilities to create and maintain such an association table.
T
Thomas G. Lockhart 已提交
3587
   </para>
3588

3589 3590 3591 3592 3593 3594
   <para>
    The <type>macaddr</> type also supports the standard relational
    operators (<literal>&gt;</>, <literal>&lt;=</>, etc.) for
    lexicographical ordering.
   </para>

T
Thomas G. Lockhart 已提交
3595
  </sect1>
3596

3597 3598 3599 3600

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

3601 3602 3603 3604 3605 3606 3607 3608
  <indexterm>
   <primary>case</primary>
  </indexterm>

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

3609
  <para>
3610
   This section describes the <acronym>SQL</acronym>-compliant conditional expressions
3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
   available in <productname>Postgres</productname>.
  </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>

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

<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
3634
   other languages.  <token>CASE</token> clauses can be used wherever
3635
   an expression is valid.  <replaceable>condition</replaceable> is an
3636
   expression that returns a <type>boolean</type> result.  If the result is true
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
   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
   omitted and no condition matches, the result is NULL.
  </para>

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

3660 3661 3662 3663 3664 3665
<prompt>=&gt;</prompt> <userinput>SELECT a,
          CASE WHEN a=1 THEN 'one'
               WHEN a=2 THEN 'two'
               ELSE 'other'
          END
    FROM test;</userinput>
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
<computeroutput>
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</computeroutput>
</screen>
   </para>
  </informalexample>

  <para>
3678 3679 3680
   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.
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
  </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
   <token>ELSE</token> clause (or NULL) is returned.  This is similar
   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>
3707 3708 3709 3710 3711 3712
<prompt>=&gt;</prompt> <userinput>SELECT a,
          CASE a WHEN 1 THEN 'one'
                 WHEN 2 THEN 'two'
                 ELSE 'other'
          END
    FROM test;</userinput>
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
<computeroutput>
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</computeroutput>
</screen>
    </para>
   </informalexample>

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

<synopsis>
<function>COALESCE</function>(<replaceable>value</replaceable><optional>, ...</optional>)
</synopsis>

  <para>
   The <function>COALESCE</function> function returns the first of its
   arguments that is not NULL.  This is often useful to substitute a
   default value for NULL values when data is retrieved for display,
   for example:
<programlisting>
SELECT COALESCE(description, short_description, '(none)') ...
</programlisting>
  </para>

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

3742 3743 3744 3745
  <indexterm>
   <primary>nullif</primary>
  </indexterm>

3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760
<synopsis>
<function>NULLIF</function>(<replaceable>value1</replaceable>, <replaceable>value2</replaceable>)
</synopsis>

  <para>
   The <function>NULLIF</function> function returns NULL if and only
   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>
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773

  <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>

3774 3775 3776 3777
 </sect1>


  <sect1 id="functions-misc">
3778
   <title>Miscellaneous Functions</>
3779 3780

   <table>
3781
    <title>Session Information Functions</>
3782 3783
    <tgroup cols="3">
     <thead>
3784
      <row><entry>Name</> <entry>Return Type</> <entry>Description</></row>
3785 3786 3787 3788 3789 3790 3791 3792 3793
     </thead>

     <tbody>
      <row>
       <entry>current_user</>
       <entry>name</>
       <entry>user name of current execution context</>
      </row>
      <row>
3794
       <entry>session_user</>
3795
       <entry>name</>
3796
       <entry>session user name</>
3797 3798
      </row>
      <row>
3799
       <entry>user</>
3800
       <entry>name</>
3801
       <entry>equivalent to <function>current_user</></>
3802 3803 3804 3805 3806
      </row>
     </tbody>
    </tgroup>
   </table>

3807 3808 3809 3810 3811
   <indexterm zone="functions-misc">
    <primary>user</primary>
    <secondary>current</secondary>
   </indexterm>

3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
   <para>
    The <function>session_user</> is the user that initiated a database
    connection and is fixed for the duration of that connection. The
    <function>current_user</> is the user identifier that is applicable
    for permission checking. Currently it is always equal to the session
    user, but in the future there might be <quote>setuid</> functions and
    other facilities to allow the current user to change temporarily.
    In Unix parlance, the session user is the <quote>real user</>
    and the current user is the <quote>effective user</>.
   </para>

   <para>
    Note that these functions have special syntactic status in <acronym>SQL</>;
    they must be called without trailing parentheses.
   </para>

   <note>
    <title>Deprecated</>
    <para>
     The function <function>getpgusername()</> is an obsolete equivalent
     of <function>current_user</>.
    </para>
   </note>
3835 3836 3837 3838 3839 3840 3841 3842 3843 3844

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

     <tbody>
      <row>
3845
       <entry><function>has_table_privilege</function>(<parameter>user</parameter>,
3846 3847 3848
                                  <parameter>table</parameter>,
                                  <parameter>access</parameter>)
       </entry>
3849
       <entry><type>boolean</type></>
3850 3851 3852
       <entry>does user have access to table</>
      </row>
      <row>
3853
       <entry><function>has_table_privilege</function>(<parameter>table</parameter>,
3854 3855
                                  <parameter>access</parameter>)
       </entry>
3856
       <entry><type>boolean</type></>
3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
       <entry>does current user have access to table</>
      </row>
     </tbody>
    </tgroup>
   </table>

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

   <para>
    <function>has_table_privilege</> determines whether a user
    can access a table in a particular way.  The user can be
3870
    specified by name or by ID (<classname>pg_user</>.<structfield>usesysid</>) or if the argument is omitted
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880
    <function>current_user</> is assumed.  The table can be specified
    by name or by OID.  (Thus, there are actually six variants of
    <function>has_table_privilege</>, which can be distinguished by
    the number and types of their arguments.)  The desired access type
    is specified by a text string, which must evaluate to one of the
    values <literal>SELECT</>, <literal>INSERT</>, <literal>UPDATE</>,
    <literal>DELETE</>, <literal>RULE</>, <literal>REFERENCES</>, or
    <literal>TRIGGER</>.  (Case of the string is not significant, however.)
   </para>

3881 3882
  </sect1>

3883

3884 3885
 <sect1 id="functions-aggregate">
  <title>Aggregate Functions</title>
3886

3887 3888
  <note>
   <title>Author</title>
3889
   <para>
3890
    Written by Isaac Wilcox <email>isaac@azartmedia.com</email> on 2000-06-16
3891
   </para>
3892
  </note>
3893

3894 3895 3896 3897 3898 3899 3900
  <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
   linkend="syntax-aggregates">.  Consult the <citetitle>PostgreSQL
   Tutorial</citetitle> for additional introductory information.
  </para>
3901

3902 3903
  <table tocentry="1">
   <title>Aggregate Functions</title>
3904

3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
   <tgroup cols="3">
    <thead>
     <row>
      <entry>Function</entry>
      <entry>Description</entry>
      <entry>Notes</entry>
     </row>
    </thead>

    <tbody>
     <row>
      <entry>AVG(<replaceable class="parameter">expression</replaceable>)</entry>
      <entry>the average (arithmetic mean) of all input values</entry>
      <entry>
3919 3920 3921 3922
       <indexterm>
        <primary>average</primary>
        <secondary>function</secondary>
       </indexterm>
3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
       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
       input, <type>double precision</type> for floating point input,
       otherwise the same as the input data type.
      </entry>
     </row>

     <row>
3934
      <entry><function>count</function>(*)</entry>
3935
      <entry>number of input values</entry>
3936
      <entry>The return value is of type <type>bigint</type>.</entry>
3937 3938 3939
     </row>

     <row>
3940
      <entry><function>count</function>(<replaceable class="parameter">expression</replaceable>)</entry>
3941 3942 3943 3944
      <entry>
       Counts the input values for which the value of <replaceable
       class="parameter">expression</replaceable> is not NULL.
      </entry>
3945
      <entry>The return value is of type <type>bigint</type>.</entry>
3946 3947 3948
     </row>

     <row>
3949
      <entry><function>max</function>(<replaceable class="parameter">expression</replaceable>)</entry>
3950 3951 3952 3953 3954 3955 3956 3957
      <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>
3958
      <entry><function>min</function>(<replaceable class="parameter">expression</replaceable>)</entry>
3959 3960 3961 3962 3963 3964 3965 3966
      <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>
3967
      <entry><function>stddev</function>(<replaceable class="parameter">expression</replaceable>)</entry>
3968 3969
      <entry>the sample standard deviation of the input values</entry>
      <entry>
3970 3971 3972
       <indexterm>
        <primary>standard deviation</primary>
       </indexterm>
3973 3974 3975 3976 3977 3978 3979 3980 3981 3982
       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
       <type>double precision</type> for floating point input,
       otherwise <type>numeric</type>.
      </entry>
     </row>

     <row>
3983
      <entry><function>sum</function>(<replaceable class="parameter">expression</replaceable>)</entry>
3984 3985 3986 3987 3988 3989
      <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>.
3990 3991 3992
       The result is of type <type>bigint</type> for <type>smallint</type>
       or <type>integer</type> input, <type>numeric</type> for
       <type>bigint</type> 
3993 3994 3995 3996 3997 3998
       input, <type>double precision</type> for floating point input,
       otherwise the same as the input data type.
      </entry>
     </row>

     <row>
3999
      <entry><function>variance</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4000 4001
      <entry>the sample variance of the input values</entry>
      <entry>
4002 4003 4004
       <indexterm>
        <primary>variance</primary>
       </indexterm>
4005
       The variance is the square of the standard deviation.  The
4006 4007
       supported data types and result types are the same as for
       standard deviation.
4008 4009 4010 4011 4012 4013
      </entry>
     </row>

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

4015 4016 4017 4018
  <para>
   It should be noted that except for <function>COUNT</function>,
   these functions return NULL when no rows are selected.  In
   particular, <function>SUM</function> of no rows returns NULL, not
4019 4020
   zero as one might expect.  <function>COALESCE</function> may be
   used to substitute zero for NULL when necessary.
4021
  </para>
4022

4023
 </sect1>
4024

4025
</chapter>
4026

4027 4028
<!-- Keep this comment at the end of the file
Local variables:
T
Thomas G. Lockhart 已提交
4029
mode:sgml
4030
sgml-omittag:nil
4031 4032 4033 4034 4035 4036 4037 4038
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 已提交
4039
sgml-local-catalogs:("/usr/lib/sgml/catalog")
4040 4041 4042
sgml-local-ecat-files:nil
End:
-->