]> granicus.if.org Git - postgresql/blob - doc/src/sgml/func.sgml
6fa708a9439c3cef4438b0e4210e1a4489f2974e
[postgresql] / doc / src / sgml / func.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.85 2001/11/21 22:33:14 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <chapter id="functions">
7  <title>Functions and Operators</title>
8
9  <indexterm zone="functions">
10   <primary>functions</primary>
11  </indexterm>
12
13  <indexterm zone="functions">
14   <primary>operators</primary>
15  </indexterm>
16
17  <para>
18   <productname>PostgreSQL</productname> provides a large number of
19   functions and operators for the built-in data types.  Users can also
20   define their own functions and operators, as described in the
21   <citetitle>Programmer's Guide</citetitle>.  The
22   <application>psql</application> commands <command>\df</command> and
23   <command>\do</command> can be used to show the list of all actually
24   available functions and operators, respectively.
25  </para>
26
27  <para>
28   If you are concerned about portability then take note that most of
29   the functions and operators described in this chapter, with the
30   exception of the most trivial arithmetic and comparison operators
31   and some explicitly marked functions, are not specified by the <acronym>SQL</acronym>
32   standard. Some of this extended functionality is present in other
33   <acronym>RDBMS</acronym> products, and in many cases this
34   functionality is compatible and consistent between various products.
35  </para>
36
37
38  <sect1 id="functions-logical">
39   <title>Logical Operators</title>
40
41   <indexterm zone="functions-logical">
42    <primary>operators</primary>
43    <secondary>logical</secondary>
44   </indexterm>
45
46   <indexterm>
47    <primary>Boolean</primary>
48    <secondary>operators</secondary>
49    <see>operators, logical</see>
50   </indexterm>
51
52   <para>
53    The usual logical operators are available:
54
55    <indexterm>
56     <primary>and</primary>
57     <secondary>operator</secondary>
58    </indexterm>
59
60    <indexterm>
61     <primary>or</primary>
62     <secondary>operator</secondary>
63    </indexterm>
64
65    <indexterm>
66     <primary>not</primary>
67     <secondary>operator</secondary>
68    </indexterm>
69
70    <simplelist>
71     <member>AND</member>
72     <member>OR</member>
73     <member>NOT</member>
74    </simplelist>
75
76    <acronym>SQL</acronym> uses a three-valued Boolean logic where NULL represents
77    <quote>unknown</quote>.  Observe the following truth tables:
78
79    <informaltable>
80     <tgroup cols="4">
81      <thead>
82       <row>
83        <entry><replaceable>a</replaceable></entry>
84        <entry><replaceable>b</replaceable></entry>
85        <entry><replaceable>a</replaceable> AND <replaceable>b</replaceable></entry>
86        <entry><replaceable>a</replaceable> OR <replaceable>b</replaceable></entry>
87       </row>
88      </thead>
89
90      <tbody>
91       <row>
92        <entry>TRUE</entry>
93        <entry>TRUE</entry>
94        <entry>TRUE</entry>
95        <entry>TRUE</entry>
96       </row>
97
98       <row>
99        <entry>TRUE</entry>
100        <entry>FALSE</entry>
101        <entry>FALSE</entry>
102        <entry>TRUE</entry>
103       </row>
104
105       <row>
106        <entry>TRUE</entry>
107        <entry>NULL</entry>
108        <entry>NULL</entry>
109        <entry>TRUE</entry>
110       </row>
111
112       <row>
113        <entry>FALSE</entry>
114        <entry>FALSE</entry>
115        <entry>FALSE</entry>
116        <entry>FALSE</entry>
117       </row>
118
119       <row>
120        <entry>FALSE</entry>
121        <entry>NULL</entry>
122        <entry>FALSE</entry>
123        <entry>NULL</entry>
124       </row>
125
126       <row>
127        <entry>NULL</entry>
128        <entry>NULL</entry>
129        <entry>NULL</entry>
130        <entry>NULL</entry>
131       </row>
132      </tbody>
133     </tgroup>
134    </informaltable>
135
136    <informaltable>
137     <tgroup cols="2">
138      <thead>
139       <row>
140        <entry><replaceable>a</replaceable></entry>
141        <entry>NOT <replaceable>a</replaceable></entry>
142       </row>
143      </thead>
144
145      <tbody>
146       <row>
147        <entry>TRUE</entry>
148        <entry>FALSE</entry>
149       </row>
150
151       <row>
152        <entry>FALSE</entry>
153        <entry>TRUE</entry>
154       </row>
155
156       <row>
157        <entry>NULL</entry>
158        <entry>NULL</entry>
159       </row>
160      </tbody>
161     </tgroup>
162    </informaltable>
163   </para>
164  </sect1>
165
166  <sect1 id="functions-comparison">
167   <title>Comparison Operators</title>
168
169   <indexterm zone="functions-comparison">
170    <primary>comparison</primary>
171    <secondary>operators</secondary>
172   </indexterm>
173
174   <table>
175    <title>Comparison Operators</TITLE>
176    <tgroup cols="2">
177     <thead>
178      <row>
179       <entry>Operator</entry>
180       <entry>Description</entry>
181      </row>
182     </thead>
183
184     <tbody>
185      <row>
186       <entry> <literal>&lt;</literal> </entry>
187       <entry>less than</entry>
188      </row>
189
190      <row>
191       <entry> <literal>&gt;</literal> </entry>
192       <entry>greater than</entry>
193      </row>
194
195      <row>
196       <entry> <literal>&lt;=</literal> </entry>
197       <entry>less than or equal to</entry>
198      </row>
199
200      <row>
201       <entry> <literal>&gt;=</literal> </entry>
202       <entry>greater than or equal to</entry>
203      </row>
204
205      <row>
206       <entry> <literal>=</literal> </entry>
207       <entry>equal</entry>
208      </row>
209
210      <row>
211       <entry> <literal>&lt;&gt;</literal> or <literal>!=</literal> </entry>
212       <entry>not equal</entry>
213      </row>
214     </tbody>
215    </tgroup>
216   </table>
217
218   <note>
219    <para>
220     The <literal>!=</literal> operator is converted to
221     <literal>&lt;&gt;</literal> in the parser stage.  It is not
222     possible to implement <literal>!=</literal> and
223     <literal>&lt;&gt;</literal> operators that do different things.
224    </para>
225   </note>
226
227   <para>
228    Comparison operators are available for all data types where this
229    makes sense.  All comparison operators are binary operators that
230    return values of type <type>boolean</type>; expressions like
231    <literal>1 &lt; 2 &lt; 3</literal> are not valid (because there is
232    no <literal>&lt;</literal> operator to compare a Boolean value with
233    <literal>3</literal>).
234   </para>
235
236   <para>
237    <indexterm>
238     <primary>between</primary>
239    </indexterm>
240    In addition to the comparison operators, the special
241    <token>BETWEEN</token> construct is available.
242 <synopsis>
243 <replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
244 </synopsis>
245    is equivalent to
246 <synopsis>
247 <replaceable>a</replaceable> &gt;= <replaceable>x</replaceable> AND <replaceable>a</replaceable> &lt;= <replaceable>y</replaceable>
248 </synopsis>
249    Similarly,
250 <synopsis>
251 <replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
252 </synopsis>
253    is equivalent to
254 <synopsis>
255 <replaceable>a</replaceable> &lt; <replaceable>x</replaceable> OR <replaceable>a</replaceable> &gt; <replaceable>y</replaceable>
256 </synopsis>
257    There is no difference between the two respective forms apart from
258    the <acronym>CPU</acronym> cycles required to rewrite the first one
259    into the second one internally.
260   </para>
261
262   <para>
263    To check whether a value is or is not NULL, use the constructs
264 <synopsis>
265 <replaceable>expression</replaceable> IS NULL
266 <replaceable>expression</replaceable> IS NOT NULL
267 </synopsis>
268    Do <emphasis>not</emphasis> use
269    <literal><replaceable>expression</replaceable> = NULL</literal>
270    because NULL is not <quote>equal to</quote> NULL.  (NULL represents
271    an unknown value, and it is not known whether two unknown values are
272    equal.)
273   </para>
274
275   <para>
276    Some applications may (incorrectly) require that
277    <literal><replaceable>expression</replaceable> = NULL</literal>
278    returns true if <replaceable>expression</replaceable> evaluates to
279    the NULL value.  To support these applications, the run-time option
280    <varname>transform_null_equals</varname> can be turned on (e.g.,
281    <literal>SET transform_null_equals TO ON;</literal>).
282    <productname>PostgreSQL</productname> would then convert <literal>x
283    = NULL</literal> clauses to <literal>x IS NULL</literal>.  This was
284    the default behavior in releases 6.5 through 7.1.
285   </para>
286
287   <para>
288    Boolean values can also be tested using the constructs
289 <synopsis>
290 <replaceable>expression</replaceable> IS TRUE
291 <replaceable>expression</replaceable> IS NOT TRUE
292 <replaceable>expression</replaceable> IS FALSE
293 <replaceable>expression</replaceable> IS NOT FALSE
294 <replaceable>expression</replaceable> IS UNKNOWN
295 <replaceable>expression</replaceable> IS NOT UNKNOWN
296 </synopsis>
297    These are similar to <literal>IS NULL</literal> in that they will
298    always return TRUE or FALSE, never NULL, even when the operand is NULL.
299    A NULL input is treated as the logical value UNKNOWN.
300   </para>
301  </sect1>
302
303
304  <sect1 id="functions-math">
305   <title>Mathematical Functions and Operators</title>
306
307   <para>
308    Mathematical operators are provided for many
309    <productname>PostgreSQL</productname> types. For types without
310    common mathematical conventions for all possible permutations 
311    (e.g. date/time types) we
312    describe the actual behavior in subsequent sections.
313   </para>
314
315   <table>
316    <title>Mathematical Operators</title>
317
318    <tgroup cols="4">
319     <thead>
320      <row>
321       <entry>Name</entry>
322       <entry>Description</entry>
323       <entry>Example</entry>
324       <entry>Result</entry>
325      </row>
326     </thead>
327
328     <tbody>
329      <row>
330       <entry> <literal>+</literal> </entry>
331       <entry>Addition</entry>
332       <entry>2 + 3</entry>
333       <entry>5</entry>
334      </row>
335
336      <row>
337       <entry> <literal>-</literal> </entry>
338       <entry>Subtraction</entry>
339       <entry>2 - 3</entry>
340       <entry>-1</entry>
341      </row>
342
343      <row>
344       <entry> <literal>*</literal> </entry>
345       <entry>Multiplication</entry>
346       <entry>2 * 3</entry>
347       <entry>6</entry>
348      </row>
349
350      <row>
351       <entry> <literal>/</literal> </entry>
352       <entry>Division (integer division truncates results)</entry>
353       <entry>4 / 2</entry>
354       <entry>2</entry>
355      </row>
356
357      <row>
358       <entry> <literal>%</literal> </entry>
359       <entry>Modulo (remainder)</entry>
360       <entry>5 % 4</entry>
361       <entry>1</entry>
362      </row>
363
364      <row>
365       <entry> <literal>^</literal> </entry>
366       <entry>Exponentiation</entry>
367       <entry>2.0 ^ 3.0</entry>
368       <entry>8</entry>
369      </row>
370
371      <row>
372       <entry> <literal>|/</literal> </entry>
373       <entry>Square root</entry>
374       <entry>|/ 25.0</entry>
375       <entry>5</entry>
376      </row>
377
378      <row>
379       <entry> <literal>||/</literal> </entry>
380       <entry>Cube root</entry>
381       <entry>||/ 27.0</entry>
382       <entry>3</entry>
383      </row>
384
385      <row>
386       <entry> <literal>!</literal> </entry>
387       <entry>Factorial</entry>
388       <entry>5 !</entry>
389       <entry>120</entry>
390      </row>
391
392      <row>
393       <entry> <literal>!!</literal> </entry>
394       <entry>Factorial (prefix operator)</entry>
395       <entry>!! 5</entry>
396       <entry>120</entry>
397      </row>
398
399      <row>
400       <entry> <literal>@</literal> </entry>
401       <entry>Absolute value</entry>
402       <entry>@ -5.0</entry>
403       <entry>5</entry>
404      </row>
405
406      <row>
407       <entry> <literal>&amp;</literal> </entry>
408       <entry>Binary AND</entry>
409       <entry>91 & 15</entry>
410       <entry>11</entry>
411      </row>
412
413      <row>
414       <entry> <literal>|</literal> </entry>
415       <entry>Binary OR</entry>
416       <entry>32 | 3</entry>
417       <entry>35</entry>
418      </row>
419
420      <row>
421       <entry> <literal>#</literal> </entry>
422       <entry>Binary XOR</entry>
423       <entry>17 # 5</entry>
424       <entry>20</entry>
425      </row>
426
427      <row>
428       <entry> <literal>~</literal> </entry>
429       <entry>Binary NOT</entry>
430       <entry>~1</entry>
431       <entry>-2</entry>
432      </row>
433
434      <row>
435       <entry> &lt;&lt; </entry>
436       <entry>Binary shift left</entry>
437       <entry>1 &lt;&lt; 4</entry>
438       <entry>16</entry>
439      </row>
440
441      <row>
442       <entry> &gt;&gt; </entry>
443       <entry>Binary shift right</entry>
444       <entry>8 &gt;&gt; 2</entry>
445       <entry>2</entry>
446      </row>
447
448     </tbody>
449    </tgroup>
450   </table>
451
452   <para>
453    The <quote>binary</quote> operators are also available for the bit
454    string types <type>BIT</type> and <type>BIT VARYING</type>.
455
456    <table>
457     <title>Bit String Binary Operators</title>
458
459     <tgroup cols="2">
460      <thead>
461       <row>
462        <entry>Example</entry>
463        <entry>Result</entry>
464       </row>
465      </thead>
466
467      <tbody>
468       <row>
469        <entry>B'10001' & B'01101'</entry>
470        <entry>00001</entry>
471       </row>
472       <row>
473        <entry>B'10001' | B'01101'</entry>
474        <entry>11101</entry>
475       </row>
476       <row>
477        <entry>B'10001' # B'01101'</entry>
478        <entry>11110</entry>
479       </row>
480       <row>
481        <entry>~ B'10001'</entry>
482        <entry>01110</entry>
483       </row>
484       <row>
485        <entry>B'10001' << 3</entry>
486        <entry>01000</entry>
487       </row>
488       <row>
489        <entry>B'10001' >> 2</entry>
490        <entry>00100</entry>
491       </row>
492      </tbody>
493     </tgroup>
494    </table>
495
496    Bit string arguments to <literal>&</literal>, <literal>|</literal>,
497    and <literal>#</literal> must be of equal length.  When bit
498    shifting, the original length of the string is preserved, as shown
499    here.
500   </para>
501
502   <table tocentry="1">
503    <title>Mathematical Functions</title>
504    <tgroup cols="5">
505     <thead>
506      <row>
507       <entry>Function</entry>
508       <entry>Return Type</entry>
509       <entry>Description</entry>
510       <entry>Example</entry>
511       <entry>Result</entry>
512      </row>
513     </thead>
514
515     <tbody>
516      <row>
517       <entry><function>abs</function>(<replaceable>x</replaceable>)</entry>
518       <entry>(same as x)</entry>
519       <entry>absolute value</entry>
520       <entry><literal>abs(-17.4)</literal></entry>
521       <entry>17.4</entry>
522      </row>
523
524      <row>
525       <entry><function>cbrt</function>(<type>dp</type>)</entry>
526       <entry><type>dp</type></entry>
527       <entry>cube root</entry>
528       <entry><literal>cbrt(27.0)</literal></entry>
529       <entry>3</entry>
530      </row>
531
532      <row>
533       <entry><function>ceil</function>(<type>numeric</type>)</entry>
534       <entry><type>numeric</type></entry>
535       <entry>smallest integer not less than argument</entry>
536       <entry><literal>ceil(-42.8)</literal></entry>
537       <entry>-42</entry>
538      </row>
539
540      <row>
541       <entry><function>degrees</function>(<type>dp</type>)</entry>
542       <entry><type>dp</type></entry>
543       <entry>radians to degrees</entry>
544       <entry><literal>degrees(0.5)</literal></entry>
545       <entry>28.6478897565412</entry>
546      </row>
547
548      <row>
549       <entry><function>exp</function>(<type>dp</type>)</entry>
550       <entry><type>dp</type></entry>
551       <entry>exponential</entry>
552       <entry><literal>exp(1.0)</literal></entry>
553       <entry>2.71828182845905</entry>
554      </row>
555
556      <row>
557       <entry><function>floor</function>(<type>numeric</type>)</entry>
558       <entry><type>numeric</type></entry>
559       <entry>largest integer not greater than argument</entry>
560       <entry><literal>floor(-42.8)</literal></entry>
561       <entry>-43</entry>
562      </row>
563
564      <row>
565       <entry><function>ln</function>(<type>dp</type>)</entry>
566       <entry><type>dp</type></entry>
567       <entry>natural logarithm</entry>
568       <entry><literal>ln(2.0)</literal></entry>
569       <entry>0.693147180559945</entry>
570      </row>
571
572      <row>
573       <entry><function>log</function>(<type>dp</type>)</entry>
574       <entry><type>dp</type></entry>
575       <entry>base 10 logarithm</entry>
576       <entry><literal>log(100.0)</literal></entry>
577       <entry>2</entry>
578      </row>
579
580      <row>
581       <entry><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
582        <parameter>x</parameter> <type>numeric</type>)</entry>
583       <entry><type>numeric</type></entry>
584       <entry>logarithm to base <parameter>b</parameter></entry>
585       <entry><literal>log(2.0, 64.0)</literal></entry>
586       <entry>6.0000000000</entry>
587      </row>
588
589      <row>
590       <entry><function>mod</function>(<parameter>y</parameter>, <parameter>x</parameter>)</entry>
591       <entry>(same as argument types)</entry>
592       <entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
593       <entry><literal>mod(9,4)</literal></entry>
594       <entry>1</entry>
595      </row>
596
597      <row>
598       <entry><function>pi</function>()</entry>
599       <entry><type>dp</type></entry>
600       <entry><quote>Pi</quote> constant</entry>
601       <entry><literal>pi()</literal></entry>
602       <entry>3.14159265358979</entry>
603      </row>
604
605      <row>
606       <entry><function>pow</function>(<parameter>e</parameter> <type>dp</type>,
607        <parameter>n</parameter> <type>dp</type>)</entry>
608       <entry><type>dp</type></entry>
609       <entry>raise a number to exponent <parameter>e</parameter></entry>
610       <entry><literal>pow(9.0, 3.0)</literal></entry>
611       <entry>729</entry>
612      </row>
613
614      <row>
615       <entry><function>radians</function>(<type>dp</type>)</entry>
616       <entry><type>dp</type></entry>
617       <entry>degrees to radians</entry>
618       <entry><literal>radians(45.0)</literal></entry>
619       <entry>0.785398163397448</entry>
620      </row>
621
622      <row>
623       <entry><function>random</function>()</entry>
624       <entry><type>dp</type></entry>
625       <entry>value between 0.0 to 1.0</entry>
626       <entry><literal>random()</literal></entry>
627       <entry></entry>
628      </row>
629
630      <row>
631       <entry><function>round</function>(<type>dp</type>)</entry>
632       <entry><type>dp</type></entry>
633       <entry>round to nearest integer</entry>
634       <entry><literal>round(42.4)</literal></entry>
635       <entry>42</entry>
636      </row>
637
638      <row>
639       <entry><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
640       <entry><type>numeric</type></entry>
641       <entry>round to <parameter>s</parameter> decimal places</entry>
642       <entry><literal>round(42.4382, 2)</literal></entry>
643       <entry>42.44</entry>
644      </row>
645 <!--
646      <row>
647       <entry><function>setseed</function>(<replaceable>new-seed</replaceable>)</entry>
648       <entry>set seed for subsequent random() calls</entry>
649       <entry><literal>setseed(0.54823)</literal></entry>
650       <entry></entry>
651      </row>
652 -->
653      <row>
654       <entry><function>sign</function>(<type>numeric</type>)</entry>
655       <entry><type>numeric</type></entry>
656       <entry>sign of the argument (-1, 0, +1)</entry>
657       <entry><literal>sign(-8.4)</literal></entry>
658       <entry>-1</entry>
659      </row>
660
661      <row>
662       <entry><function>sqrt</function>(<type>dp</type>)</entry>
663       <entry><type>dp</type></entry>
664       <entry>square root</entry>
665       <entry><literal>sqrt(2.0)</literal></entry>
666       <entry>1.4142135623731</entry>
667      </row>
668
669      <row>
670       <entry><function>trunc</function>(<type>dp</type>)</entry>
671       <entry><type>dp</type></entry>
672       <entry>truncate toward zero</entry>
673       <entry><literal>trunc(42.8)</literal></entry>
674       <entry>42</entry>
675      </row>
676
677      <row>
678       <entry><function>trunc</function>(<type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
679       <entry><type>numeric</type></entry>
680       <entry>truncate to <parameter>s</parameter> decimal places</entry>
681       <entry><literal>trunc(42.4382, 2)</literal></entry>
682       <entry>42.43</entry>
683      </row>
684
685     </tbody>
686    </tgroup>
687   </table>
688
689   <para>
690    In the table above, <literal>dp</literal> indicates <type>double precision</type>.
691    The functions <function>exp</function>, <function>ln</function>,
692    <function>log</function>, <function>pow</function>,
693    <function>round</function> (1 argument), <function>sqrt</function>,
694    and <function>trunc</function> (1 argument) are also available for
695    the type <type>numeric</type> in place of <type>double
696    precision</type>.
697    Functions returning a <type>numeric</type> result take
698    <type>numeric</type> input arguments, unless otherwise specified.
699    Many of these functions are implemented on top
700    of the host system's C library; accuracy and behavior in boundary cases
701    could therefore vary depending on the host system.
702   </para>
703
704   <table>
705    <title>Trigonometric Functions</title>
706
707    <tgroup cols="2">
708     <thead>
709      <row>
710       <entry>Function</entry>
711       <entry>Description</entry>
712      </row>
713     </thead>
714
715     <tbody>
716      <row>
717       <entry><function>acos</function>(<replaceable>x</replaceable>)</entry>
718       <entry>inverse cosine</entry>
719      </row>
720
721      <row>
722       <entry><function>asin</function>(<replaceable>x</replaceable>)</entry>
723       <entry>inverse sine</entry>
724      </row>
725
726      <row>
727       <entry><function>atan</function>(<replaceable>x</replaceable>)</entry>
728       <entry>inverse tangent</entry>
729      </row>
730
731      <row>
732       <entry><function>atan2</function>(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</entry>
733       <entry>inverse tangent of <replaceable>y</replaceable>/<replaceable>x</replaceable></entry>
734      </row>
735
736      <row>
737       <entry><function>cos</function>(<replaceable>x</replaceable>)</entry>
738       <entry>cosine</entry>
739      </row>
740
741      <row>
742       <entry><function>cot</function>(<replaceable>x</replaceable>)</entry>
743       <entry>cotangent</entry>
744      </row>
745
746      <row>
747       <entry><function>sin</function>(<replaceable>x</replaceable>)</entry>
748       <entry>sine</entry>
749      </row>
750
751      <row>
752       <entry><function>tan</function>(<replaceable>x</replaceable>)</entry>
753       <entry>tangent</entry>
754      </row>
755     </tbody>
756    </tgroup>
757   </table>
758
759   <para>
760    All trigonometric functions have arguments and return values of
761    type <type>double precision</type>.
762   </para>
763
764  </sect1>
765
766
767  <sect1 id="functions-string">
768   <title>String Functions and Operators</title>
769
770   <para>
771    This section describes functions and operators for examining and
772    manipulating string values.  Strings in this context include values
773    of all the types <type>CHARACTER</type>, <type>CHARACTER
774    VARYING</type>, and <type>TEXT</type>.  Unless otherwise noted, all
775    of the functions listed below work on all of these types, but be
776    wary of potential effects of the automatic padding when using the
777    <type>CHARACTER</type> type.  Generally the functions described
778    here also work on data of non-string types by converting that data
779    to a string representation first.  Some functions also exist
780    natively for bit string types.
781   </para>
782
783   <para>
784    <acronym>SQL</acronym> defines some string functions with a special syntax where
785    certain keywords rather than commas are used to separate the
786    arguments.  Details are in <xref linkend="functions-string-sql">.
787    These functions are also implemented using the regular syntax for
788    function invocation.  (See <xref linkend="functions-string-other">.)
789   </para>
790
791   <table id="functions-string-sql">
792    <title><acronym>SQL</acronym> String Functions and Operators</title>
793    <tgroup cols="5">
794     <thead>
795      <row>
796       <entry>Function</entry>
797       <entry>Return Type</entry>
798       <entry>Description</entry>
799       <entry>Example</entry>
800       <entry>Result</entry>  
801      </row>
802     </thead>
803
804     <tbody>
805      <row>
806       <entry> <parameter>string</parameter> <literal>||</literal> <parameter>string</parameter> </entry>
807       <entry> <type>text</type> </entry>
808       <entry>
809        string concatenation
810        <indexterm>
811         <primary>character strings</primary>
812         <secondary>concatenation</secondary>
813        </indexterm>
814       </entry>
815       <entry><literal>'Postgre' || 'SQL'</></entry>
816       <entry><literal>PostgreSQL</></entry>
817      </row>
818
819      <row>
820       <entry><function>bit_length</function>(<parameter>string</parameter>)</entry>
821       <entry><type>integer</type></entry>
822       <entry>number of bits in string</entry>
823       <entry><literal>bit_length('jose')</literal></entry>
824       <entry><literal>32</literal></entry>
825      </row>
826
827      <row>
828       <entry><function>char_length</function>(<parameter>string</parameter>) or <function>character_length</function>(<parameter>string</parameter>)</entry>
829       <entry><type>integer</type></entry>
830       <entry>
831        number of characters in string
832        <indexterm>
833         <primary>character strings</primary>
834         <secondary>length</secondary>
835        </indexterm>
836        <indexterm>
837         <primary>length</primary>
838         <secondary>character strings</secondary>
839         <see>character strings, length</see>
840        </indexterm>
841       </entry>
842       <entry><literal>char_length('jose')</></entry>
843       <entry><literal>4</></entry>
844      </row>
845
846      <row>
847       <entry><function>lower</function>(<parameter>string</parameter>)</entry>
848       <entry><type>text</type></entry>
849       <entry>Convert string to lower case.</entry>
850       <entry><literal>lower('TOM')</literal></entry>
851       <entry><literal>tom</literal></entry>
852      </row>
853
854      <row>
855       <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
856       <entry><type>integer</type></entry>
857       <entry>number of bytes in string</entry>
858       <entry><literal>octet_length('jose')</literal></entry>
859       <entry><literal>4</literal></entry>
860      </row>
861
862      <row>
863       <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
864       <entry><type>integer</type></entry>
865       <entry>location of specified substring</entry>
866       <entry><literal>position('om' in 'Thomas')</literal></entry>
867       <entry><literal>3</literal></entry>
868      </row>
869
870      <row>
871       <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
872       <entry><type>text</type></entry>
873       <entry>
874        extract substring
875        <indexterm>
876         <primary>substring</primary>
877        </indexterm>
878       </entry>
879       <entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
880       <entry><literal>hom</literal></entry>
881      </row>
882
883      <row>
884       <entry>
885        <function>trim</function>(<optional>leading | trailing | both</optional>
886        <optional><parameter>characters</parameter></optional> from
887        <parameter>string</parameter>)
888       </entry>
889       <entry><type>text</type></entry>
890       <entry>
891        Removes the longest string containing only the
892        <parameter>characters</parameter> (a space by default) from the
893        beginning/end/both ends of the <parameter>string</parameter>.
894       </entry>
895       <entry><literal>trim(both 'x' from 'xTomxx')</literal></entry>
896       <entry><literal>Tom</literal></entry>
897      </row>
898
899      <row>
900       <entry><function>upper</function>(<parameter>string</parameter>)</entry>
901       <entry><type>text</type></entry>
902       <entry>Convert string to upper case.</entry>
903       <entry><literal>upper('tom')</literal></entry>
904       <entry><literal>TOM</literal></entry>
905      </row>
906     </tbody>
907    </tgroup>
908   </table>
909
910   <para>
911    Additional string manipulation functions are available and are
912    listed below.  Some of them are used internally to implement the
913    <acronym>SQL</acronym>-standard string functions listed above.
914   </para>
915
916   <table id="functions-string-other">
917    <title>Other String Functions</title>
918    <tgroup cols="5">
919     <thead>
920      <row>
921       <entry>Function</entry>
922       <entry>Return Type</entry>
923       <entry>Description</entry>
924       <entry>Example</entry>
925       <entry>Result</entry>
926      </row>
927     </thead>
928
929     <tbody>
930      <row>
931       <entry><function>ascii</function>(<type>text</type>)</entry>
932       <entry>integer</entry>
933       <entry>Returns the <acronym>ASCII</acronym> code of the first character of the argument.</entry>
934       <entry><literal>ascii('x')</literal></entry>
935       <entry><literal>120</literal></entry>
936      </row>
937
938      <row>
939       <entry><function>btrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
940       <entry><type>text</type></entry>
941       <entry>
942        Remove (trim) the longest string consisting only of characters
943        in <parameter>trim</parameter> from the start and end of
944        <parameter>string</parameter>.
945       </entry>
946       <entry><literal>btrim('xyxtrimyyx','xy')</literal></entry>
947       <entry><literal>trim</literal></entry>
948      </row>
949
950      <row>
951       <entry><function>chr</function>(<type>integer</type>)</entry>
952       <entry><type>text</type></entry>
953       <entry>Returns the character with the given <acronym>ASCII</acronym> code.</entry>
954       <entry><literal>chr(65)</literal></entry>
955       <entry><literal>A</literal></entry>
956      </row>
957
958      <row>
959       <entry>
960        <function>convert</function>(<parameter>string</parameter> <type>text</type>,
961        <optional><parameter>src_encoding</parameter> <type>name</type>,</optional>
962        <parameter>dest_encoding</parameter> <type>name</type>)
963       </entry>
964       <entry><type>text</type></entry>
965       <entry>
966        Converts string using <parameter>dest_encoding</parameter>.
967        The original encoding is specified by
968        <parameter>src_encoding</parameter>.  If
969        <parameter>src_encoding</parameter> is omitted, database
970        encoding is assumed.
971       </entry>
972       <entry><literal>convert('text_in_unicode', 'UNICODE', 'LATIN1')</literal></entry>
973       <entry><literal>text_in_unicode</literal> represented in ISO 8859-1</entry>
974      </row>
975
976      <row>
977       <entry><function>initcap</function>(<type>text</type>)</entry>
978       <entry><type>text</type></entry>
979       <entry>Converts first letter of each word (whitespace separated) to upper case.</entry>
980       <entry><literal>initcap('hi thomas')</literal></entry>
981       <entry><literal>Hi Thomas</literal></entry>
982      </row>
983
984      <row>
985       <entry><function>length</function>(<parameter>string</parameter>)</entry>
986       <entry><type>integer</type></entry>
987       <entry>
988        length of string
989        <indexterm>
990         <primary>character strings</primary>
991         <secondary>length</secondary>
992        </indexterm>
993        <indexterm>
994         <primary>length</primary>
995         <secondary>character strings</secondary>
996         <see>character strings, length</see>
997        </indexterm>
998       </entry>
999       <entry><literal>length('jose')</></entry>
1000       <entry><literal>4</></entry>
1001      </row>
1002
1003      <row>
1004       <entry>
1005        <function>lpad</function>(<parameter>string</parameter> <type>text</type>,
1006        <parameter>length</parameter> <type>integer</type>
1007        <optional>, <parameter>fill</parameter> <type>text</type></optional>)
1008       </entry>
1009       <entry>text</entry>
1010       <entry>
1011        Fills up the <parameter>string</parameter> to length
1012        <parameter>length</parameter> by prepending the characters
1013        <parameter>fill</parameter> (a space by default).  If the
1014        <parameter>string</parameter> is already longer than
1015        <parameter>length</parameter> then it is truncated (on the
1016        right).
1017       </entry>
1018       <entry><literal>lpad('hi', 5, 'xy')</literal></entry>
1019       <entry><literal>xyxhi</literal></entry>
1020      </row>
1021
1022      <row>
1023       <entry><function>ltrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
1024       <entry><type>text</type></entry>
1025       <entry>
1026        Removes the longest string containing only characters from
1027        <parameter>trim</parameter> from the start of the string.
1028       </entry>
1029       <entry><literal>ltrim('zzzytrim','xyz')</literal></entry>
1030       <entry><literal>trim</literal></entry>
1031      </row>
1032
1033      <row>
1034       <entry><function>pg_client_encoding</function>()</entry>
1035       <entry><type>name</type></entry>
1036       <entry>
1037        Returns current client encoding name.
1038       </entry>
1039       <entry><literal>pg_client_encoding()</literal></entry>
1040       <entry><literal>SQL_ASCII</literal></entry>
1041      </row>
1042
1043      <row>
1044       <entry><function>repeat</function>(<type>text</type>, <type>integer</type>)</entry>
1045       <entry><type>text</type></entry>
1046       <entry>Repeat text a number of times.</entry>
1047       <entry><literal>repeat('Pg', 4)</literal></entry>
1048       <entry><literal>PgPgPgPg</literal></entry>
1049      </row>
1050
1051      <row>
1052       <entry>
1053        <function>rpad</function>(<parameter>string</parameter> <type>text</type>,
1054        <parameter>length</parameter> <type>integer</type>
1055        <optional>, <parameter>fill</parameter> <type>text</type></optional>)
1056       </entry>
1057       <entry><type>text</type></entry>
1058       <entry>
1059        Fills up the <parameter>string</parameter> to length
1060        <parameter>length</parameter> by appending the characters
1061        <parameter>fill</parameter> (a space by default).  If the
1062        <parameter>string</parameter> is already longer than
1063        <parameter>length</parameter> then it is truncated.
1064       </entry>
1065       <entry><literal>rpad('hi', 5, 'xy')</literal></entry>
1066       <entry><literal>hixyx</literal></entry>
1067      </row>
1068
1069      <row>
1070       <entry><function>rtrim</function>(<parameter>string</parameter> text, <parameter>trim</parameter> text)</entry>
1071       <entry><type>text</type></entry>
1072       <entry>
1073        Removes the longest string containing only characters from
1074        <parameter>trim</parameter> from the end of the string.
1075       </entry>
1076       <entry><literal>rtrim('trimxxxx','x')</literal></entry>
1077       <entry><literal>trim</literal></entry>
1078      </row>
1079
1080      <row>
1081       <entry><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</entry>
1082       <entry><type>text</type></entry>
1083       <entry>
1084        Locates specified substring. (same as
1085        <literal>position(<parameter>substring</parameter> in
1086        <parameter>string</parameter>)</literal>, but note the reversed
1087        argument order)
1088       </entry>
1089       <entry><literal>strpos('high','ig')</literal></entry>
1090       <entry><literal>2</literal></entry>
1091      </row>
1092
1093      <row>
1094       <entry><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</entry>
1095       <entry><type>text</type></entry>
1096       <entry>
1097        Extracts specified substring. (same as <literal>substring(<parameter>string</parameter> from <parameter>from</parameter> for <parameter>count</parameter>)</literal>)
1098       </entry>
1099       <entry><literal>substr('alphabet', 3, 2)</literal></entry>
1100       <entry><literal>ph</literal></entry>
1101      </row>
1102
1103      <row>
1104       <entry><function>to_ascii</function>(<type>text</type> <optional>, <parameter>encoding</parameter></optional>)</entry>
1105       <entry><type>text</type></entry>
1106       <entry>Converts text from multibyte encoding to <acronym>ASCII</acronym>.</entry>
1107       <entry><literal>to_ascii('Karel')</literal></entry>
1108       <entry><literal>Karel</literal></entry>
1109      </row>
1110
1111      <row>
1112       <entry>
1113        <function>translate</function>(<parameter>string</parameter> <type>text</type>,
1114        <parameter>from</parameter> <type>text</type>,
1115        <parameter>to</parameter> <type>text</type>)
1116       </entry>
1117       <entry><type>text</type></entry>
1118       <entry>
1119        Any character in <parameter>string</parameter> that matches a
1120        character in the <parameter>from</parameter> set is replaced by
1121        the corresponding character in the <parameter>to</parameter>
1122        set.
1123       </entry>
1124       <entry><literal>translate('12345', '14', 'ax')</literal></entry>
1125       <entry><literal>a23x5</literal></entry>
1126      </row>       
1127      
1128      <row>
1129       <entry>
1130        <function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
1131               <parameter>type</parameter> <type>text</type>)
1132       </entry>
1133       <entry><type>text</type></entry>
1134       <entry>
1135        Encodes binary data to <acronym>ASCII</acronym>-only representation.  Supported
1136        types are: 'base64', 'hex', 'escape'.
1137       </entry>
1138       <entry><literal>encode('123\\000\\001', 'base64')</literal></entry>
1139       <entry><literal>MTIzAAE=</literal></entry>
1140      </row>       
1141
1142      <row>
1143       <entry>
1144        <function>decode</function>(<parameter>string</parameter> <type>text</type>,
1145               <parameter>type</parameter> <type>text</type>)
1146       </entry>
1147       <entry><type>bytea</type></entry>
1148       <entry>
1149        Decodes binary data from <parameter>string</parameter> previously 
1150        encoded with encode().  Parameter type is same as in encode().
1151       </entry>
1152       <entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
1153       <entry><literal>123\000\001</literal></entry>
1154      </row>       
1155
1156     </tbody>
1157    </tgroup>
1158   </table>
1159
1160   <para>
1161    The <function>to_ascii</function> function supports conversion from
1162    LATIN1, LATIN2, WIN1250 (CP1250) only.
1163   </para>
1164  </sect1>
1165
1166
1167  <sect1 id="functions-binarystring">
1168   <title>Binary String Functions and Operators</title>
1169
1170   <para>
1171    This section describes functions and operators for examining and
1172    manipulating binary string values.  Strings in this context include
1173    values of the type <type>BYTEA</type>.
1174   </para>
1175
1176   <para>
1177    <acronym>SQL</acronym> defines some string functions with a special syntax where
1178    certain keywords rather than commas are used to separate the
1179    arguments.  Details are in <xref linkend="functions-binarystring-sql">.
1180    Some functions are also implemented using the regular syntax for
1181    function invocation.  (See <xref linkend="functions-binarystring-other">.)
1182   </para>
1183
1184   <table id="functions-binarystring-sql">
1185    <title><acronym>SQL</acronym> Binary String Functions and Operators</title>
1186    <tgroup cols="5">
1187     <thead>
1188      <row>
1189       <entry>Function</entry>
1190       <entry>Return Type</entry>
1191       <entry>Description</entry>
1192       <entry>Example</entry>
1193       <entry>Result</entry>  
1194      </row>
1195     </thead>
1196
1197     <tbody>
1198      <row>
1199       <entry> <parameter>string</parameter> <literal>||</literal> <parameter>string</parameter> </entry>
1200       <entry> <type>bytea</type> </entry>
1201       <entry>
1202        string concatenation
1203        <indexterm>
1204         <primary>binary strings</primary>
1205         <secondary>concatenation</secondary>
1206        </indexterm>
1207       </entry>
1208       <entry><literal>'\\\\Postgre'::bytea || '\\047SQL\\000'::bytea</></entry>
1209       <entry><literal>\\Postgre'SQL\000</></entry>
1210      </row>
1211
1212      <row>
1213       <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
1214       <entry><type>integer</type></entry>
1215       <entry>number of bytes in binary string</entry>
1216       <entry><literal>octet_length('jo\\000se'::bytea)</literal></entry>
1217       <entry><literal>5</literal></entry>
1218      </row>
1219
1220      <row>
1221       <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
1222       <entry><type>integer</type></entry>
1223       <entry>location of specified substring</entry>
1224       <entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
1225       <entry><literal>3</literal></entry>
1226      </row>
1227
1228      <row>
1229       <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
1230       <entry><type>bytea</type></entry>
1231       <entry>
1232        extract substring
1233        <indexterm>
1234         <primary>substring</primary>
1235        </indexterm>
1236       </entry>
1237       <entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
1238       <entry><literal>h\000o</literal></entry>
1239      </row>
1240
1241      <row>
1242       <entry>
1243        <function>trim</function>(<optional>both</optional>
1244        <parameter>characters</parameter> from
1245        <parameter>string</parameter>)
1246       </entry>
1247       <entry><type>bytea</type></entry>
1248       <entry>
1249        Removes the longest string containing only the
1250        <parameter>characters</parameter> from the
1251        beginning/end/both ends of the <parameter>string</parameter>.
1252       </entry>
1253       <entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
1254       <entry><literal>Tom</literal></entry>
1255      </row>
1256
1257     </tbody>
1258    </tgroup>
1259   </table>
1260
1261   <para>
1262    Additional binary string manipulation functions are available and are
1263    listed below.  Some of them are used internally to implement the
1264    <acronym>SQL</acronym>-standard string functions listed above.
1265   </para>
1266
1267   <table id="functions-binarystring-other">
1268    <title>Other Binary String Functions</title>
1269    <tgroup cols="5">
1270     <thead>
1271      <row>
1272       <entry>Function</entry>
1273       <entry>Return Type</entry>
1274       <entry>Description</entry>
1275       <entry>Example</entry>
1276       <entry>Result</entry>
1277      </row>
1278     </thead>
1279
1280     <tbody>
1281      <row>
1282       <entry><function>btrim</function>(<parameter>string</parameter> <type>bytea</type>, <parameter>trim</parameter> <type>bytea</type>)</entry>
1283       <entry><type>bytea</type></entry>
1284       <entry>
1285        Remove (trim) the longest string consisting only of characters
1286        in <parameter>trim</parameter> from the start and end of
1287        <parameter>string</parameter>.
1288       </entry>
1289       <entry><literal>btrim('\\000trim\\000'::bytea,'\\000'::bytea)</literal></entry>
1290       <entry><literal>trim</literal></entry>
1291      </row>
1292
1293      <row>
1294       <entry><function>length</function>(<parameter>string</parameter>)</entry>
1295       <entry><type>integer</type></entry>
1296       <entry>
1297        length of binary string
1298        <indexterm>
1299         <primary>binary strings</primary>
1300         <secondary>length</secondary>
1301        </indexterm>
1302        <indexterm>
1303         <primary>length</primary>
1304         <secondary>binary strings</secondary>
1305         <see>binary strings, length</see>
1306        </indexterm>
1307       </entry>
1308       <entry><literal>length('jo\\000se'::bytea)</></entry>
1309       <entry><literal>5</></entry>
1310      </row>
1311
1312      <row>
1313       <entry>
1314        <function>encode</function>(<parameter>string</parameter> <type>bytea</type>,
1315               <parameter>type</parameter> <type>text</type>)
1316       </entry>
1317       <entry><type>text</type></entry>
1318       <entry>
1319        Encodes binary string to <acronym>ASCII</acronym>-only representation.  Supported
1320        types are: 'base64', 'hex', 'escape'.
1321       </entry>
1322       <entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
1323       <entry><literal>123\000456</literal></entry>
1324      </row>       
1325
1326      <row>
1327       <entry>
1328        <function>decode</function>(<parameter>string</parameter> <type>text</type>,
1329               <parameter>type</parameter> <type>text</type>)
1330       </entry>
1331       <entry><type>bytea</type></entry>
1332       <entry>
1333        Decodes binary string from <parameter>string</parameter> previously 
1334        encoded with encode().  Parameter type is same as in encode().
1335       </entry>
1336       <entry><literal>decode('123\\000456', 'escape')</literal></entry>
1337       <entry><literal>123\000456</literal></entry>
1338      </row>       
1339
1340     </tbody>
1341    </tgroup>
1342   </table>
1343
1344  </sect1>
1345
1346
1347  <sect1 id="functions-matching">
1348   <title>Pattern Matching</title>
1349
1350   <para>
1351    There are two separate approaches to pattern matching provided by
1352    <productname>PostgreSQL</productname>:  the <acronym>SQL</acronym>
1353    <function>LIKE</function> operator and
1354    <acronym>POSIX</acronym>-style regular expressions.
1355   </para>
1356
1357   <tip>
1358    <para>
1359     If you have pattern matching needs that go beyond this, or want to
1360     make pattern-driven substitutions or translations, consider
1361     writing a user-defined function in Perl or Tcl.
1362    </para>
1363   </tip>
1364
1365   <sect2 id="functions-like">
1366    <title>Pattern Matching with <function>LIKE</function></title>
1367
1368    <indexterm>
1369     <primary>like</primary>
1370    </indexterm>
1371
1372 <synopsis>
1373 <replaceable>string</replaceable> LIKE <replaceable>pattern</replaceable> <optional> ESCAPE <replaceable>escape-character</replaceable> </optional>
1374 <replaceable>string</replaceable> NOT LIKE <replaceable>pattern</replaceable> <optional> ESCAPE <replaceable>escape-character</replaceable> </optional>
1375 </synopsis>
1376
1377    <para>
1378     Every <replaceable>pattern</replaceable> defines a set of strings.
1379     The <function>LIKE</function> expression returns true if the
1380     <replaceable>string</replaceable> is contained in the set of
1381     strings represented by <replaceable>pattern</replaceable>.  (As
1382     expected, the <function>NOT LIKE</function> expression returns
1383     false if <function>LIKE</function> returns true, and vice versa.
1384     An equivalent expression is <literal>NOT
1385     (<replaceable>string</replaceable> LIKE
1386     <replaceable>pattern</replaceable>)</literal>.)
1387    </para>
1388
1389    <para>
1390     If <replaceable>pattern</replaceable> does not contain percent
1391     signs or underscore, then the pattern only represents the string
1392     itself; in that case <function>LIKE</function> acts like the
1393     equals operator.  An underscore (<literal>_</literal>) in
1394     <replaceable>pattern</replaceable> stands for (matches) any single
1395     character; a percent sign (<literal>%</literal>) matches any string
1396     of zero or more characters.
1397    </para>
1398
1399    <informalexample>
1400     <para>
1401      Some examples:
1402 <programlisting>
1403 'abc' LIKE 'abc'    <lineannotation>true</lineannotation>
1404 'abc' LIKE 'a%'     <lineannotation>true</lineannotation>
1405 'abc' LIKE '_b_'    <lineannotation>true</lineannotation>
1406 'abc' LIKE 'c'      <lineannotation>false</lineannotation>
1407 </programlisting>
1408     </para>
1409    </informalexample>
1410
1411    <para>
1412     <function>LIKE</function> pattern matches always cover the entire
1413     string.  To match a pattern anywhere within a string, the
1414     pattern must therefore start and end with a percent sign.
1415    </para>
1416
1417    <para>
1418     To match a literal underscore or percent sign without matching
1419     other characters, the respective character in
1420     <replaceable>pattern</replaceable> must be 
1421     preceded by the escape character.  The default escape
1422     character is the backslash but a different one may be selected by
1423     using the <literal>ESCAPE</literal> clause.  To match the escape
1424     character itself, write two escape characters.
1425    </para>
1426
1427    <para>
1428     Note that the backslash already has a special meaning in string
1429     literals, so to write a pattern constant that contains a backslash
1430     you must write two backslashes in the query.  Thus, writing a pattern
1431     that actually matches a literal backslash means writing four backslashes
1432     in the query.  You can avoid this by selecting a different escape
1433     character with <literal>ESCAPE</literal>; then backslash isn't special
1434     to <function>LIKE</> anymore. (But it's still special to the string
1435     literal parser, so you still need two of them.)
1436    </para>
1437
1438    <para>
1439     It's also possible to select no escape character by writing
1440     <literal>ESCAPE ''</literal>.  In this case there is no way to
1441     turn off the special meaning of underscore and percent signs in
1442     the pattern.
1443    </para>
1444
1445    <para>
1446     The keyword <token>ILIKE</token> can be used instead of
1447     <token>LIKE</token> to make the match case insensitive according
1448     to the active locale.  This is not in the <acronym>SQL</acronym> standard but is a
1449     <productname>PostgreSQL</productname> extension.
1450    </para>
1451
1452    <para>
1453     The operator <literal>~~</literal> is equivalent to
1454     <function>LIKE</function>, and <literal>~~*</literal> corresponds to
1455     <function>ILIKE</function>.  There are also
1456     <literal>!~~</literal> and <literal>!~~*</literal> operators that
1457     represent <function>NOT LIKE</function> and <function>NOT
1458     ILIKE</function>.  All of these operators are
1459     <productname>PostgreSQL</productname>-specific.
1460    </para>
1461   </sect2>
1462
1463
1464   <sect2 id="functions-regexp">
1465    <title><acronym>POSIX</acronym> Regular Expressions</title>
1466
1467    <indexterm zone="functions-regexp">
1468     <primary>regular expressions</primary>
1469     <seealso>pattern matching</seealso>
1470    </indexterm>
1471
1472    <table>
1473     <title>Regular Expression Match Operators</title>
1474
1475     <tgroup cols="3">
1476      <thead>
1477       <row>
1478        <entry>Operator</entry>
1479        <entry>Description</entry>
1480        <entry>Example</entry>
1481       </row>
1482      </thead>
1483
1484      <tbody>
1485        <ROW>
1486         <ENTRY> <literal>~</literal> </ENTRY>
1487         <ENTRY>Matches regular expression, case sensitive</ENTRY>
1488         <ENTRY><literal>'thomas' ~ '.*thomas.*'</literal></ENTRY>
1489        </ROW>
1490        <ROW>
1491         <ENTRY> <literal>~*</literal> </ENTRY>
1492         <ENTRY>Matches regular expression, case insensitive</ENTRY>
1493         <ENTRY><literal>'thomas' ~* '.*Thomas.*'</literal></ENTRY>
1494        </ROW>
1495        <ROW>
1496         <ENTRY> <literal>!~</literal> </ENTRY>
1497         <ENTRY>Does not match regular expression, case sensitive</ENTRY>
1498         <ENTRY><literal>'thomas' !~ '.*Thomas.*'</literal></ENTRY>
1499        </ROW>
1500        <ROW>
1501         <ENTRY> <literal>!~*</literal> </ENTRY>
1502         <ENTRY>Does not match regular expression, case insensitive</ENTRY>
1503         <ENTRY><literal>'thomas' !~* '.*vadim.*'</literal></ENTRY>
1504        </ROW>
1505      </tbody>
1506     </tgroup>
1507    </table>
1508
1509    <para>
1510     <acronym>POSIX</acronym> regular expressions provide a more powerful means for
1511     pattern matching than the <function>LIKE</function> function.
1512     Many Unix tools such as <command>egrep</command>,
1513     <command>sed</command>, or <command>awk</command> use a pattern
1514     matching language that is similar to the one described here.
1515    </para>
1516
1517    <para>
1518     A regular expression is a character sequence that is an
1519     abbreviated definition of a set of strings (a <firstterm>regular
1520     set</firstterm>).  A string is said to match a regular expression
1521     if it is a member of the regular set described by the regular
1522     expression.  As with <function>LIKE</function>, pattern characters
1523     match string characters exactly unless they are special characters
1524     in the regular expression language --- but regular expressions use
1525     different special characters than <function>LIKE</function> does.
1526     Unlike <function>LIKE</function> patterns, a
1527     regular expression is allowed to match anywhere within a string, unless
1528     the regular expression is explicitly anchored to the beginning or
1529     end of the string.
1530    </para>
1531
1532
1533 <!-- derived from the re_format.7 man page -->
1534    <para>
1535     Regular expressions (<quote>RE</quote>s), as defined in <acronym>POSIX</acronym>
1536     1003.2, come in two forms: modern REs (roughly those of
1537     <command>egrep</command>; 1003.2 calls these
1538     <quote>extended</quote> REs) and obsolete REs (roughly those of
1539     <command>ed</command>; 1003.2 <quote>basic</quote> REs).
1540     <productname>PostgreSQL</productname> implements the modern form.
1541    </para>
1542
1543    <para>
1544     A (modern) RE is one or more non-empty
1545     <firstterm>branches</firstterm>, separated by
1546     <literal>|</literal>.  It matches anything that matches one of the
1547     branches.
1548    </para>
1549
1550    <para>
1551     A branch is one or more <firstterm>pieces</firstterm>,
1552     concatenated.  It matches a match for the first, followed by a
1553     match for the second, etc.
1554    </para>
1555
1556    <para>
1557     A piece is an <firstterm>atom</firstterm> possibly followed by a
1558     single <literal>*</literal>, <literal>+</literal>,
1559     <literal>?</literal>, or <firstterm>bound</firstterm>.  An atom
1560     followed by <literal>*</literal> matches a sequence of 0 or more
1561     matches of the atom.  An atom followed by <literal>+</literal>
1562     matches a sequence of 1 or more matches of the atom.  An atom
1563     followed by <literal>?</literal> matches a sequence of 0 or 1
1564     matches of the atom.
1565    </para>
1566
1567    <para>
1568     A <firstterm>bound</firstterm> is <literal>{</literal> followed by
1569     an unsigned decimal integer, possibly followed by
1570     <literal>,</literal> possibly followed by another unsigned decimal
1571     integer, always followed by <literal>}</literal>.  The integers
1572     must lie between 0 and <symbol>RE_DUP_MAX</symbol> (255)
1573     inclusive, and if there are two of them, the first may not exceed
1574     the second.  An atom followed by a bound containing one integer
1575     <replaceable>i</replaceable> and no comma matches a sequence of
1576     exactly <replaceable>i</replaceable> matches of the atom.  An atom
1577     followed by a bound containing one integer
1578     <replaceable>i</replaceable> and a comma matches a sequence of
1579     <replaceable>i</replaceable> or more matches of the atom.  An atom
1580     followed by a bound containing two integers
1581     <replaceable>i</replaceable> and <replaceable>j</replaceable>
1582     matches a sequence of <replaceable>i</replaceable> through
1583     <replaceable>j</replaceable> (inclusive) matches of the atom.
1584    </para>
1585
1586    <note>
1587     <para>
1588      A repetition operator (<literal>?</literal>,
1589      <literal>*</literal>, <literal>+</literal>, or bounds) cannot
1590      follow another repetition operator.  A repetition operator cannot
1591      begin an expression or subexpression or follow
1592      <literal>^</literal> or <literal>|</literal>.
1593     </para>
1594    </note>
1595
1596    <para>
1597     An <firstterm>atom</firstterm> is a regular expression enclosed in
1598     <literal>()</literal> (matching a match for the regular
1599     expression), an empty set of <literal>()</literal> (matching the
1600     null string), a <firstterm>bracket expression</firstterm> (see
1601     below), <literal>.</literal> (matching any single character),
1602     <literal>^</literal> (matching the null string at the beginning of the
1603     input string), <literal>$</literal> (matching the null string at the end
1604     of the input string), a <literal>\</literal> followed by one of the
1605     characters <literal>^.[$()|*+?{\</literal> (matching that
1606     character taken as an ordinary character), a <literal>\</literal>
1607     followed by any other character (matching that character taken as
1608     an ordinary character, as if the <literal>\</literal> had not been
1609     present), or a single character with no other significance
1610     (matching that character).  A <literal>{</literal> followed by a
1611     character other than a digit is an ordinary character, not the
1612     beginning of a bound.  It is illegal to end an RE with
1613     <literal>\</literal>.
1614    </para>
1615
1616    <para>
1617     Note that the backslash (<literal>\</literal>) already has a special
1618     meaning in string
1619     literals, so to write a pattern constant that contains a backslash
1620     you must write two backslashes in the query.
1621    </para>
1622
1623    <para>
1624     A <firstterm>bracket expression</firstterm> is a list of
1625     characters enclosed in <literal>[]</literal>.  It normally matches
1626     any single character from the list (but see below).  If the list
1627     begins with <literal>^</literal>, it matches any single character
1628     (but see below) not from the rest of the list.  If two characters
1629     in the list are separated by <literal>-</literal>, this is
1630     shorthand for the full range of characters between those two
1631     (inclusive) in the collating sequence,
1632     e.g. <literal>[0-9]</literal> in <acronym>ASCII</acronym> matches
1633     any decimal digit.  It is illegal for two ranges to share an
1634     endpoint, e.g.  <literal>a-c-e</literal>.  Ranges are very
1635     collating-sequence-dependent, and portable programs should avoid
1636     relying on them.
1637    </para>
1638
1639    <para>
1640     To include a literal <literal>]</literal> in the list, make it the
1641     first character (following a possible <literal>^</literal>).  To
1642     include a literal <literal>-</literal>, make it the first or last
1643     character, or the second endpoint of a range.  To use a literal
1644     <literal>-</literal> as the first endpoint of a range, enclose it
1645     in <literal>[.</literal> and <literal>.]</literal> to make it a
1646     collating element (see below).  With the exception of these and
1647     some combinations using <literal>[</literal> (see next
1648     paragraphs), all other special characters, including
1649     <literal>\</literal>, lose their special significance within a
1650     bracket expression.
1651    </para>
1652
1653    <para>
1654     Within a bracket expression, a collating element (a character, a
1655     multiple-character sequence that collates as if it were a single
1656     character, or a collating-sequence name for either) enclosed in
1657     <literal>[.</literal> and <literal>.]</literal> stands for the
1658     sequence of characters of that collating element.  The sequence is
1659     a single element of the bracket expression's list.  A bracket
1660     expression containing a multiple-character collating element can thus
1661     match more than one character, e.g. if the collating sequence
1662     includes a <literal>ch</literal> collating element, then the RE
1663     <literal>[[.ch.]]*c</literal> matches the first five characters of
1664     <literal>chchcc</literal>.
1665    </para>
1666
1667    <para>
1668     Within a bracket expression, a collating element enclosed in
1669     <literal>[=</literal> and <literal>=]</literal> is an equivalence
1670     class, standing for the sequences of characters of all collating
1671     elements equivalent to that one, including itself.  (If there are
1672     no other equivalent collating elements, the treatment is as if the
1673     enclosing delimiters were <literal>[.</literal> and
1674     <literal>.]</literal>.)  For example, if <literal>o</literal> and
1675     <literal>^</literal> are the members of an equivalence class, then
1676     <literal>[[=o=]]</literal>, <literal>[[=^=]]</literal>, and
1677     <literal>[o^]</literal> are all synonymous.  An equivalence class
1678     may not be an endpoint of a range.
1679    </para>
1680
1681    <para>
1682     Within a bracket expression, the name of a character class
1683     enclosed in <literal>[:</literal> and <literal>:]</literal> stands
1684     for the list of all characters belonging to that class.  Standard
1685     character class names are: <literal>alnum</literal>,
1686     <literal>alpha</literal>, <literal>blank</literal>,
1687     <literal>cntrl</literal>, <literal>digit</literal>,
1688     <literal>graph</literal>, <literal>lower</literal>,
1689     <literal>print</literal>, <literal>punct</literal>,
1690     <literal>space</literal>, <literal>upper</literal>,
1691     <literal>xdigit</literal>.  These stand for the character classes
1692     defined in
1693     <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
1694     A locale may provide others.  A character class may not be used as
1695     an endpoint of a range.
1696    </para>
1697
1698    <para>
1699     There are two special cases of bracket expressions:  the bracket
1700     expressions <literal>[[:<:]]</literal> and
1701     <literal>[[:>:]]</literal> match the null string at the beginning
1702     and end of a word respectively.  A word is defined as a sequence
1703     of word characters which is neither preceded nor followed by word
1704     characters.  A word character is an alnum character (as defined by
1705     <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
1706     or an underscore.  This is an extension, compatible with but not
1707     specified by <acronym>POSIX</acronym> 1003.2, and should be used with caution in
1708     software intended to be portable to other systems.
1709    </para>
1710
1711    <para>
1712     In the event that an RE could match more than one substring of a
1713     given string, the RE matches the one starting earliest in the
1714     string.  If the RE could match more than one substring starting at
1715     that point, it matches the longest.  Subexpressions also match the
1716     longest possible substrings, subject to the constraint that the
1717     whole match be as long as possible, with subexpressions starting
1718     earlier in the RE taking priority over ones starting later.  Note
1719     that higher-level subexpressions thus take priority over their
1720     lower-level component subexpressions.
1721    </para>
1722
1723    <para>
1724     Match lengths are measured in characters, not collating
1725     elements.  A null string is considered longer than no match at
1726     all.  For example, <literal>bb*</literal> matches the three middle
1727     characters of <literal>abbbc</literal>,
1728     <literal>(wee|week)(knights|nights)</literal> matches all ten
1729     characters of <literal>weeknights</literal>, when
1730     <literal>(.*).*</literal> is matched against
1731     <literal>abc</literal> the parenthesized subexpression matches all
1732     three characters, and when <literal>(a*)*</literal> is matched
1733     against <literal>bc</literal> both the whole RE and the
1734     parenthesized subexpression match the null string.
1735    </para>
1736
1737    <para>
1738     If case-independent matching is specified, the effect is much as
1739     if all case distinctions had vanished from the alphabet.  When an
1740     alphabetic that exists in multiple cases appears as an ordinary
1741     character outside a bracket expression, it is effectively
1742     transformed into a bracket expression containing both cases,
1743     e.g. <literal>x</literal> becomes <literal>[xX]</literal>.  When
1744     it appears inside a bracket expression, all case counterparts of
1745     it are added to the bracket expression, so that (e.g.)
1746     <literal>[x]</literal> becomes <literal>[xX]</literal> and
1747     <literal>[^x]</literal> becomes <literal>[^xX]</literal>.
1748    </para>
1749
1750    <para>
1751     There is no particular limit on the length of REs, except insofar
1752     as memory is limited.  Memory usage is approximately linear in RE
1753     size, and largely insensitive to RE complexity, except for bounded
1754     repetitions.  Bounded repetitions are implemented by macro
1755     expansion, which is costly in time and space if counts are large
1756     or bounded repetitions are nested.  An RE like, say,
1757     <literal>((((a{1,100}){1,100}){1,100}){1,100}){1,100}</literal>
1758     will (eventually) run almost any existing machine out of swap
1759     space.
1760     <footnote>
1761      <para>
1762       This was written in 1994, mind you.  The
1763       numbers have probably changed, but the problem
1764       persists.
1765      </para>
1766     </footnote>
1767    </para>
1768 <!-- end re_format.7 man page -->
1769   </sect2>
1770
1771  </sect1>
1772
1773
1774   <sect1 id="functions-formatting">
1775    <title>Data Type Formatting Functions</title>
1776
1777    <indexterm zone="functions-formatting">
1778     <primary>formatting</primary>
1779    </indexterm>
1780
1781    <note>
1782     <title>Author</title>
1783     <para>
1784      Written by Karel Zak (<email>zakkr@zf.jcu.cz</email>) on 2000-01-24
1785     </para>
1786    </note>
1787
1788    <para>
1789     The <productname>PostgreSQL</productname> formatting functions
1790     provide a powerful set of tools for converting various data types
1791     (date/time, integer, floating point, numeric) to formatted strings
1792     and for converting from formatted strings to specific data types.
1793     These functions all follow a common calling convention: the first
1794     argument is the value to be formatted and the second argument is a
1795     template that defines the output or input format.
1796    </para>
1797
1798    <para>
1799     <table tocentry="1">
1800      <title>Formatting Functions</title>
1801      <tgroup cols="4">
1802       <thead>
1803        <row>
1804         <entry>Function</entry>
1805         <entry>Returns</entry>
1806         <entry>Description</entry>
1807         <entry>Example</entry>
1808        </row>
1809       </thead>
1810       <tbody>
1811        <row>
1812         <entry><function>to_char</function>(<type>timestamp</type>, <type>text</type>)</entry>
1813         <entry><type>text</type></entry>
1814         <entry>convert time stamp to string</entry>
1815         <entry><literal>to_char(timestamp 'now','HH12:MI:SS')</literal></entry>
1816        </row>
1817        <row>
1818         <entry><function>to_char</function>(<type>interval</type>, <type>text</type>)</entry>
1819         <entry><type>text</type></entry>
1820         <entry>convert interval to string</entry>
1821         <entry><literal>to_char(interval '15h 2m 12s','HH24:MI:SS')</literal></entry>
1822        </row>
1823        <row>
1824         <entry><function>to_char</function>(<type>int</type>, <type>text</type>)</entry>
1825         <entry><type>text</type></entry>
1826         <entry>convert int4/int8 to string</entry>
1827         <entry><literal>to_char(125, '999')</literal></entry>
1828        </row>
1829        <row>
1830         <entry><function>to_char</function>(<type>double precision</type>, <type>text</type>)</entry>
1831         <entry><type>text</type></entry>
1832         <entry>convert real/double precision to string</entry>
1833         <entry><literal>to_char(125.8, '999D9')</literal></entry>
1834        </row>
1835        <row>
1836         <entry><function>to_char</function>(<type>numeric</type>, <type>text</type>)</entry>
1837         <entry><type>text</type></entry>
1838         <entry>convert numeric to string</entry>
1839         <entry><literal>to_char(numeric '-125.8', '999D99S')</literal></entry>
1840        </row>
1841        <row>
1842         <entry><function>to_date</function>(<type>text</type>, <type>text</type>)</entry>
1843         <entry><type>date</type></entry>
1844         <entry>convert string to date</entry>
1845         <entry><literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
1846        </row>
1847        <row>
1848         <entry><function>to_timestamp</function>(<type>text</type>, <type>text</type>)</entry>
1849         <entry><type>timestamp</type></entry>
1850         <entry>convert string to time stamp</entry>
1851         <entry><literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
1852        </row>
1853        <row>
1854         <entry><function>to_number</function>(<type>text</type>, <type>text</type>)</entry>
1855         <entry><type>numeric</type></entry>
1856         <entry>convert string to numeric</entry>
1857         <entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
1858        </row>
1859       </tbody>
1860      </tgroup>
1861     </table>
1862    </para>
1863
1864    <para>
1865     In an output template string, there are certain patterns that are
1866     recognized and replaced with appropriately-formatted data from the value
1867     to be formatted.  Any text that is not a template pattern is simply
1868     copied verbatim.  Similarly, in an input template string template patterns
1869     identify the parts of the input data string to be looked at and the
1870     values to be found there.
1871    </para>
1872
1873    <para>
1874     <table tocentry="1">
1875      <title>Template patterns for date/time conversions</title>
1876      <tgroup cols="2">
1877       <thead>
1878        <row>
1879         <entry>Pattern</entry>
1880         <entry>Description</entry>
1881        </row>
1882       </thead>
1883       <tbody>
1884        <row>
1885         <entry><literal>HH</literal></entry>
1886         <entry>hour of day (01-12)</entry>
1887        </row>
1888        <row>
1889         <entry><literal>HH12</literal></entry>
1890         <entry>hour of day (01-12)</entry>
1891        </row>       
1892        <row>
1893         <entry><literal>HH24</literal></entry>
1894         <entry>hour of day (00-23)</entry>
1895        </row>       
1896        <row>
1897         <entry><literal>MI</literal></entry>
1898         <entry>minute (00-59)</entry>
1899        </row>   
1900        <row>
1901         <entry><literal>SS</literal></entry>
1902         <entry>second (00-59)</entry>
1903        </row>
1904        <row>
1905         <entry><literal>MS</literal></entry>
1906         <entry>millisecond (000-999)</entry>
1907        </row>
1908        <row>
1909         <entry><literal>US</literal></entry>
1910         <entry>microsecond (000000-999999)</entry>
1911        </row>
1912        <row>
1913         <entry><literal>SSSS</literal></entry>
1914         <entry>seconds past midnight (0-86399)</entry>
1915        </row>
1916        <row>
1917         <entry><literal>AM</literal> or <literal>A.M.</literal> or <literal>PM</literal> or <literal>P.M.</literal></entry>
1918         <entry>meridian indicator (upper case)</entry>
1919        </row>
1920        <row>
1921         <entry><literal>am</literal> or <literal>a.m.</literal> or <literal>pm</literal> or <literal>p.m.</literal></entry>
1922         <entry>meridian indicator (lower case)</entry>
1923        </row>
1924        <row>
1925         <entry><literal>Y,YYY</literal></entry>
1926         <entry>year (4 and more digits) with comma</entry>
1927        </row>
1928        <row>
1929         <entry><literal>YYYY</literal></entry>
1930         <entry>year (4 and more digits)</entry>
1931        </row>
1932        <row>
1933         <entry><literal>YYY</literal></entry>
1934         <entry>last 3 digits of year</entry>
1935        </row>
1936        <row>
1937         <entry><literal>YY</literal></entry>
1938         <entry>last 2 digits of year</entry>
1939        </row>
1940        <row>
1941         <entry><literal>Y</literal></entry>
1942         <entry>last digit of year</entry>
1943        </row>
1944        <row>
1945         <entry><literal>BC</literal> or <literal>B.C.</literal> or <literal>AD</literal> or <literal>A.D.</literal></entry>
1946         <entry>era indicator (upper case)</entry>
1947        </row>
1948        <row>
1949         <entry><literal>bc</literal> or <literal>b.c.</literal> or <literal>ad</literal> or <literal>a.d.</literal></entry>
1950         <entry>era indicator (lower case)</entry>
1951        </row>
1952        <row>
1953         <entry><literal>MONTH</literal></entry>
1954         <entry>full upper case month name (blank-padded to 9 chars)</entry>
1955        </row>
1956        <row>
1957         <entry><literal>Month</literal></entry>
1958         <entry>full mixed case month name (blank-padded to 9 chars)</entry>
1959        </row>
1960        <row>
1961         <entry><literal>month</literal></entry>
1962         <entry>full lower case month name (blank-padded to 9 chars)</entry>
1963        </row>
1964        <row>
1965         <entry><literal>MON</literal></entry>
1966         <entry>abbreviated upper case month name (3 chars)</entry>
1967        </row>
1968        <row>
1969         <entry><literal>Mon</literal></entry>
1970         <entry>abbreviated mixed case month name (3 chars)</entry>
1971        </row>
1972        <row>
1973         <entry><literal>mon</literal></entry>
1974         <entry>abbreviated lower case month name (3 chars)</entry>
1975        </row>
1976        <row>
1977         <entry><literal>MM</literal></entry>
1978         <entry>month number (01-12)</entry>
1979        </row>
1980        <row>
1981         <entry><literal>DAY</literal></entry>
1982         <entry>full upper case day name (blank-padded to 9 chars)</entry>
1983        </row>
1984        <row>
1985         <entry><literal>Day</literal></entry>
1986         <entry>full mixed case day name (blank-padded to 9 chars)</entry>
1987        </row>
1988        <row>
1989         <entry><literal>day</literal></entry>
1990         <entry>full lower case day name (blank-padded to 9 chars)</entry>
1991        </row>
1992        <row>
1993         <entry><literal>DY</literal></entry>
1994         <entry>abbreviated upper case day name (3 chars)</entry>
1995        </row>
1996        <row>
1997         <entry><literal>Dy</literal></entry>
1998         <entry>abbreviated mixed case day name (3 chars)</entry>
1999        </row>
2000        <row>
2001         <entry><literal>dy</literal></entry>
2002         <entry>abbreviated lower case day name (3 chars)</entry>
2003        </row>
2004        <row>
2005         <entry><literal>DDD</literal></entry>
2006         <entry>day of year (001-366)</entry>
2007        </row>
2008        <row>
2009         <entry><literal>DD</literal></entry>
2010         <entry>day of month (01-31)</entry>
2011        </row>
2012        <row>
2013         <entry><literal>D</literal></entry>
2014         <entry>day of week (1-7; SUN=1)</entry>
2015        </row>
2016        <row>
2017         <entry><literal>W</literal></entry>
2018         <entry>week of month (1-5) where first week start on the first day of the month</entry>
2019        </row> 
2020        <row>
2021         <entry><literal>WW</literal></entry>
2022         <entry>week number of year (1-53) where first week start on the first day of the year</entry>
2023        </row>
2024        <row>
2025         <entry><literal>IW</literal></entry>
2026         <entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
2027        </row>
2028        <row>
2029         <entry><literal>CC</literal></entry>
2030         <entry>century (2 digits)</entry>
2031        </row>
2032        <row>
2033         <entry><literal>J</literal></entry>
2034         <entry>Julian Day (days since January 1, 4712 BC)</entry>
2035        </row>
2036        <row>
2037         <entry><literal>Q</literal></entry>
2038         <entry>quarter</entry>
2039        </row>
2040        <row>
2041         <entry><literal>RM</literal></entry>
2042         <entry>month in Roman Numerals (I-XII; I=January) - upper case</entry>
2043        </row>
2044        <row>
2045         <entry><literal>rm</literal></entry>
2046         <entry>month in Roman Numerals (I-XII; I=January) - lower case</entry>
2047        </row>
2048        <row>
2049         <entry><literal>TZ</literal></entry>
2050         <entry>timezone name - upper case</entry>
2051        </row>
2052        <row>
2053         <entry><literal>tz</literal></entry>
2054         <entry>timezone name - lower case</entry>
2055        </row>
2056       </tbody>
2057      </tgroup>
2058     </table>
2059    </para>
2060
2061    <para>
2062     Certain modifiers may be applied to any template pattern to alter its
2063     behavior.  For example, <quote><literal>FMMonth</literal></quote>
2064     is the <quote><literal>Month</literal></quote> pattern with the
2065     <quote><literal>FM</literal></quote> prefix.
2066    </para>
2067
2068    <para>
2069     <table tocentry="1">
2070      <title>Template pattern modifiers for date/time conversions</title>
2071      <tgroup cols="3">
2072       <thead>
2073        <row>
2074         <entry>Modifier</entry>
2075         <entry>Description</entry>
2076         <entry>Example</entry>
2077        </row>
2078       </thead>
2079       <tbody>
2080        <row>
2081         <entry><literal>FM</literal> prefix</entry>
2082         <entry>fill mode (suppress padding blanks and zeroes)</entry>
2083         <entry><literal>FMMonth</literal></entry>
2084        </row>
2085        <row>
2086         <entry><literal>TH</literal> suffix</entry>
2087         <entry>add upper-case ordinal number suffix</entry>
2088         <entry><literal>DDTH</literal></entry>
2089        </row>   
2090        <row>
2091         <entry><literal>th</literal> suffix</entry>
2092         <entry>add lower-case ordinal number suffix</entry>
2093         <entry><literal>DDth</literal></entry>
2094        </row>
2095        <row>
2096         <entry><literal>FX</literal> prefix</entry>
2097         <entry>Fixed format global option (see below)</entry>
2098         <entry><literal>FX Month DD Day</literal></entry>
2099        </row>   
2100        <row>
2101         <entry><literal>SP</literal> suffix</entry>
2102         <entry>spell mode (not yet implemented)</entry>
2103         <entry><literal>DDSP</literal></entry>
2104        </row>       
2105       </tbody>
2106      </tgroup>
2107     </table>
2108    </para>
2109
2110    <para>
2111     Usage notes:
2112
2113     <itemizedlist>
2114      <listitem>
2115       <para>
2116        <literal>FM</literal> suppresses leading zeroes or trailing blanks
2117        that would otherwise be added to make the output of a pattern be
2118        fixed-width.
2119       </para>
2120      </listitem>
2121
2122      <listitem>
2123       <para>
2124        <function>to_timestamp</function> and <function>to_date</function>
2125        skip multiple blank spaces in the input string if the <literal>FX</literal> option 
2126        is not used. <literal>FX</literal> must be specified as the first item
2127        in the template; for example 
2128        <literal>to_timestamp('2000    JUN','YYYY MON')</literal> is right, but
2129        <literal>to_timestamp('2000    JUN','FXYYYY MON')</literal> returns an error,
2130        because <function>to_timestamp</function> expects one blank space only.
2131       </para>
2132      </listitem>
2133
2134      <listitem>
2135       <para>
2136        If a backslash (<quote><literal>\</literal></quote>) is desired
2137        in a string constant, a double backslash
2138        (<quote><literal>\\</literal></quote>) must be entered; for
2139        example <literal>'\\HH\\MI\\SS'</literal>.  This is true for
2140        any string constant in <productname>PostgreSQL</productname>.
2141       </para>
2142      </listitem>
2143
2144      <listitem>
2145       <para>
2146        Ordinary text is allowed in <function>to_char</function>
2147        templates and will be output literally.  You can put a substring
2148        in double quotes to force it to be interpreted as literal text
2149        even if it contains pattern keywords.  For example, in
2150        <literal>'"Hello Year: "YYYY'</literal>, the <literal>YYYY</literal>
2151        will be replaced by year data, but the single <literal>Y</literal>
2152        will not be.
2153       </para>
2154      </listitem>
2155
2156      <listitem>
2157       <para>
2158        If you want to have a double quote in the output you must
2159        precede it with a backslash, for example <literal>'\\"YYYY
2160        Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
2161       </para>
2162      </listitem>
2163
2164      <listitem>
2165       <para>
2166        <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
2167        <type>date</type> is restricted if you use a year with more than 4 digits. You must
2168        use some non-digit character or template after <literal>YYYY</literal>,
2169        otherwise the year is always interpreted as 4 digits. For example
2170        (with year 20000):
2171        <literal>to_date('200001131', 'YYYYMMDD')</literal> will be 
2172        interpreted as a 4-digit year; better is to use a non-digit 
2173        separator after the year, like
2174        <literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
2175        <literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
2176       </para>
2177      </listitem>
2178
2179      <listitem>
2180       <para>
2181        Millisecond <literal>MS</literal> and microsecond <literal>US</literal>
2182        values are in conversion from string to time stamp used as part of
2183        second after decimal point. For example 
2184        <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
2185        but 300, because the conversion count it as <literal>12 + 0.3</literal>.
2186        It means for format 'SS:MS' is '12:3' or '12:30' or '12:300' same
2187        number of milliseconds. For the three milliseconds must be used
2188        '12:003' that the conversion count as
2189        <literal> 12 + 0.003 = 12.003 seconds </literal>. Here is a more 
2190        complex example: 
2191        <literal>to_timestamp('15:12:02.020.001230','HH:MI:SS.MS.US')</literal>
2192        is 15 hours, 12 minutes, 2.021230 seconds. 
2193       </para>
2194      </listitem>
2195     </itemizedlist>
2196    </para>
2197
2198    <para>
2199     <table tocentry="1">
2200      <title>Template patterns for numeric conversions</title>
2201      <tgroup cols="2">
2202       <thead>
2203        <row>
2204         <entry>Pattern</entry>
2205         <entry>Description</entry>
2206        </row>
2207       </thead>
2208       <tbody>
2209        <row>
2210         <entry><literal>9</literal></entry>
2211         <entry>value with the specified number of digits</entry>
2212        </row>
2213        <row>
2214         <entry><literal>0</literal></entry>
2215         <entry>value with leading zeros</entry>
2216        </row>
2217        <row>
2218         <entry><literal>.</literal> (period)</entry>
2219         <entry>decimal point</entry>
2220        </row>       
2221        <row>
2222         <entry><literal>,</literal> (comma)</entry>
2223         <entry>group (thousand) separator</entry>
2224        </row>
2225        <row>
2226         <entry><literal>PR</literal></entry>
2227         <entry>negative value in angle brackets</entry>
2228        </row>
2229        <row>
2230         <entry><literal>S</literal></entry>
2231         <entry>negative value with minus sign (uses locale)</entry>
2232        </row>
2233        <row>
2234         <entry><literal>L</literal></entry>
2235         <entry>currency symbol (uses locale)</entry>
2236        </row>
2237        <row>
2238         <entry><literal>D</literal></entry>
2239         <entry>decimal point (uses locale)</entry>
2240        </row>
2241        <row>
2242         <entry><literal>G</literal></entry>
2243         <entry>group separator (uses locale)</entry>
2244        </row>
2245        <row>
2246         <entry><literal>MI</literal></entry>
2247         <entry>minus sign in specified position (if number < 0)</entry>
2248        </row>
2249        <row>
2250         <entry><literal>PL</literal></entry>
2251         <entry>plus sign in specified position (if number > 0)</entry>
2252        </row>
2253        <row>
2254         <entry><literal>SG</literal></entry>
2255         <entry>plus/minus sign in specified position</entry>
2256        </row>
2257        <row>
2258         <entry><literal>RN</literal></entry>
2259         <entry>roman numeral (input between 1 and 3999)</entry>
2260        </row>
2261        <row>
2262         <entry><literal>TH</literal> or <literal>th</literal></entry>
2263         <entry>convert to ordinal number</entry>
2264        </row>
2265        <row>
2266         <entry><literal>V</literal></entry>
2267         <entry>shift <replaceable>n</replaceable> digits (see
2268          notes)</entry>
2269        </row>
2270        <row>
2271         <entry><literal>EEEE</literal></entry>
2272         <entry>scientific numbers (not supported yet)</entry>
2273        </row>
2274       </tbody>
2275      </tgroup>
2276     </table>
2277    </para>
2278
2279    <para>
2280     Usage notes:
2281
2282     <itemizedlist>
2283      <listitem>
2284       <para>
2285        A sign formatted using 'SG', 'PL' or 'MI' is not an anchor in
2286        the number; for example,
2287        to_char(-12, 'S9999') produces <literal>'  -12'</literal>,
2288        but to_char(-12, 'MI9999') produces <literal>'-  12'</literal>.
2289        The Oracle implementation does not allow the use of
2290        <literal>MI</literal> ahead of <literal>9</literal>, but rather
2291        requires that <literal>9</literal> precede
2292        <literal>MI</literal>.
2293       </para>
2294      </listitem>
2295
2296      <listitem>
2297       <para>
2298        <literal>9</literal> specifies a value with the same number of 
2299        digits as there are <literal>9</literal>s. If a digit is
2300        not available use blank space.
2301       </para>
2302      </listitem>
2303
2304      <listitem>
2305       <para>
2306        <literal>TH</literal> does not convert values less than zero
2307        and does not convert decimal numbers.
2308       </para>
2309      </listitem>
2310
2311      <listitem>
2312       <para>
2313        <literal>PL</literal>, <literal>SG</literal>, and
2314        <literal>TH</literal> are <productname>PostgreSQL</productname>
2315        extensions. 
2316       </para>
2317      </listitem>
2318
2319      <listitem>
2320       <para>
2321        <literal>V</literal> effectively
2322        multiplies the input values by
2323        <literal>10^<replaceable>n</replaceable></literal>, where
2324        <replaceable>n</replaceable> is the number of digits following
2325        <literal>V</literal>. 
2326        <function>to_char</function> does not support the use of
2327        <literal>V</literal> combined with a decimal point.
2328        (E.g., <literal>99.9V99</literal> is not allowed.)
2329       </para>
2330      </listitem>
2331     </itemizedlist>
2332    </para>   
2333
2334    <para>
2335     <table tocentry="1">
2336      <title><function>to_char</function> Examples</title>
2337      <tgroup cols="2">
2338       <thead>
2339        <row>
2340         <entry>Input</entry>
2341         <entry>Output</entry>
2342        </row>
2343       </thead>
2344       <tbody>
2345        <row>
2346         <entry><literal>to_char(now(),'Day, DD  HH12:MI:SS')</literal></entry>
2347         <entry><literal>'Tuesday  , 06  05:39:18'</literal></entry>
2348        </row>
2349        <row>
2350         <entry><literal>to_char(now(),'FMDay, FMDD  HH12:MI:SS')</literal></entry>
2351         <entry><literal>'Tuesday, 6  05:39:18'</literal></entry>
2352        </row>          
2353        <row>
2354         <entry><literal>to_char(-0.1,'99.99')</literal></entry>
2355         <entry><literal>' -.10'</literal></entry>
2356        </row>
2357        <row>
2358         <entry><literal>to_char(-0.1,'FM9.99')</literal></entry>
2359         <entry><literal>'-.1'</literal></entry>
2360        </row>
2361        <row>
2362         <entry><literal>to_char(0.1,'0.9')</literal></entry>
2363         <entry><literal>' 0.1'</literal></entry>
2364        </row>
2365        <row>
2366         <entry><literal>to_char(12,'9990999.9')</literal></entry>
2367         <entry><literal>'    0012.0'</literal></entry>
2368        </row>
2369        <row>
2370         <entry><literal>to_char(12,'FM9990999.9')</literal></entry>
2371         <entry><literal>'0012'</literal></entry>
2372        </row>
2373        <row>
2374         <entry><literal>to_char(485,'999')</literal></entry>
2375         <entry><literal>' 485'</literal></entry>
2376        </row>
2377        <row>
2378         <entry><literal>to_char(-485,'999')</literal></entry>
2379         <entry><literal>'-485'</literal></entry>
2380        </row>
2381        <row>
2382         <entry><literal>to_char(485,'9 9 9')</literal></entry>
2383         <entry><literal>' 4 8 5'</literal></entry>
2384        </row>
2385        <row>
2386         <entry><literal>to_char(1485,'9,999')</literal></entry>
2387         <entry><literal>' 1,485'</literal></entry>
2388        </row>
2389        <row>
2390         <entry><literal>to_char(1485,'9G999')</literal></entry>
2391         <entry><literal>' 1 485'</literal></entry>
2392        </row>
2393        <row>
2394         <entry><literal>to_char(148.5,'999.999')</literal></entry>
2395         <entry><literal>' 148.500'</literal></entry>
2396        </row>
2397        <row>
2398         <entry><literal>to_char(148.5,'999D999')</literal></entry>
2399         <entry><literal>' 148,500'</literal></entry>     
2400        </row>
2401        <row>
2402         <entry><literal>to_char(3148.5,'9G999D999')</literal></entry>
2403         <entry><literal>' 3 148,500'</literal></entry>
2404        </row>
2405        <row>
2406         <entry><literal>to_char(-485,'999S')</literal></entry>
2407         <entry><literal>'485-'</literal></entry>
2408        </row>
2409        <row>            
2410         <entry><literal>to_char(-485,'999MI')</literal></entry>
2411         <entry><literal>'485-'</literal></entry>        
2412        </row>
2413        <row>
2414         <entry><literal>to_char(485,'999MI')</literal></entry>
2415         <entry><literal>'485'</literal></entry>         
2416        </row>
2417        <row>
2418         <entry><literal>to_char(485,'PL999')</literal></entry>
2419         <entry><literal>'+485'</literal></entry>        
2420        </row>
2421        <row>            
2422         <entry><literal>to_char(485,'SG999')</literal></entry>
2423         <entry><literal>'+485'</literal></entry>        
2424        </row>
2425        <row>
2426         <entry><literal>to_char(-485,'SG999')</literal></entry>
2427         <entry><literal>'-485'</literal></entry>        
2428        </row>
2429        <row>
2430         <entry><literal>to_char(-485,'9SG99')</literal></entry>
2431         <entry><literal>'4-85'</literal></entry>        
2432        </row>
2433        <row>
2434         <entry><literal>to_char(-485,'999PR')</literal></entry>
2435         <entry><literal>'&lt;485&gt;'</literal></entry>         
2436        </row>
2437        <row>
2438         <entry><literal>to_char(485,'L999')</literal></entry>
2439         <entry><literal>'DM 485</literal></entry>        
2440        </row>
2441        <row>
2442         <entry><literal>to_char(485,'RN')</literal></entry>             
2443         <entry><literal>'        CDLXXXV'</literal></entry>
2444        </row>
2445        <row>
2446         <entry><literal>to_char(485,'FMRN')</literal></entry>   
2447         <entry><literal>'CDLXXXV'</literal></entry>
2448        </row>
2449        <row>
2450         <entry><literal>to_char(5.2,'FMRN')</literal></entry>
2451         <entry><literal>V</literal></entry>             
2452        </row>
2453        <row>
2454         <entry><literal>to_char(482,'999th')</literal></entry>
2455         <entry><literal>' 482nd'</literal></entry>                              
2456        </row>
2457        <row>
2458         <entry><literal>to_char(485, '"Good number:"999')</literal></entry>
2459         <entry><literal>'Good number: 485'</literal></entry>
2460        </row>
2461        <row>
2462         <entry><literal>to_char(485.8,'"Pre:"999" Post:" .999')</literal></entry>
2463         <entry><literal>'Pre: 485 Post: .800'</literal></entry>
2464        </row>
2465        <row>
2466         <entry><literal>to_char(12,'99V999')</literal></entry>          
2467         <entry><literal>' 12000'</literal></entry>
2468        </row>
2469        <row>
2470         <entry><literal>to_char(12.4,'99V999')</literal></entry>
2471         <entry><literal>' 12400'</literal></entry>
2472        </row>
2473        <row>            
2474         <entry><literal>to_char(12.45, '99V9')</literal></entry>
2475         <entry><literal>' 125'</literal></entry>
2476        </row>
2477       </tbody>
2478      </tgroup>
2479     </table>
2480    </para>
2481   </sect1>
2482
2483
2484   <sect1 id="functions-datetime">
2485    <title>Date/Time Functions and Operators</title>
2486
2487    <para>
2488     <xref linkend="functions-datetime-table"> shows the available
2489     functions for date/time value processing.  
2490     <xref linkend="operators-datetime-table"> illustrates the
2491     behaviors of the basic arithmetic
2492     operators (<literal>+</literal>, <literal>*</literal>, etc.).
2493     For formatting functions, refer to <xref
2494     linkend="functions-formatting">.  You should be familiar with the
2495     background information on date/time data types (see <xref
2496     linkend="datatype-datetime">).
2497    </para>
2498
2499    <para>
2500     The date/time operators described below behave similarly for types
2501     involving time zones as well as those without.
2502
2503     <table id="operators-datetime-table">
2504      <title>Date/Time Operators</title>
2505
2506      <tgroup cols="3">
2507       <thead>
2508        <row>
2509         <entry>Name</entry>
2510         <entry>Example</entry>
2511         <entry>Result</entry>
2512        </row>
2513       </thead>
2514
2515       <tbody>
2516        <row>
2517         <entry> <literal>+</literal> </entry>
2518         <entry><type>timestamp</type> '2001-09-28 01:00' + <type>interval</type> '23 hours'</entry>
2519         <entry><type>timestamp</type> '2001-09-29 00:00'</entry>
2520        </row>
2521
2522        <row>
2523         <entry> <literal>+</literal> </entry>
2524         <entry><type>date</type> '2001-09-28' + <type>interval</type> '1 hour'</entry>
2525         <entry><type>timestamp</type> '2001-09-28 01:00'</entry>
2526        </row>
2527
2528        <row>
2529         <entry> <literal>+</literal> </entry>
2530         <entry><type>time</type> '01:00' + <type>interval</type> '3 hours'</entry>
2531         <entry><type>time</type> '04:00'</entry>
2532        </row>
2533
2534        <row>
2535         <entry> <literal>-</literal> </entry>
2536         <entry><type>timestamp</type> '2001-09-28 23:00' - <type>interval</type> '23 hours'</entry>
2537         <entry><type>timestamp</type> '2001-09-28'</entry>
2538        </row>
2539
2540        <row>
2541         <entry> <literal>-</literal> </entry>
2542         <entry><type>date</type> '2001-09-28' + <type>interval</type> '1 hour'</entry>
2543         <entry><type>timestamp</type> '2001-09-27 23:00'</entry>
2544        </row>
2545
2546        <row>
2547         <entry> <literal>-</literal> </entry>
2548         <entry><type>time</type> '05:00' + <type>interval</type> '2 hours'</entry>
2549         <entry><type>time</type> '03:00'</entry>
2550        </row>
2551
2552        <row>
2553         <entry> <literal>-</literal> </entry>
2554         <entry><type>interval</type> '2 hours' - <type>time</type> '05:00'</entry>
2555         <entry><type>time</type> '03:00:00'</entry>
2556        </row>
2557
2558        <row>
2559         <entry> <literal>*</literal> </entry>
2560         <entry><type>interval</type> '1 hour' * <type>int</type> '3'</entry>
2561         <entry><type>interval</type> '03:00'</entry>
2562        </row>
2563
2564        <row>
2565         <entry> <literal>/</literal> </entry>
2566         <entry><type>interval</type> '1 hour' / <type>int</type> '3'</entry>
2567         <entry><type>interval</type> '00:20'</entry>
2568        </row>
2569       </tbody>
2570      </tgroup>
2571     </table>
2572    </para>
2573
2574    <para>
2575     The date/time functions are summarized below, with additional
2576     details in subsequent sections.
2577
2578     <table id="functions-datetime-table">
2579      <title>Date/Time Functions</title>
2580      <tgroup cols="5">
2581       <thead>
2582        <row>
2583         <entry>Name</entry>
2584         <entry>Return Type</entry>
2585         <entry>Description</entry>
2586         <entry>Example</entry>
2587         <entry>Result</entry>
2588        </row>
2589       </thead>
2590
2591       <tbody>
2592        <row>
2593         <entry><function>age</function>(<type>timestamp</type>)</entry>
2594         <entry><type>interval</type></entry>
2595         <entry>Subtract from today</entry>
2596         <entry><literal>age(timestamp '1957-06-13')</literal></entry>
2597         <entry><literal>43 years 8 mons 3 days</literal></entry>
2598        </row>
2599
2600        <row>
2601         <entry><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</entry>
2602         <entry><type>interval</type></entry>
2603         <entry>Subtract arguments</entry>
2604         <entry><literal>age('2001-04-10', timestamp '1957-06-13')</literal></entry>
2605         <entry><literal>43 years 9 mons 27 days</literal></entry>
2606        </row>
2607
2608        <row>
2609         <entry><function>current_date</function></entry>
2610         <entry><type>date</type></entry>
2611         <entry>Today's date; see <link
2612          linkend="functions-datetime-current">below</link>
2613         </entry>
2614         <entry></entry>
2615         <entry></entry>
2616        </row>
2617
2618        <row>
2619         <entry><function>current_time</function></entry>
2620         <entry><type>time</type></entry>
2621         <entry>Time of day; see <link
2622          linkend="functions-datetime-current">below</link>
2623         </entry>
2624         <entry></entry>
2625         <entry></entry>
2626        </row>
2627
2628        <row>
2629         <entry><function>current_timestamp</function></entry>
2630         <entry><type>timestamp</type></entry>
2631         <entry>date and time; see also <link
2632          linkend="functions-datetime-current">below</link>
2633         </entry>
2634         <entry></entry>
2635         <entry></entry>
2636        </row>
2637
2638        <row>
2639         <entry><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</entry>
2640         <entry><type>double precision</type></entry>
2641         <entry>Get subfield (equivalent to
2642          <function>extract</function>); see also <link
2643          linkend="functions-datetime-datepart">below</link>
2644         </entry>
2645         <entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
2646         <entry><literal>20</literal></entry>
2647        </row>
2648
2649        <row>
2650         <entry><function>date_part</function>(<type>text</type>, <type>interval</type>)</entry>
2651         <entry><type>double precision</type></entry>
2652         <entry>Get subfield (equivalent to
2653          <function>extract</function>); see also <link
2654          linkend="functions-datetime-datepart">below</link>
2655         </entry>
2656         <entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
2657         <entry><literal>3</literal></entry>
2658        </row>
2659
2660        <row>
2661         <entry><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</entry>
2662         <entry><type>timestamp</type></entry>
2663         <entry>Truncate to specified precision; see also <link
2664          linkend="functions-datetime-trunc">below</link>
2665         </entry>
2666         <entry><literal>date_trunc('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
2667         <entry><literal>2001-02-16 20:00:00+00</literal></entry>
2668        </row>
2669
2670        <row>
2671         <entry><function>extract</function>(<parameter>field</parameter> from <type>timestamp</type>)</entry>
2672         <entry><type>double precision</type></entry>
2673         <entry>Get subfield; see also <link
2674          linkend="functions-datetime-extract">below</link>
2675         </entry>
2676         <entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
2677         <entry><literal>20</literal></entry>
2678        </row>
2679
2680        <row>
2681         <entry><function>extract</function>(<parameter>field</parameter> from <type>interval</type>)</entry>
2682         <entry><type>double precision</type></entry>
2683         <entry>Get subfield; see also <link
2684          linkend="functions-datetime-extract">below</link>
2685         </entry>
2686         <entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
2687         <entry><literal>3</literal></entry>
2688        </row>
2689
2690        <row>
2691         <entry><function>isfinite</function>(<type>timestamp</type>)</entry>
2692         <entry><type>boolean</type></entry>
2693         <entry>Test for finite time stamp (neither invalid nor infinity)</entry>
2694         <entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
2695         <entry><literal>true</literal></entry>
2696        </row>
2697
2698        <row>
2699         <entry><function>isfinite</function>(<type>interval</type>)</entry>
2700         <entry><type>boolean</type></entry>
2701         <entry>Test for finite interval</entry>
2702         <entry><literal>isfinite(interval '4 hours')</literal></entry>
2703         <entry><literal>true</literal></entry>
2704        </row>
2705
2706        <row>
2707         <entry><function>now</function>()</entry>
2708         <entry><type>timestamp</type></entry>
2709         <entry>Current date and time (equivalent to
2710          <function>current_timestamp</function>); see also <link
2711          linkend="functions-datetime-current">below</link>
2712         </entry>
2713         <entry></entry>
2714         <entry></entry>
2715        </row>
2716
2717        <row>
2718         <entry><function>timeofday()</function></entry>
2719         <entry><type>text</type></entry>
2720         <entry>High-precision date and time; see also <link
2721          linkend="functions-datetime-current">below</link>
2722         </entry>
2723         <entry><literal>timeofday()</literal></entry>
2724         <entry><literal>Wed Feb 21 17:01:13.000126 2001 EST</literal></entry>
2725        </row>
2726
2727        <row>
2728         <entry><function>timestamp</function>(<type>date</type>)</entry>
2729         <entry><type>timestamp</type></entry>
2730         <entry><type>date</type> to <type>timestamp</type></entry>
2731         <entry><literal>timestamp(date '2000-12-25')</literal></entry>
2732         <entry><literal>2000-12-25 00:00:00</literal></entry>
2733        </row>
2734
2735        <row>
2736         <entry><function>timestamp</function>(<type>date</type>, <type>time</type>)</entry>
2737         <entry><type>timestamp</type></entry>
2738         <entry><type>date</type> and <type>time</type> to <type>timestamp</type></entry>
2739         <entry><literal>timestamp(date '1998-02-24',time '23:07')</literal></entry>
2740         <entry><literal>1998-02-24 23:07:00</literal></entry>
2741        </row>
2742       </tbody>
2743      </tgroup>
2744     </table>
2745    </para>
2746
2747   <sect2 id="functions-datetime-extract">
2748    <title><function>EXTRACT</function>, <function>date_part</function></title>
2749
2750 <synopsis>
2751 EXTRACT (<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
2752 </synopsis>
2753
2754    <para>
2755     The <function>extract</function> function retrieves sub-fields
2756     from date/time values, such as year or hour.
2757     <replaceable>source</replaceable> is a value expression that
2758     evaluates to type <type>timestamp</type> or <type>interval</type>.
2759     (Expressions of type <type>date</type> or <type>time</type> will
2760     be cast to <type>timestamp</type> and can therefore be used as
2761     well.)  <replaceable>field</replaceable> is an identifier or
2762     string that selects what field to extract from the source value.
2763     The <function>extract</function> function returns values of type
2764     <type>double precision</type>.
2765     The following are valid values:
2766
2767     <!-- alphabetical -->
2768     <variablelist>
2769      <varlistentry>
2770       <term><literal>century</literal></term>
2771       <listitem>
2772        <para>
2773         The year field divided by 100
2774        </para>
2775
2776        <informalexample>
2777 <screen>
2778 SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
2779 <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
2780 </screen>
2781        </informalexample>
2782
2783        <para>
2784         Note that the result for the century field is simply the year field
2785         divided by 100, and not the conventional definition which puts most
2786         years in the 1900's in the twentieth century.
2787        </para>
2788       </listitem>
2789      </varlistentry>
2790
2791      <varlistentry>
2792       <term><literal>day</literal></term>
2793       <listitem>
2794        <para>
2795         The day (of the month) field (1 - 31)
2796        </para>
2797
2798        <informalexample>
2799 <screen>
2800 SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
2801 <lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
2802 </screen>
2803        </informalexample>
2804       </listitem>
2805      </varlistentry>
2806
2807      <varlistentry>
2808       <term><literal>decade</literal></term>
2809       <listitem>
2810        <para>
2811         The year field divided by 10
2812        </para>
2813
2814        <informalexample>
2815 <screen>
2816 SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
2817 <lineannotation>Result: </lineannotation><computeroutput>200</computeroutput>
2818 </screen>
2819        </informalexample>
2820       </listitem>
2821      </varlistentry>
2822
2823      <varlistentry>
2824       <term><literal>dow</literal></term>
2825       <listitem>
2826        <para>
2827         The day of the week (0 - 6; Sunday is 0) (for
2828         <type>timestamp</type> values only)
2829        </para>
2830
2831        <informalexample>
2832 <screen>
2833 SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
2834 <lineannotation>Result: </lineannotation><computeroutput>5</computeroutput>
2835 </screen>
2836        </informalexample>
2837       </listitem>
2838      </varlistentry>
2839
2840      <varlistentry>
2841       <term><literal>doy</literal></term>
2842       <listitem>
2843        <para>
2844         The day of the year (1 - 365/366) (for <type>timestamp</type> values only)
2845        </para>
2846        <informalexample>
2847 <screen>
2848 SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
2849 <lineannotation>Result: </lineannotation><computeroutput>47</computeroutput>
2850 </screen>
2851        </informalexample>
2852       </listitem>
2853      </varlistentry>
2854
2855      <varlistentry>
2856       <term><literal>epoch</literal></term>
2857       <listitem>
2858        <para>
2859         For <type>date</type> and <type>timestamp</type> values, the
2860         number of seconds since 1970-01-01 00:00:00-00 (Result may be
2861         negative.); for <type>interval</type> values, the total number
2862         of seconds in the interval
2863        </para>
2864
2865        <informalexample>
2866 <screen>
2867 SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40');
2868 <lineannotation>Result: </lineannotation><computeroutput>982352320</computeroutput>
2869
2870 SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
2871 <lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
2872 </screen>
2873        </informalexample>
2874       </listitem>
2875      </varlistentry>
2876
2877      <varlistentry>
2878       <term><literal>hour</literal></term>
2879       <listitem>
2880        <para>
2881         The hour field (0 - 23)
2882        </para>
2883
2884        <informalexample>
2885 <screen>
2886 SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
2887 <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
2888 </screen>
2889        </informalexample>
2890       </listitem>
2891      </varlistentry>
2892
2893      <varlistentry>
2894       <term><literal>microseconds</literal></term>
2895       <listitem>
2896        <para>
2897         The seconds field, including fractional parts, multiplied by 1
2898         000 000.  Note that this includes full seconds.
2899        </para>
2900
2901        <informalexample>
2902 <screen>
2903 SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
2904 <lineannotation>Result: </lineannotation><computeroutput>28500000</computeroutput>
2905 </screen>
2906        </informalexample>
2907       </listitem>
2908      </varlistentry>
2909
2910      <varlistentry>
2911       <term><literal>millennium</literal></term>
2912       <listitem>
2913        <para>
2914         The year field divided by 1000
2915        </para>
2916
2917        <informalexample>
2918 <screen>
2919 SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
2920 <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
2921 </screen>
2922        </informalexample>
2923
2924        <para>
2925         Note that the result for the millennium field is simply the year field
2926         divided by 1000, and not the conventional definition which puts
2927         years in the 1900's in the second millennium.
2928        </para>
2929       </listitem>
2930      </varlistentry>
2931
2932      <varlistentry>
2933       <term><literal>milliseconds</literal></term>
2934       <listitem>
2935        <para>
2936         The seconds field, including fractional parts, multiplied by
2937         1000.  Note that this includes full seconds.
2938        </para>
2939
2940        <informalexample>
2941 <screen>
2942 SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
2943 <lineannotation>Result: </lineannotation><computeroutput>28500</computeroutput>
2944 </screen>
2945        </informalexample>
2946       </listitem>
2947      </varlistentry>
2948
2949      <varlistentry>
2950       <term><literal>minute</literal></term>
2951       <listitem>
2952        <para>
2953         The minutes field (0 - 59)
2954        </para>
2955
2956        <informalexample>
2957 <screen>
2958 SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
2959 <lineannotation>Result: </lineannotation><computeroutput>38</computeroutput>
2960 </screen>
2961        </informalexample>
2962       </listitem>
2963      </varlistentry>
2964
2965      <varlistentry>
2966       <term><literal>month</literal></term>
2967       <listitem>
2968        <para>
2969         For <type>timestamp</type> values, the number of the month
2970         within the year (1 - 12) ; for <type>interval</type> values
2971         the number of months, modulo 12 (0 - 11)
2972        </para>
2973
2974        <informalexample>
2975 <screen>
2976 SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
2977 <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
2978
2979 SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
2980 <lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
2981
2982 SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
2983 <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
2984 </screen>
2985        </informalexample>
2986       </listitem>
2987      </varlistentry>
2988
2989      <varlistentry>
2990       <term><literal>quarter</literal></term>
2991       <listitem>
2992        <para>
2993         The quarter of the year (1 - 4) that the day is in (for
2994         <type>timestamp</type> values only)
2995        </para>
2996
2997        <informalexample>
2998 <screen>
2999 SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
3000 <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
3001 </screen>
3002        </informalexample>
3003       </listitem>
3004      </varlistentry>
3005
3006      <varlistentry>
3007       <term><literal>second</literal></term>
3008       <listitem>
3009        <para>
3010         The seconds field, including fractional parts (0 -
3011         59<footnote><simpara>60 if leap seconds are
3012         implemented by the operating system</simpara></footnote>)
3013        </para>
3014
3015        <informalexample>
3016 <screen>
3017 SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
3018 <lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
3019
3020 SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
3021 <lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
3022 </screen>
3023        </informalexample>
3024       </listitem>
3025      </varlistentry>
3026 <!--
3027      <varlistentry>
3028       <term><literal>timezone</literal></term>
3029       <listitem>
3030        <para>
3031         The time zone offset. XXX But in what units?
3032        </para>
3033       </listitem>
3034      </varlistentry>
3035 -->
3036
3037      <varlistentry>
3038       <term><literal>timezone_hour</literal></term>
3039       <listitem>
3040        <para>
3041         The hour component of the time zone offset.
3042        </para>
3043       </listitem>
3044      </varlistentry>
3045
3046      <varlistentry>
3047       <term><literal>timezone_minute</literal></term>
3048       <listitem>
3049        <para>
3050         The minute component of the time zone offset.
3051        </para>
3052       </listitem>
3053      </varlistentry>
3054
3055      <varlistentry>
3056       <term><literal>week</literal></term>
3057       <listitem>
3058        <para>
3059         From a <type>timestamp</type> value, calculate the number of
3060         the week of the year that the day is in.  By definition
3061         (<acronym>ISO</acronym> 8601), the first week of a year
3062         contains January 4 of that year.  (The <acronym>ISO</acronym>
3063         week starts on Monday.)  In other words, the first Thursday of
3064         a year is in week 1 of that year.
3065        </para>
3066
3067        <informalexample>
3068 <screen>
3069 SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
3070 <lineannotation>Result: </lineannotation><computeroutput>7</computeroutput>
3071 </screen>
3072        </informalexample>
3073       </listitem>
3074      </varlistentry>
3075
3076      <varlistentry>
3077       <term><literal>year</literal></term>
3078       <listitem>
3079        <para>
3080         The year field
3081        </para>
3082
3083        <informalexample>
3084 <screen>
3085 SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
3086 <lineannotation>Result: </lineannotation><computeroutput>2001</computeroutput>
3087 </screen>
3088        </informalexample>
3089       </listitem>
3090      </varlistentry>
3091
3092     </variablelist>
3093
3094    </para>
3095
3096    <para>
3097     The <function>extract</function> function is primarily intended
3098     for computational processing.  For formatting date/time values for
3099     display, see <xref linkend="functions-formatting">.
3100    </para>
3101
3102    <anchor id="functions-datetime-datepart">
3103    <para>
3104     The <function>date_part</function> function is modeled on the traditional
3105     <productname>Ingres</productname> equivalent to the
3106     <acronym>SQL</acronym>-function <function>extract</function>:
3107 <synopsis>
3108 date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
3109 </synopsis>
3110     Note that here the <replaceable>field</replaceable> value needs to
3111     be a string.  The valid field values for
3112     <function>date_part</function> are the same as for
3113     <function>extract</function>.
3114    </para>
3115
3116    <informalexample>
3117 <screen>
3118 SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
3119 <lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
3120
3121 SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
3122 <lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
3123 </screen>
3124    </informalexample>
3125
3126   </sect2>
3127
3128   <sect2 id="functions-datetime-trunc">
3129    <title><function>date_trunc</function></title>
3130
3131    <para>
3132     The function <function>date_trunc</function> is conceptually
3133     similar to the <function>trunc</function> function for numbers.
3134    </para>
3135
3136    <para>
3137 <synopsis>
3138 date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
3139 </synopsis>
3140     <replaceable>source</replaceable> is a value expression of type
3141     <type>timestamp</type> (values of type <type>date</type> and
3142     <type>time</type> are cast automatically).
3143     <replaceable>field</replaceable> selects to which precision to
3144     truncate the time stamp value.  The return value is of type
3145     <type>timestamp</type> with all fields that are less than the
3146     selected one set to zero (or one, for day and month).
3147    </para>
3148
3149    <para>
3150     Valid values for <replaceable>field</replaceable> are:
3151     <simplelist>
3152      <member>microseconds</member>
3153      <member>milliseconds</member>
3154      <member>second</member>
3155      <member>minute</member>
3156      <member>hour</member>
3157      <member>day</member>
3158      <member>month</member>
3159      <member>year</member>
3160      <member>decade</member>
3161      <member>century</member>
3162      <member>millennium</member>
3163     </simplelist>
3164    </para>
3165
3166    <informalexample>
3167     <para>
3168 <screen>
3169 SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
3170 <lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00+00</computeroutput>
3171
3172 SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
3173 <lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00+00</computeroutput>
3174 </screen>
3175     </para>
3176    </informalexample>
3177   </sect2>
3178
3179   <sect2 id="functions-datetime-current">
3180    <title>Current Date/Time</title>
3181
3182    <indexterm>
3183     <primary>date</primary>
3184     <secondary>current</secondary>
3185    </indexterm>
3186
3187    <indexterm>
3188     <primary>time</primary>
3189     <secondary>current</secondary>
3190    </indexterm>
3191
3192    <para>
3193     The following functions are available to obtain the current date and/or
3194     time:
3195 <synopsis>
3196 CURRENT_TIME
3197 CURRENT_DATE
3198 CURRENT_TIMESTAMP
3199 </synopsis>
3200     Note that because of the requirements of the
3201     <acronym>SQL</acronym> standard, these functions must not be
3202     called with trailing parentheses.
3203    </para>
3204
3205    <informalexample>
3206 <screen>
3207 SELECT CURRENT_TIME;
3208 <computeroutput>19:07:32</computeroutput>
3209
3210 SELECT CURRENT_DATE;
3211 <computeroutput>2001-02-17</computeroutput>
3212
3213 SELECT CURRENT_TIMESTAMP;
3214 <computeroutput>2001-02-17 19:07:32-05</computeroutput>
3215 </screen>
3216    </informalexample>
3217
3218    <para>
3219     The function <function>now()</function> is the traditional
3220     <productname>PostgreSQL</productname> equivalent to
3221     <function>CURRENT_TIMESTAMP</function>.
3222    </para>
3223
3224    <para>
3225     There is also <function>timeofday()</function>, which returns current
3226     time to higher precision than the <function>CURRENT_TIMESTAMP</function>
3227     family does:
3228    </para>
3229
3230    <informalexample>
3231 <screen>
3232 SELECT timeofday();
3233  Sat Feb 17 19:07:32.000126 2001 EST
3234 </screen>
3235    </informalexample>
3236
3237    <para>
3238     <function>timeofday()</function> uses the operating system call
3239     <function>gettimeofday(2)</function>, which may have resolution as
3240     good as microseconds (depending on your platform); the other functions
3241     rely on <function>time(2)</function> which is restricted to one-second
3242     resolution.  For historical reasons, <function>timeofday()</function>
3243     returns its result as a text string rather than a <type>timestamp</type> value.
3244    </para>
3245
3246    <para>
3247     It is quite important to realize that
3248     <function>CURRENT_TIMESTAMP</function> and related functions all return
3249     the time as of the start of the current transaction; their values do not
3250     increment while a transaction is running.  But
3251     <function>timeofday()</function> returns the actual current time.
3252    </para>
3253
3254    <para>
3255     All the date/time data types also accept the special literal value
3256     <literal>now</> to specify the current date and time.  Thus,
3257     the following three all return the same result:
3258 <programlisting>
3259 SELECT CURRENT_TIMESTAMP;
3260 SELECT now();
3261 SELECT TIMESTAMP 'now';
3262 </programlisting>
3263     <note>
3264      <para>
3265       You do not want to use the third form when specifying a DEFAULT
3266       value while creating a table.  The system will convert <literal>now</>
3267       to a <type>timestamp</type> as soon as the constant is parsed, so that when
3268       the default value is needed,
3269       the time of the table creation would be used!  The first two
3270       forms will not be evaluated until the default value is used,
3271       because they are function calls.  Thus they will give the desired
3272       behavior of defaulting to the time of row insertion.
3273      </para>
3274     </note>
3275    </para>
3276   </sect2>
3277  </sect1>
3278
3279   
3280  <sect1 id="functions-geometry">
3281    <title>Geometric Functions and Operators</title>
3282
3283    <para>
3284     The geometric types <type>point</type>, <type>box</type>,
3285     <type>lseg</type>, <type>line</type>, <type>path</type>,
3286     <type>polygon</type>, and <type>circle</type> have a large set of
3287     native support functions and operators.
3288    </para>
3289
3290    <table>
3291      <TITLE>Geometric Operators</TITLE>
3292      <TGROUP COLS="3">
3293       <THEAD>
3294        <ROW>
3295         <ENTRY>Operator</ENTRY>
3296         <ENTRY>Description</ENTRY>
3297         <ENTRY>Usage</ENTRY>
3298        </ROW>
3299       </THEAD>
3300       <TBODY>
3301        <ROW>
3302         <ENTRY> + </ENTRY>
3303         <ENTRY>Translation</ENTRY>
3304         <ENTRY><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></ENTRY>
3305        </ROW>
3306        <ROW>
3307         <ENTRY> - </ENTRY>
3308         <ENTRY>Translation</ENTRY>
3309         <ENTRY><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></ENTRY>
3310        </ROW>
3311        <ROW>
3312         <ENTRY> * </ENTRY>
3313         <ENTRY>Scaling/rotation</ENTRY>
3314         <ENTRY><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></ENTRY>
3315        </ROW>
3316        <ROW>
3317         <ENTRY> / </ENTRY>
3318         <ENTRY>Scaling/rotation</ENTRY>
3319         <ENTRY><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></ENTRY>
3320        </ROW>
3321        <ROW>
3322         <ENTRY> # </ENTRY>
3323         <ENTRY>Intersection</ENTRY>
3324         <ENTRY><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></ENTRY>
3325        </ROW>
3326        <ROW>
3327         <ENTRY> # </ENTRY>
3328         <ENTRY>Number of points in polygon</ENTRY>
3329         <ENTRY><literal># '((1,0),(0,1),(-1,0))'</literal></ENTRY>
3330        </ROW>
3331        <ROW>
3332         <ENTRY> ## </ENTRY>
3333         <ENTRY>Point of closest proximity</ENTRY>
3334         <ENTRY><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></ENTRY>
3335        </ROW>
3336        <ROW>
3337         <ENTRY> &amp;&amp; </ENTRY>
3338         <ENTRY>Overlaps?</ENTRY>
3339         <ENTRY><literal>box '((0,0),(1,1))' &amp;&amp; box '((0,0),(2,2))'</literal></ENTRY>
3340        </ROW>
3341        <ROW>
3342         <ENTRY> &amp;&lt; </ENTRY>
3343         <ENTRY>Overlaps to left?</ENTRY>
3344         <ENTRY><literal>box '((0,0),(1,1))' &amp;&lt; box '((0,0),(2,2))'</literal></ENTRY>
3345        </ROW>
3346        <ROW>
3347         <ENTRY> &amp;&gt; </ENTRY>
3348         <ENTRY>Overlaps to right?</ENTRY>
3349         <ENTRY><literal>box '((0,0),(3,3))' &amp;&gt; box '((0,0),(2,2))'</literal></ENTRY>
3350        </ROW>
3351        <ROW>
3352         <ENTRY> &lt;-&gt; </ENTRY>
3353         <ENTRY>Distance between</ENTRY>
3354         <ENTRY><literal>circle '((0,0),1)' &lt;-&gt; circle '((5,0),1)'</literal></ENTRY>
3355        </ROW>
3356        <ROW>
3357         <ENTRY> &lt;&lt; </ENTRY>
3358         <ENTRY>Left of?</ENTRY>
3359         <ENTRY><literal>circle '((0,0),1)' &lt;&lt; circle '((5,0),1)'</literal></ENTRY>
3360        </ROW>
3361        <ROW>
3362         <ENTRY> &lt;^ </ENTRY>
3363         <ENTRY>Is below?</ENTRY>
3364         <ENTRY><literal>circle '((0,0),1)' &lt;^ circle '((0,5),1)'</literal></ENTRY>
3365        </ROW>
3366        <ROW>
3367         <ENTRY> &gt;&gt; </ENTRY>
3368         <ENTRY>Is right of?</ENTRY>
3369         <ENTRY><literal>circle '((5,0),1)' &gt;&gt; circle '((0,0),1)'</literal></ENTRY>
3370        </ROW>
3371        <ROW>
3372         <ENTRY> &gt;^ </ENTRY>
3373         <ENTRY>Is above?</ENTRY>
3374         <ENTRY><literal>circle '((0,5),1)' >^ circle '((0,0),1)'</literal></ENTRY>
3375        </ROW>
3376        <ROW>
3377         <ENTRY> ?# </ENTRY>
3378         <ENTRY>Intersects or overlaps</ENTRY>
3379         <ENTRY><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></ENTRY>
3380        </ROW>
3381        <ROW>
3382         <ENTRY> ?- </ENTRY>
3383         <ENTRY>Is horizontal?</ENTRY>
3384         <ENTRY><literal>point '(1,0)' ?- point '(0,0)'</literal></ENTRY>
3385        </ROW>
3386        <ROW>
3387         <ENTRY> ?-| </ENTRY>
3388         <ENTRY>Is perpendicular?</ENTRY>
3389         <ENTRY><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></ENTRY>
3390        </ROW>
3391        <ROW>
3392         <ENTRY> @-@  </ENTRY>
3393         <ENTRY>Length or circumference</ENTRY>
3394         <ENTRY><literal>@-@ path '((0,0),(1,0))'</literal></ENTRY>
3395        </ROW>
3396        <ROW>
3397         <ENTRY> ?| </ENTRY>
3398         <ENTRY>Is vertical?</ENTRY>
3399         <ENTRY><literal>point '(0,1)' ?| point '(0,0)'</literal></ENTRY>
3400        </ROW>
3401        <ROW>
3402         <ENTRY> ?|| </ENTRY>
3403         <ENTRY>Is parallel?</ENTRY>
3404         <ENTRY><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></ENTRY>
3405        </ROW>
3406        <ROW>
3407         <ENTRY> @ </ENTRY>
3408         <ENTRY>Contained or on</ENTRY>
3409         <ENTRY><literal>point '(1,1)' @ circle '((0,0),2)'</literal></ENTRY>
3410        </ROW>
3411        <ROW>
3412         <ENTRY> @@ </ENTRY>
3413         <ENTRY>Center of</ENTRY>
3414         <ENTRY><literal>@@ circle '((0,0),10)'</literal></ENTRY>
3415        </ROW>
3416        <ROW>
3417         <ENTRY> ~= </ENTRY>
3418         <ENTRY>Same as</ENTRY>
3419         <ENTRY><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></ENTRY>
3420        </ROW>
3421       </TBODY>
3422      </TGROUP>
3423    </TABLE>
3424
3425    <table>
3426      <title>Geometric Functions</title>
3427      <tgroup cols="4">
3428       <thead>
3429        <row>
3430         <entry>Function</entry>
3431         <entry>Returns</entry>
3432         <entry>Description</entry>
3433         <entry>Example</entry>
3434        </row>
3435       </thead>
3436       <tbody>
3437        <row>
3438         <entry><function>area</function>(object)</entry>
3439         <entry><type>double precision</type></entry>
3440         <entry>area of item</entry>
3441         <entry><literal>area(box '((0,0),(1,1))')</literal></entry>
3442        </row>
3443        <row>
3444         <entry><function>box</function>(box, box)</entry>
3445         <entry><type>box</type></entry>
3446         <entry>intersection box</entry>
3447         <entry><literal>box(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
3448        </row>
3449        <row>
3450         <entry><function>center</function>(object)</entry>
3451         <entry><type>point</type></entry>
3452         <entry>center of item</entry>
3453         <entry><literal>center(box '((0,0),(1,2))')</literal></entry>
3454        </row>
3455        <row>
3456         <entry><function>diameter</function>(circle)</entry>
3457         <entry><type>double precision</type></entry>
3458         <entry>diameter of circle</entry>
3459         <entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
3460        </row>
3461        <row>
3462         <entry><function>height</function>(box)</entry>
3463         <entry><type>double precision</type></entry>
3464         <entry>vertical size of box</entry>
3465         <entry><literal>height(box '((0,0),(1,1))')</literal></entry>
3466        </row>
3467        <row>
3468         <entry><function>isclosed</function>(path)</entry>
3469         <entry><type>boolean</type></entry>
3470         <entry>a closed path?</entry>
3471         <entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
3472        </row>
3473        <row>
3474         <entry><function>isopen</function>(path)</entry>
3475         <entry><type>boolean</type></entry>
3476         <entry>an open path?</entry>
3477         <entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
3478        </row>
3479        <row>
3480         <entry><function>length</function>(object)</entry>
3481         <entry><type>double precision</type></entry>
3482         <entry>length of item</entry>
3483         <entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
3484        </row>
3485        <row>
3486         <entry><function>pclose</function>(path)</entry>
3487         <entry><type>path</type></entry>
3488         <entry>convert path to closed</entry>
3489         <entry><literal>popen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
3490        </row>
3491 <![IGNORE[
3492 <!-- Not defined by this name. Implements the intersection operator '#' -->
3493        <row>
3494         <entry><function>point</function>(lseg,lseg)</entry>
3495         <entry><type>point</type></entry>
3496         <entry>intersection</entry>
3497         <entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
3498        </row>
3499 ]]>
3500        <row>
3501         <entry><function>npoint</function>(path)</entry>
3502         <entry><type>integer</type></entry>
3503         <entry>number of points</entry>
3504         <entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
3505        </row>
3506        <row>
3507         <entry><function>popen</function>(path)</entry>
3508         <entry><type>path</type></entry>
3509         <entry>convert path to open path</entry>
3510         <entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
3511        </row>
3512        <row>
3513         <entry><function>radius</function>(circle)</entry>
3514         <entry><type>double precision</type></entry>
3515         <entry>radius of circle</entry>
3516         <entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
3517        </row>
3518        <row>
3519         <entry><function>width</function>(box)</entry>
3520         <entry><type>double precision</type></entry>
3521         <entry>horizontal size</entry>
3522         <entry><literal>width(box '((0,0),(1,1))')</literal></entry>
3523        </row>
3524       </tbody>
3525      </tgroup>
3526    </table>
3527
3528
3529    <table>
3530      <title>Geometric Type Conversion Functions</title>
3531      <tgroup cols="4">
3532       <thead>
3533        <row>
3534         <entry>Function</entry>
3535         <entry>Returns</entry>
3536         <entry>Description</entry>
3537         <entry>Example</entry>
3538        </row>
3539       </thead>
3540       <tbody>
3541        <row>
3542         <entry><function>box</function>(<type>circle</type>)</entry>
3543         <entry><type>box</type></entry>
3544         <entry>circle to box</entry>
3545         <entry><literal>box(circle '((0,0),2.0)')</literal></entry>
3546        </row>
3547        <row>
3548         <entry><function>box</function>(<type>point</type>, <type>point</type>)</entry>
3549         <entry><type>box</type></entry>
3550         <entry>points to box</entry>
3551         <entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
3552        </row>
3553        <row>
3554         <entry><function>box</function>(<type>polygon</type>)</entry>
3555         <entry><type>box</type></entry>
3556         <entry>polygon to box</entry>
3557         <entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
3558        </row>
3559        <row>
3560         <entry><function>circle</function>(<type>box</type>)</entry>
3561         <entry><type>circle</type></entry>
3562         <entry>to circle</entry>
3563         <entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
3564        </row>
3565        <row>
3566         <entry><function>circle</function>(<type>point</type>, <type>double precision</type>)</entry>
3567         <entry><type>circle</type></entry>
3568         <entry>point to circle</entry>
3569         <entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
3570        </row>
3571        <row>
3572         <entry><function>lseg</function>(<type>box</type>)</entry>
3573         <entry><type>lseg</type></entry>
3574         <entry>box diagonal to lseg</entry>
3575         <entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
3576        </row>
3577        <row>
3578         <entry><function>lseg</function>(<type>point</type>, <type>point</type>)</entry>
3579         <entry><type>lseg</type></entry>
3580         <entry>points to lseg</entry>
3581         <entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
3582        </row>
3583        <row>
3584         <entry><function>path</function>(<type>polygon</type>)</entry>
3585         <entry><type>point</type></entry>
3586         <entry>polygon to path</entry>
3587         <entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
3588        </row>
3589        <row>
3590         <entry><function>point</function>(<type>circle</type>)</entry>
3591         <entry><type>point</type></entry>
3592         <entry>center</entry>
3593         <entry><literal>point(circle '((0,0),2.0)')</literal></entry>
3594        </row>
3595        <row>
3596         <entry><function>point</function>(<type>lseg</type>, <type>lseg</type>)</entry>
3597         <entry><type>point</type></entry>
3598         <entry>intersection</entry>
3599         <entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
3600        </row>
3601        <row>
3602         <entry><function>point</function>(<type>polygon</type>)</entry>
3603         <entry><type>point</type></entry>
3604         <entry>center</entry>
3605         <entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
3606        </row>
3607        <row>
3608         <entry><function>polygon</function>(<type>box</type>)</entry>
3609         <entry><type>polygon</type></entry>
3610         <entry>12 point polygon</entry>
3611         <entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
3612        </row>
3613        <row>
3614         <entry><function>polygon</function>(<type>circle</type>)</entry>
3615         <entry><type>polygon</type></entry>
3616         <entry>12-point polygon</entry>
3617         <entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
3618        </row>
3619        <row>
3620         <entry><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</entry>
3621         <entry><type>polygon</type></entry>
3622         <entry><replaceable class="parameter">npts</replaceable> polygon</entry>
3623         <entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
3624        </row>
3625        <row>
3626         <entry><function>polygon</function>(<type>path</type>)</entry>
3627         <entry><type>polygon</type></entry>
3628         <entry>path to polygon</entry>
3629         <entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
3630        </row>
3631       </tbody>
3632      </tgroup>
3633    </table>
3634
3635   </sect1>
3636
3637
3638   <sect1 id="functions-net">
3639    <title>Network Address Type Functions</title>
3640
3641
3642     <table tocentry="1" id="cidr-inet-operators-table">
3643      <title><type>cidr</> and <type>inet</> Operators</title>
3644      <TGROUP COLS="3">
3645       <THEAD>
3646        <ROW>
3647         <ENTRY>Operator</ENTRY>
3648         <ENTRY>Description</ENTRY>
3649         <ENTRY>Usage</ENTRY>
3650        </ROW>
3651       </THEAD>
3652       <TBODY>
3653        <ROW>
3654         <ENTRY> &lt; </ENTRY>
3655         <ENTRY>Less than</ENTRY>
3656         <ENTRY><literal>inet '192.168.1.5' &lt; inet '192.168.1.6'</literal></ENTRY>
3657        </ROW>
3658        <ROW>
3659         <ENTRY> &lt;= </ENTRY>
3660         <ENTRY>Less than or equal</ENTRY>
3661         <ENTRY><literal>inet '192.168.1.5' &lt;= inet '192.168.1.5'</literal></ENTRY>
3662        </ROW>
3663        <ROW>
3664         <ENTRY> = </ENTRY>
3665         <ENTRY>Equals</ENTRY>
3666         <ENTRY><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></ENTRY>
3667        </ROW>
3668        <ROW>
3669         <ENTRY> &gt;= </ENTRY>
3670         <ENTRY>Greater or equal</ENTRY>
3671         <ENTRY><literal>inet '192.168.1.5' &gt;= inet '192.168.1.5'</literal></ENTRY>
3672        </ROW>
3673        <ROW>
3674         <ENTRY> &gt; </ENTRY>
3675         <ENTRY>Greater</ENTRY>
3676         <ENTRY><literal>inet '192.168.1.5' &gt; inet '192.168.1.4'</literal></ENTRY>
3677        </ROW>
3678        <ROW>
3679         <ENTRY> &lt;&gt; </ENTRY>
3680         <ENTRY>Not equal</ENTRY>
3681         <ENTRY><literal>inet '192.168.1.5' &lt;&gt; inet '192.168.1.4'</literal></ENTRY>
3682        </ROW>
3683        <ROW>
3684         <ENTRY> &lt;&lt; </ENTRY>
3685         <ENTRY>is contained within</ENTRY>
3686         <ENTRY><literal>inet '192.168.1.5' &lt;&lt; inet '192.168.1/24'</literal></ENTRY>
3687        </ROW>
3688        <ROW>
3689         <ENTRY> &lt;&lt;= </ENTRY>
3690         <ENTRY>is contained within or equals</ENTRY>
3691         <ENTRY><literal>inet '192.168.1/24' &lt;&lt;= inet '192.168.1/24'</literal></ENTRY>
3692        </ROW>
3693        <ROW>
3694         <ENTRY> &gt;&gt; </ENTRY>
3695         <ENTRY>contains</ENTRY>
3696         <ENTRY><literal>inet'192.168.1/24' &gt;&gt; inet '192.168.1.5'</literal></ENTRY>
3697        </ROW>
3698        <ROW>
3699         <ENTRY> &gt;&gt;= </ENTRY>
3700         <ENTRY>contains or equals</ENTRY>
3701         <ENTRY><literal>inet '192.168.1/24' &gt;&gt;= inet '192.168.1/24'</literal></ENTRY>
3702        </ROW>
3703       </TBODY>
3704      </TGROUP>
3705     </TABLE>
3706
3707     <para>
3708      All of the operators for <type>inet</type> can be applied to
3709      <type>cidr</type> values as well.  The operators
3710      <literal>&lt;&lt;</>, <literal>&lt;&lt;=</>,
3711      <literal>&gt;&gt;</>, <literal>&gt;&gt;=</>
3712      test for subnet inclusion: they consider only the network parts
3713      of the two addresses, ignoring any host part, and determine whether
3714      one network part is identical to or a subnet of the other.
3715     </para>
3716
3717
3718     <table tocentry="1" id="cidr-inet-functions">
3719      <title><type>cidr</> and <type>inet</> Functions</title>
3720      <tgroup cols="5">
3721       <thead>
3722        <row>
3723         <entry>Function</entry>
3724         <entry>Returns</entry>
3725         <entry>Description</entry>
3726         <entry>Example</entry>
3727         <entry>Result</entry>
3728        </row>
3729       </thead>
3730       <tbody>
3731        <row>
3732         <entry><function>broadcast</function>(<type>inet</type>)</entry>
3733         <entry><type>inet</type></entry>
3734         <entry>broadcast address for network</entry>
3735         <entry><literal>broadcast('192.168.1.5/24')</literal></entry>
3736         <entry><literal>192.168.1.255/24</literal></entry>
3737        </row>
3738        <row>
3739         <entry><function>host</function>(<type>inet</type>)</entry>
3740         <entry><type>text</type></entry>
3741         <entry>extract IP address as text</entry>
3742         <entry><literal>host('192.168.1.5/24')</literal></entry>
3743         <entry><literal>192.168.1.5</literal></entry>
3744        </row>
3745        <row>
3746         <entry><function>masklen</function>(<type>inet</type>)</entry>
3747         <entry><type>integer</type></entry>
3748         <entry>extract netmask length</entry>
3749         <entry><literal>masklen('192.168.1.5/24')</literal></entry>
3750         <entry><literal>24</literal></entry>
3751        </row>
3752        <row>
3753         <entry><function>set_masklen</function>(<type>inet</type>,<type>integer</type>)</entry>
3754         <entry><type>inet</type></entry>
3755         <entry>set netmask length for inet value</entry>
3756         <entry><literal>set_masklen('192.168.1.5/24',16)</literal></entry>
3757         <entry><literal>192.168.1.5/16</literal></entry>
3758        </row>
3759        <row>
3760         <entry><function>netmask</function>(<type>inet</type>)</entry>
3761         <entry><type>inet</type></entry>
3762         <entry>construct netmask for network</entry>
3763         <entry><literal>netmask('192.168.1.5/24')</literal></entry>
3764         <entry><literal>255.255.255.0</literal></entry>
3765        </row>
3766        <row>
3767         <entry><function>network</function>(<type>inet</type>)</entry>
3768         <entry><type>cidr</type></entry>
3769         <entry>extract network part of address</entry>
3770         <entry><literal>network('192.168.1.5/24')</literal></entry>
3771         <entry><literal>192.168.1.0/24</literal></entry>
3772        </row>
3773        <row>
3774         <entry><function>text</function>(<type>inet</type>)</entry>
3775         <entry><type>text</type></entry>
3776         <entry>extract IP address and masklen as text</entry>
3777         <entry><literal>text(inet '192.168.1.5')</literal></entry>
3778         <entry><literal>192.168.1.5/32</literal></entry>
3779        </row>
3780        <row>
3781         <entry><function>abbrev</function>(<type>inet</type>)</entry>
3782         <entry><type>text</type></entry>
3783         <entry>extract abbreviated display as text</entry>
3784         <entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
3785         <entry><literal>10.1/16</literal></entry>
3786        </row>
3787       </tbody>
3788      </tgroup>
3789     </table>
3790
3791    <para>
3792     All of the functions for <type>inet</type> can be applied to
3793     <type>cidr</type> values as well.  The <function>host</>(),
3794     <function>text</>(), and <function>abbrev</>() functions are primarily
3795     intended to offer alternative display formats. You can cast a text
3796     field to inet using normal casting syntax: <literal>inet(expression)</literal> or 
3797     <literal>colname::inet</literal>.
3798    </para>
3799
3800    <para>
3801     <table tocentry="1" id="macaddr-functions">
3802      <title><type>macaddr</> Functions</title>
3803      <tgroup cols="5">
3804       <thead>
3805        <row>
3806         <entry>Function</entry>
3807         <entry>Returns</entry>
3808         <entry>Description</entry>
3809         <entry>Example</entry>
3810         <entry>Result</entry>
3811        </row>
3812       </thead>
3813       <tbody>
3814        <row>
3815         <entry><function>trunc</function>(<type>macaddr</type>)</entry>
3816         <entry><type>macaddr</type></entry>
3817         <entry>set last 3 bytes to zero</entry>
3818         <entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
3819         <entry><literal>12:34:56:00:00:00</literal></entry>
3820        </row>
3821       </tbody>
3822      </tgroup>
3823     </table>
3824    </para>
3825
3826    <para>
3827     The function <function>trunc</>(<type>macaddr</>) returns a MAC
3828     address with the last 3 bytes set to 0.  This can be used to
3829     associate the remaining prefix with a manufacturer.  The directory
3830     <filename>contrib/mac</> in the source distribution contains some
3831     utilities to create and maintain such an association table.
3832    </para>
3833
3834    <para>
3835     The <type>macaddr</> type also supports the standard relational
3836     operators (<literal>&gt;</>, <literal>&lt;=</>, etc.) for
3837     lexicographical ordering.
3838    </para>
3839
3840   </sect1>
3841
3842
3843  <sect1 id="functions-sequence">
3844   <title>Sequence-Manipulation Functions</title>
3845
3846   <indexterm>
3847    <primary>sequences</primary>
3848   </indexterm>
3849   <indexterm>
3850    <primary>nextval</primary>
3851   </indexterm>
3852   <indexterm>
3853    <primary>currval</primary>
3854   </indexterm>
3855   <indexterm>
3856    <primary>setval</primary>
3857   </indexterm>
3858
3859    <table>
3860     <title>Sequence Functions</>
3861     <tgroup cols="3">
3862      <thead>
3863       <row><entry>Function</> <entry>Returns</> <entry>Description</></row>
3864      </thead>
3865
3866      <tbody>
3867       <row>
3868         <entry><function>nextval</function>(<type>text</type>)</entry>
3869         <entry><type>bigint</type></entry>
3870         <entry>Advance sequence and return new value</>
3871       </row>
3872       <row>
3873         <entry><function>currval</function>(<type>text</type>)</entry>
3874         <entry><type>bigint</type></entry>
3875         <entry>Return value most recently obtained with <function>nextval</></entry>
3876       </row>
3877       <row>
3878         <entry><function>setval</function>(<type>text</type>,<type>bigint</type>)</entry>
3879         <entry><type>bigint</type></entry>
3880         <entry>Set sequence's current value</>
3881       </row>
3882       <row>
3883         <entry><function>setval</function>(<type>text</type>,<type>bigint</type>,<type>boolean</>)</entry>
3884         <entry><type>bigint</type></entry>
3885         <entry>Set sequence's current value and <literal>is_called</> flag</entry>
3886       </row>
3887      </tbody>
3888     </tgroup>
3889    </table>
3890
3891   <para>
3892    This section describes <productname>PostgreSQL</productname>'s functions
3893    for operating on <firstterm>sequence objects</>.
3894    Sequence objects (also called sequence generators or
3895    just sequences) are special single-row tables created with
3896    <command>CREATE SEQUENCE</>.  A sequence object is usually used to
3897    generate unique identifiers for rows of a table.  The sequence functions
3898    provide simple, multi-user-safe methods for obtaining successive
3899    sequence values from sequence objects.
3900   </para>
3901
3902   <para>
3903    For largely historical reasons, the sequence to be operated on by
3904    a sequence-function call is specified by a text-string argument.
3905    To achieve some compatibility with the handling of ordinary SQL
3906    names, the sequence functions convert their argument to lower case
3907    unless the string is double-quoted.  Thus
3908 <programlisting>
3909 nextval('foo')      <lineannotation>operates on sequence </><literal>foo</>
3910 nextval('FOO')      <lineannotation>operates on sequence </><literal>foo</>
3911 nextval('"Foo"')    <lineannotation>operates on sequence </><literal>Foo</>
3912 </programlisting>
3913    Of course, the text argument can be the result of an expression,
3914    not only a simple literal, which is occasionally useful.
3915   </para>
3916
3917   <para>
3918    The available sequence functions are:
3919
3920     <variablelist>
3921      <varlistentry>
3922       <term><function>nextval</></term>
3923       <listitem>
3924        <para>
3925         Advance the sequence object to its next value and return that
3926         value.  This is done atomically: even if multiple server processes
3927         execute <function>nextval</> concurrently, each will safely receive
3928         a distinct sequence value.
3929        </para>
3930       </listitem>
3931      </varlistentry>
3932
3933      <varlistentry>
3934       <term><function>currval</></term>
3935       <listitem>
3936        <para>
3937         Return the value most recently obtained by <function>nextval</>
3938         for this sequence in the current server process.  (An error is
3939         reported if <function>nextval</> has never been called for this
3940         sequence in this process.)  Notice that because this is returning
3941         a process-local value, it gives a predictable answer even if other
3942         server processes are executing <function>nextval</> meanwhile.
3943        </para>
3944       </listitem>
3945      </varlistentry>
3946
3947      <varlistentry>
3948       <term><function>setval</></term>
3949       <listitem>
3950        <para>
3951         Reset the sequence object's counter value.  The two-parameter
3952         form sets the sequence's <literal>last_value</> field to the specified
3953         value and sets its <literal>is_called</> field to <literal>true</>,
3954         meaning that the next <function>nextval</> will advance the sequence
3955         before returning a value.  In the three-parameter form,
3956         <literal>is_called</> may be set either <literal>true</> or
3957         <literal>false</>.  If it's set to <literal>false</>,
3958         the next <function>nextval</> will return exactly the specified
3959         value, and sequence advancement commences with the following
3960         <function>nextval</>.  For example,
3961        </para>
3962
3963        <informalexample>
3964 <screen>
3965 SELECT setval('foo', 42);           <lineannotation>Next nextval() will return 43</>
3966 SELECT setval('foo', 42, true);     <lineannotation>Same as above</>
3967 SELECT setval('foo', 42, false);    <lineannotation>Next nextval() will return 42</>
3968 </screen>
3969        </informalexample>
3970
3971        <para>
3972         The result returned by <function>setval</> is just the value of its
3973         second argument.
3974        </para>
3975       </listitem>
3976      </varlistentry>
3977     </variablelist>
3978   </para>
3979
3980   <important>
3981    <para>
3982     To avoid blocking of concurrent transactions that obtain numbers from the
3983     same sequence, a <function>nextval</> operation is never rolled back;
3984     that is, once a value has been fetched it is considered used, even if the
3985     transaction that did the <function>nextval</> later aborts.  This means
3986     that aborted transactions may leave unused <quote>holes</quote> in the
3987     sequence of assigned values.  <function>setval</> operations are never
3988     rolled back, either.
3989    </para>
3990   </important>
3991
3992   <para>
3993    If a sequence object has been created with default parameters,
3994    <function>nextval()</> calls on it will return successive values
3995    beginning with one.  Other behaviors can be obtained by using
3996    special parameters in the <command>CREATE SEQUENCE</> command;
3997    see its command reference page for more information.
3998   </para>
3999
4000  </sect1>
4001
4002
4003  <sect1 id="functions-conditional">
4004   <title>Conditional Expressions</title>
4005
4006   <indexterm>
4007    <primary>case</primary>
4008   </indexterm>
4009
4010   <indexterm>
4011    <primary>conditionals</primary>
4012   </indexterm>
4013
4014   <para>
4015    This section describes the <acronym>SQL</acronym>-compliant conditional expressions
4016    available in <productname>PostgreSQL</productname>.
4017   </para>
4018
4019   <tip>
4020    <para>
4021     If your needs go beyond the capabilities of these conditional
4022     expressions you might want to consider writing a stored procedure
4023     in a more expressive programming language.
4024    </para>
4025   </tip>
4026
4027   <bridgehead renderas="sect2">CASE</bridgehead>
4028
4029 <synopsis>
4030 CASE WHEN <replaceable>condition</replaceable> THEN <replaceable>result</replaceable>
4031      <optional>WHEN ...</optional>
4032      <optional>ELSE <replaceable>result</replaceable></optional>
4033 END
4034 </synopsis>
4035
4036   <para>
4037    The <acronym>SQL</acronym> <token>CASE</token> expression is a
4038    generic conditional expression, similar to if/else statements in
4039    other languages.  <token>CASE</token> clauses can be used wherever
4040    an expression is valid.  <replaceable>condition</replaceable> is an
4041    expression that returns a <type>boolean</type> result.  If the result is true
4042    then the value of the <token>CASE</token> expression is
4043    <replaceable>result</replaceable>.  If the result is false any
4044    subsequent <token>WHEN</token> clauses are searched in the same
4045    manner.  If no <token>WHEN</token>
4046    <replaceable>condition</replaceable> is true then the value of the
4047    case expression is the <replaceable>result</replaceable> in the
4048    <token>ELSE</token> clause.  If the <token>ELSE</token> clause is
4049    omitted and no condition matches, the result is NULL.
4050   </para>
4051
4052   <informalexample>
4053    <para>
4054     An example:
4055 <screen>
4056 <prompt>=&gt;</prompt> <userinput>SELECT * FROM test;</userinput>
4057 <computeroutput>
4058  a
4059 ---
4060  1
4061  2
4062  3
4063 </computeroutput>
4064
4065 <prompt>=&gt;</prompt> <userinput>SELECT a,
4066           CASE WHEN a=1 THEN 'one'
4067                WHEN a=2 THEN 'two'
4068                ELSE 'other'
4069           END
4070     FROM test;</userinput>
4071 <computeroutput>
4072  a | case
4073 ---+-------
4074  1 | one
4075  2 | two
4076  3 | other
4077 </computeroutput>
4078 </screen>
4079    </para>
4080   </informalexample>
4081
4082   <para>
4083    The data types of all the <replaceable>result</replaceable>
4084    expressions must be coercible to a single output type.
4085    See <xref linkend="typeconv-union-case"> for more detail.
4086   </para>
4087
4088 <synopsis>
4089 CASE <replaceable>expression</replaceable>
4090     WHEN <replaceable>value</replaceable> THEN <replaceable>result</replaceable>
4091     <optional>WHEN ...</optional>
4092     <optional>ELSE <replaceable>result</replaceable></optional>
4093 END
4094 </synopsis>
4095
4096   <para>
4097    This <quote>simple</quote> <token>CASE</token> expression is a
4098    specialized variant of the general form above.  The
4099    <replaceable>expression</replaceable> is computed and compared to
4100    all the <replaceable>value</replaceable>s in the
4101    <token>WHEN</token> clauses until one is found that is equal.  If
4102    no match is found, the <replaceable>result</replaceable> in the
4103    <token>ELSE</token> clause (or NULL) is returned.  This is similar
4104    to the <function>switch</function> statement in C.
4105   </para>
4106
4107   <informalexample>
4108    <para>
4109     The example above can be written using the simple
4110     <token>CASE</token> syntax:
4111 <screen>
4112 <prompt>=&gt;</prompt> <userinput>SELECT a,
4113           CASE a WHEN 1 THEN 'one'
4114                  WHEN 2 THEN 'two'
4115                  ELSE 'other'
4116           END
4117     FROM test;</userinput>
4118 <computeroutput>
4119  a | case
4120 ---+-------
4121  1 | one
4122  2 | two
4123  3 | other
4124 </computeroutput>
4125 </screen>
4126     </para>
4127    </informalexample>
4128
4129    <bridgehead renderas="sect2">COALESCE</bridgehead>
4130
4131 <synopsis>
4132 <function>COALESCE</function>(<replaceable>value</replaceable><optional>, ...</optional>)
4133 </synopsis>
4134
4135   <para>
4136    The <function>COALESCE</function> function returns the first of its
4137    arguments that is not NULL.  This is often useful to substitute a
4138    default value for NULL values when data is retrieved for display,
4139    for example:
4140 <programlisting>
4141 SELECT COALESCE(description, short_description, '(none)') ...
4142 </programlisting>
4143   </para>
4144
4145  <bridgehead renderas="sect2">NULLIF</bridgehead>
4146
4147   <indexterm>
4148    <primary>nullif</primary>
4149   </indexterm>
4150
4151 <synopsis>
4152 <function>NULLIF</function>(<replaceable>value1</replaceable>, <replaceable>value2</replaceable>)
4153 </synopsis>
4154
4155   <para>
4156    The <function>NULLIF</function> function returns NULL if and only
4157    if <replaceable>value1</replaceable> and
4158    <replaceable>value2</replaceable> are equal.  Otherwise it returns
4159    <replaceable>value1</replaceable>.  This can be used to perform the
4160    inverse operation of the <function>COALESCE</function> example
4161    given above:
4162 <programlisting>
4163 SELECT NULLIF(value, '(none)') ...
4164 </programlisting>
4165   </para>
4166
4167   <tip>
4168    <para>
4169     <function>COALESCE</function> and <function>NULLIF</function> are
4170     just shorthand for <token>CASE</token> expressions.  They are actually
4171     converted into <token>CASE</token> expressions at a very early stage
4172     of processing, and subsequent processing thinks it is dealing with
4173     <token>CASE</token>.  Thus an incorrect <function>COALESCE</function> or
4174     <function>NULLIF</function> usage may draw an error message that
4175     refers to <token>CASE</token>.
4176    </para>
4177   </tip>
4178
4179  </sect1>
4180
4181
4182   <sect1 id="functions-misc">
4183    <title>Miscellaneous Functions</>
4184
4185    <table>
4186     <title>Session Information Functions</>
4187     <tgroup cols="3">
4188      <thead>
4189       <row><entry>Name</> <entry>Return Type</> <entry>Description</></row>
4190      </thead>
4191
4192      <tbody>
4193       <row>
4194        <entry><function>current_user</></entry>
4195        <entry><type>name</></entry>
4196        <entry>user name of current execution context</>
4197       </row>
4198       <row>
4199        <entry><function>session_user</></entry>
4200        <entry><type>name</></entry>
4201        <entry>session user name</>
4202       </row>
4203       <row>
4204        <entry><function>user</></entry>
4205        <entry><type>name</></entry>
4206        <entry>equivalent to <function>current_user</></>
4207       </row>
4208      </tbody>
4209     </tgroup>
4210    </table>
4211
4212    <indexterm zone="functions-misc">
4213     <primary>user</primary>
4214     <secondary>current</secondary>
4215    </indexterm>
4216
4217    <para>
4218     The <function>session_user</> is the user that initiated a database
4219     connection; it is fixed for the duration of that connection. The
4220     <function>current_user</> is the user identifier that is applicable
4221     for permission checking. Currently it is always equal to the session
4222     user, but in the future there might be <quote>setuid</> functions and
4223     other facilities to allow the current user to change temporarily.
4224     In Unix parlance, the session user is the <quote>real user</>
4225     and the current user is the <quote>effective user</>.
4226    </para>
4227
4228    <para>
4229     Note that these functions have special syntactic status in <acronym>SQL</>:
4230     they must be called without trailing parentheses.
4231    </para>
4232
4233    <note>
4234     <title>Deprecated</>
4235     <para>
4236      The function <function>getpgusername()</> is an obsolete equivalent
4237      of <function>current_user</>.
4238     </para>
4239    </note>
4240
4241    <table>
4242     <title>System Information Functions</>
4243     <tgroup cols="3">
4244      <thead>
4245       <row><entry>Name</> <entry>Return Type</> <entry>Description</></row>
4246      </thead>
4247
4248      <tbody>
4249       <row>
4250        <entry><function>version</></entry>
4251        <entry><type>text</></entry>
4252        <entry>PostgreSQL version information</>
4253       </row>
4254      </tbody>
4255     </tgroup>
4256    </table>
4257
4258    <indexterm zone="functions-misc">
4259     <primary>version</primary>
4260    </indexterm>
4261
4262    <para>
4263     <function>version()</> returns a string describing the PostgreSQL
4264     server's version.
4265    </para>
4266
4267    <table>
4268     <title>Access Privilege Inquiry Functions</>
4269     <tgroup cols="3">
4270      <thead>
4271       <row><entry>Name</> <entry>Return Type</> <entry>Description</></row>
4272      </thead>
4273
4274      <tbody>
4275       <row>
4276        <entry><function>has_table_privilege</function>(<parameter>user</parameter>,
4277                                   <parameter>table</parameter>,
4278                                   <parameter>access</parameter>)
4279        </entry>
4280        <entry><type>boolean</type></>
4281        <entry>does user have access to table</>
4282       </row>
4283       <row>
4284        <entry><function>has_table_privilege</function>(<parameter>table</parameter>,
4285                                   <parameter>access</parameter>)
4286        </entry>
4287        <entry><type>boolean</type></>
4288        <entry>does current user have access to table</>
4289       </row>
4290      </tbody>
4291     </tgroup>
4292    </table>
4293
4294    <indexterm zone="functions-misc">
4295     <primary>has_table_privilege</primary>
4296    </indexterm>
4297
4298    <para>
4299     <function>has_table_privilege</> determines whether a user
4300     can access a table in a particular way.  The user can be
4301     specified by name or by ID
4302     (<classname>pg_user</>.<structfield>usesysid</>), or if the argument is
4303     omitted
4304     <function>current_user</> is assumed.  The table can be specified
4305     by name or by OID.  (Thus, there are actually six variants of
4306     <function>has_table_privilege</>, which can be distinguished by
4307     the number and types of their arguments.)  The desired access type
4308     is specified by a text string, which must evaluate to one of the
4309     values <literal>SELECT</>, <literal>INSERT</>, <literal>UPDATE</>,
4310     <literal>DELETE</>, <literal>RULE</>, <literal>REFERENCES</>, or
4311     <literal>TRIGGER</>.  (Case of the string is not significant, however.)
4312    </para>
4313
4314    <table>
4315     <title>Catalog Information Functions</>
4316     <tgroup cols="3">
4317      <thead>
4318       <row><entry>Name</> <entry>Return Type</> <entry>Description</></row>
4319      </thead>
4320
4321      <tbody>
4322       <row>
4323        <entry><function>pg_get_viewdef</>(<parameter>viewname</parameter>)</entry>
4324        <entry><type>text</></entry>
4325        <entry>Get CREATE VIEW command for view</>
4326       </row>
4327       <row>
4328        <entry><function>pg_get_ruledef</>(<parameter>rulename</parameter>)</entry>
4329        <entry><type>text</></entry>
4330        <entry>Get CREATE RULE command for rule</>
4331       </row>
4332       <row>
4333        <entry><function>pg_get_indexdef</>(<parameter>indexOID</parameter>)</entry>
4334        <entry><type>text</></entry>
4335        <entry>Get CREATE INDEX command for index</>
4336       </row>
4337       <row>
4338        <entry><function>pg_get_userbyid</>(<parameter>userid</parameter>)</entry>
4339        <entry><type>name</></entry>
4340        <entry>Get user name given sysid</>
4341       </row>
4342      </tbody>
4343     </tgroup>
4344    </table>
4345
4346    <indexterm zone="functions-misc">
4347     <primary>pg_get_viewdef</primary>
4348    </indexterm>
4349
4350    <indexterm zone="functions-misc">
4351     <primary>pg_get_ruledef</primary>
4352    </indexterm>
4353
4354    <indexterm zone="functions-misc">
4355     <primary>pg_get_indexdef</primary>
4356    </indexterm>
4357
4358    <indexterm zone="functions-misc">
4359     <primary>pg_get_userbyid</primary>
4360    </indexterm>
4361
4362    <para>
4363     These functions extract information from the system catalogs.
4364     <function>pg_get_viewdef()</>, <function>pg_get_ruledef()</>, and
4365     <function>pg_get_indexdef()</> respectively reconstruct the creating
4366     command for a view, rule, or index.  (Note that this is a decompiled
4367     reconstruction, not the verbatim text of the command.)
4368     <function>pg_get_userbyid()</> extracts a user's name given a
4369     <structfield>usesysid</> value.
4370    </para>
4371
4372   </sect1>
4373
4374
4375  <sect1 id="functions-aggregate">
4376   <title>Aggregate Functions</title>
4377
4378   <note>
4379    <title>Author</title>
4380    <para>
4381     Written by Isaac Wilcox <email>isaac@azartmedia.com</email> on 2000-06-16
4382    </para>
4383   </note>
4384
4385   <para>
4386    <firstterm>Aggregate functions</firstterm> compute a single result
4387    value from a set of input values.  The special syntax
4388    considerations for aggregate functions are explained in <xref
4389    linkend="syntax-aggregates">.  Consult the <citetitle>PostgreSQL
4390    Tutorial</citetitle> for additional introductory information.
4391   </para>
4392
4393   <table tocentry="1">
4394    <title>Aggregate Functions</title>
4395
4396    <tgroup cols="3">
4397     <thead>
4398      <row>
4399       <entry>Function</entry>
4400       <entry>Description</entry>
4401       <entry>Notes</entry>
4402      </row>
4403     </thead>
4404
4405     <tbody>
4406      <row>
4407       <entry>AVG(<replaceable class="parameter">expression</replaceable>)</entry>
4408       <entry>the average (arithmetic mean) of all input values</entry>
4409       <entry>
4410        <indexterm>
4411         <primary>average</primary>
4412         <secondary>function</secondary>
4413        </indexterm>
4414        Finding the average value is available on the following data
4415        types: <type>smallint</type>, <type>integer</type>,
4416        <type>bigint</type>, <type>real</type>, <type>double
4417        precision</type>, <type>numeric</type>, <type>interval</type>.
4418        The result is of type <type>numeric</type> for any integer type
4419        input, <type>double precision</type> for floating point input,
4420        otherwise the same as the input data type.
4421       </entry>
4422      </row>
4423
4424      <row>
4425       <entry><function>count</function>(*)</entry>
4426       <entry>number of input values</entry>
4427       <entry>The return value is of type <type>bigint</type>.</entry>
4428      </row>
4429
4430      <row>
4431       <entry><function>count</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4432       <entry>
4433        Counts the input values for which the value of <replaceable
4434        class="parameter">expression</replaceable> is not NULL.
4435       </entry>
4436       <entry>The return value is of type <type>bigint</type>.</entry>
4437      </row>
4438
4439      <row>
4440       <entry><function>max</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4441       <entry>the maximum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
4442       <entry>
4443        Available for all numeric, string, and date/time types.  The
4444        result has the same type as the input expression.
4445       </entry>
4446      </row>
4447
4448      <row>
4449       <entry><function>min</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4450       <entry>the minimum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
4451       <entry>
4452        Available for all numeric, string, and date/time types.  The
4453        result has the same type as the input expression.
4454       </entry>
4455      </row>
4456
4457      <row>
4458       <entry><function>stddev</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4459       <entry>the sample standard deviation of the input values</entry>
4460       <entry>
4461        <indexterm>
4462         <primary>standard deviation</primary>
4463        </indexterm>
4464        Finding the standard deviation is available on the following
4465        data types: <type>smallint</type>, <type>integer</type>,
4466        <type>bigint</type>, <type>real</type>, <type>double
4467        precision</type>, <type>numeric</type>.  The result is of type
4468        <type>double precision</type> for floating point input,
4469        otherwise <type>numeric</type>.
4470       </entry>
4471      </row>
4472
4473      <row>
4474       <entry><function>sum</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4475       <entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
4476       <entry>
4477        Summation is available on the following data types:
4478        <type>smallint</type>, <type>integer</type>,
4479        <type>bigint</type>, <type>real</type>, <type>double
4480        precision</type>, <type>numeric</type>, <type>interval</type>.
4481        The result is of type <type>bigint</type> for <type>smallint</type>
4482        or <type>integer</type> input, <type>numeric</type> for
4483        <type>bigint</type> 
4484        input, <type>double precision</type> for floating point input,
4485        otherwise the same as the input data type.
4486       </entry>
4487      </row>
4488
4489      <row>
4490       <entry><function>variance</function>(<replaceable class="parameter">expression</replaceable>)</entry>
4491       <entry>the sample variance of the input values</entry>
4492       <entry>
4493        <indexterm>
4494         <primary>variance</primary>
4495        </indexterm>
4496        The variance is the square of the standard deviation.  The
4497        supported data types and result types are the same as for
4498        standard deviation.
4499       </entry>
4500      </row>
4501
4502     </tbody>
4503    </tgroup>
4504   </table>
4505
4506   <para>
4507    It should be noted that except for <function>COUNT</function>,
4508    these functions return NULL when no rows are selected.  In
4509    particular, <function>SUM</function> of no rows returns NULL, not
4510    zero as one might expect.  <function>COALESCE</function> may be
4511    used to substitute zero for NULL when necessary.
4512   </para>
4513
4514  </sect1>
4515
4516 </chapter>
4517
4518 <!-- Keep this comment at the end of the file
4519 Local variables:
4520 mode:sgml
4521 sgml-omittag:nil
4522 sgml-shorttag:t
4523 sgml-minimize-attributes:nil
4524 sgml-always-quote-attributes:t
4525 sgml-indent-step:1
4526 sgml-indent-data:t
4527 sgml-parent-document:nil
4528 sgml-default-dtd-file:"./reference.ced"
4529 sgml-exposed-tags:nil
4530 sgml-local-catalogs:("/usr/lib/sgml/catalog")
4531 sgml-local-ecat-files:nil
4532 End:
4533 -->