2 $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.120 2002/09/02 05:53:23 momjian Exp $
3 PostgreSQL documentation
6 <chapter id="functions">
7 <title>Functions and Operators</title>
9 <indexterm zone="functions">
10 <primary>functions</primary>
13 <indexterm zone="functions">
14 <primary>operators</primary>
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.
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
32 <acronym>SQL</acronym>
33 standard. Some of this extended functionality is present in other
34 <acronym>RDBMS</acronym> products, and in many cases this
35 functionality is compatible and consistent between various products.
39 <sect1 id="functions-logical">
40 <title>Logical Operators</title>
42 <indexterm zone="functions-logical">
43 <primary>operators</primary>
44 <secondary>logical</secondary>
48 <primary>Boolean</primary>
49 <secondary>operators</secondary>
50 <see>operators, logical</see>
54 The usual logical operators are available:
57 <primary>and</primary>
58 <secondary>operator</secondary>
63 <secondary>operator</secondary>
67 <primary>not</primary>
68 <secondary>operator</secondary>
77 <acronym>SQL</acronym> uses a three-valued Boolean logic where NULL represents
78 <quote>unknown</quote>. Observe the following truth tables:
84 <entry><replaceable>a</replaceable></entry>
85 <entry><replaceable>b</replaceable></entry>
86 <entry><replaceable>a</replaceable> AND <replaceable>b</replaceable></entry>
87 <entry><replaceable>a</replaceable> OR <replaceable>b</replaceable></entry>
141 <entry><replaceable>a</replaceable></entry>
142 <entry>NOT <replaceable>a</replaceable></entry>
167 <sect1 id="functions-comparison">
168 <title>Comparison Operators</title>
170 <indexterm zone="functions-comparison">
171 <primary>comparison</primary>
172 <secondary>operators</secondary>
176 <title>Comparison Operators</title>
180 <entry>Operator</entry>
181 <entry>Description</entry>
187 <entry> <literal><</literal> </entry>
188 <entry>less than</entry>
192 <entry> <literal>></literal> </entry>
193 <entry>greater than</entry>
197 <entry> <literal><=</literal> </entry>
198 <entry>less than or equal to</entry>
202 <entry> <literal>>=</literal> </entry>
203 <entry>greater than or equal to</entry>
207 <entry> <literal>=</literal> </entry>
212 <entry> <literal><></literal> or <literal>!=</literal> </entry>
213 <entry>not equal</entry>
221 The <literal>!=</literal> operator is converted to
222 <literal><></literal> in the parser stage. It is not
223 possible to implement <literal>!=</literal> and
224 <literal><></literal> operators that do different things.
229 Comparison operators are available for all data types where this
230 makes sense. All comparison operators are binary operators that
231 return values of type <type>boolean</type>; expressions like
232 <literal>1 < 2 < 3</literal> are not valid (because there is
233 no <literal><</literal> operator to compare a Boolean value with
234 <literal>3</literal>).
239 <primary>between</primary>
241 In addition to the comparison operators, the special
242 <token>BETWEEN</token> construct is available.
244 <replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
248 <replaceable>a</replaceable> >= <replaceable>x</replaceable> AND <replaceable>a</replaceable> <= <replaceable>y</replaceable>
252 <replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
256 <replaceable>a</replaceable> < <replaceable>x</replaceable> OR <replaceable>a</replaceable> > <replaceable>y</replaceable>
258 There is no difference between the two respective forms apart from
259 the <acronym>CPU</acronym> cycles required to rewrite the first one
260 into the second one internally.
264 To check whether a value is or is not NULL, use the constructs
266 <replaceable>expression</replaceable> IS NULL
267 <replaceable>expression</replaceable> IS NOT NULL
269 or the equivalent, but less standard, constructs
271 <replaceable>expression</replaceable> ISNULL
272 <replaceable>expression</replaceable> NOTNULL
277 Do <emphasis>not</emphasis> write
278 <literal><replaceable>expression</replaceable> = NULL</literal>
279 because NULL is not <quote>equal to</quote> NULL. (NULL represents
280 an unknown value, and it is not known whether two unknown values are
285 Some applications may (incorrectly) require that
286 <literal><replaceable>expression</replaceable> = NULL</literal>
287 returns true if <replaceable>expression</replaceable> evaluates to
288 the NULL value. To support these applications, the run-time option
289 <varname>transform_null_equals</varname> can be turned on (e.g.,
290 <literal>SET transform_null_equals TO ON;</literal>).
291 <productname>PostgreSQL</productname> will then convert
292 <literal>x = NULL</literal> clauses to
293 <literal>x IS NULL</literal>. This was
294 the default behavior in releases 6.5 through 7.1.
298 Boolean values can also be tested using the constructs
300 <replaceable>expression</replaceable> IS TRUE
301 <replaceable>expression</replaceable> IS NOT TRUE
302 <replaceable>expression</replaceable> IS FALSE
303 <replaceable>expression</replaceable> IS NOT FALSE
304 <replaceable>expression</replaceable> IS UNKNOWN
305 <replaceable>expression</replaceable> IS NOT UNKNOWN
307 These are similar to <literal>IS NULL</literal> in that they will
308 always return TRUE or FALSE, never NULL, even when the operand is NULL.
309 A NULL input is treated as the logical value UNKNOWN.
313 <sect1 id="functions-math">
314 <title>Mathematical Functions and Operators</title>
317 Mathematical operators are provided for many
318 <productname>PostgreSQL</productname> types. For types without
319 common mathematical conventions for all possible permutations
320 (e.g. date/time types) we
321 describe the actual behavior in subsequent sections.
325 <title>Mathematical Operators</title>
331 <entry>Description</entry>
332 <entry>Example</entry>
333 <entry>Result</entry>
339 <entry> <literal>+</literal> </entry>
340 <entry>Addition</entry>
346 <entry> <literal>-</literal> </entry>
347 <entry>Subtraction</entry>
353 <entry> <literal>*</literal> </entry>
354 <entry>Multiplication</entry>
360 <entry> <literal>/</literal> </entry>
361 <entry>Division (integer division truncates results)</entry>
367 <entry> <literal>%</literal> </entry>
368 <entry>Modulo (remainder)</entry>
374 <entry> <literal>^</literal> </entry>
375 <entry>Exponentiation</entry>
376 <entry>2.0 ^ 3.0</entry>
381 <entry> <literal>|/</literal> </entry>
382 <entry>Square root</entry>
383 <entry>|/ 25.0</entry>
388 <entry> <literal>||/</literal> </entry>
389 <entry>Cube root</entry>
390 <entry>||/ 27.0</entry>
395 <entry> <literal>!</literal> </entry>
396 <entry>Factorial</entry>
402 <entry> <literal>!!</literal> </entry>
403 <entry>Factorial (prefix operator)</entry>
409 <entry> <literal>@</literal> </entry>
410 <entry>Absolute value</entry>
411 <entry>@ -5.0</entry>
416 <entry> <literal>&</literal> </entry>
417 <entry>Binary AND</entry>
418 <entry>91 & 15</entry>
423 <entry> <literal>|</literal> </entry>
424 <entry>Binary OR</entry>
425 <entry>32 | 3</entry>
430 <entry> <literal>#</literal> </entry>
431 <entry>Binary XOR</entry>
432 <entry>17 # 5</entry>
437 <entry> <literal>~</literal> </entry>
438 <entry>Binary NOT</entry>
444 <entry> << </entry>
445 <entry>Binary shift left</entry>
446 <entry>1 << 4</entry>
451 <entry> >> </entry>
452 <entry>Binary shift right</entry>
453 <entry>8 >> 2</entry>
462 The <quote>binary</quote> operators are also available for the bit
463 string types <type>BIT</type> and <type>BIT VARYING</type>.
466 <title>Bit String Binary Operators</title>
471 <entry>Example</entry>
472 <entry>Result</entry>
478 <entry>B'10001' & B'01101'</entry>
482 <entry>B'10001' | B'01101'</entry>
486 <entry>B'10001' # B'01101'</entry>
490 <entry>~ B'10001'</entry>
494 <entry>B'10001' << 3</entry>
498 <entry>B'10001' >> 2</entry>
505 Bit string arguments to <literal>&</literal>, <literal>|</literal>,
506 and <literal>#</literal> must be of equal length. When bit
507 shifting, the original length of the string is preserved, as shown
512 <title>Mathematical Functions</title>
516 <entry>Function</entry>
517 <entry>Return Type</entry>
518 <entry>Description</entry>
519 <entry>Example</entry>
520 <entry>Result</entry>
526 <entry><function>abs</function>(<replaceable>x</replaceable>)</entry>
527 <entry>(same as x)</entry>
528 <entry>absolute value</entry>
529 <entry><literal>abs(-17.4)</literal></entry>
534 <entry><function>cbrt</function>(<type>dp</type>)</entry>
535 <entry><type>dp</type></entry>
536 <entry>cube root</entry>
537 <entry><literal>cbrt(27.0)</literal></entry>
542 <entry><function>ceil</function>(<type>numeric</type>)</entry>
543 <entry><type>numeric</type></entry>
544 <entry>smallest integer not less than argument</entry>
545 <entry><literal>ceil(-42.8)</literal></entry>
550 <entry><function>degrees</function>(<type>dp</type>)</entry>
551 <entry><type>dp</type></entry>
552 <entry>radians to degrees</entry>
553 <entry><literal>degrees(0.5)</literal></entry>
554 <entry>28.6478897565412</entry>
558 <entry><function>exp</function>(<type>dp</type>)</entry>
559 <entry><type>dp</type></entry>
560 <entry>exponential</entry>
561 <entry><literal>exp(1.0)</literal></entry>
562 <entry>2.71828182845905</entry>
566 <entry><function>floor</function>(<type>numeric</type>)</entry>
567 <entry><type>numeric</type></entry>
568 <entry>largest integer not greater than argument</entry>
569 <entry><literal>floor(-42.8)</literal></entry>
574 <entry><function>ln</function>(<type>dp</type>)</entry>
575 <entry><type>dp</type></entry>
576 <entry>natural logarithm</entry>
577 <entry><literal>ln(2.0)</literal></entry>
578 <entry>0.693147180559945</entry>
582 <entry><function>log</function>(<type>dp</type>)</entry>
583 <entry><type>dp</type></entry>
584 <entry>base 10 logarithm</entry>
585 <entry><literal>log(100.0)</literal></entry>
590 <entry><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
591 <parameter>x</parameter> <type>numeric</type>)</entry>
592 <entry><type>numeric</type></entry>
593 <entry>logarithm to base <parameter>b</parameter></entry>
594 <entry><literal>log(2.0, 64.0)</literal></entry>
595 <entry>6.0000000000</entry>
599 <entry><function>mod</function>(<parameter>y</parameter>,
600 <parameter>x</parameter>)</entry>
601 <entry>(same as argument types)</entry>
602 <entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
603 <entry><literal>mod(9,4)</literal></entry>
608 <entry><function>pi</function>()</entry>
609 <entry><type>dp</type></entry>
610 <entry><quote>Pi</quote> constant</entry>
611 <entry><literal>pi()</literal></entry>
612 <entry>3.14159265358979</entry>
616 <entry><function>pow</function>(<parameter>e</parameter> <type>dp</type>,
617 <parameter>n</parameter> <type>dp</type>)</entry>
618 <entry><type>dp</type></entry>
619 <entry>raise a number to exponent <parameter>e</parameter></entry>
620 <entry><literal>pow(9.0, 3.0)</literal></entry>
625 <entry><function>radians</function>(<type>dp</type>)</entry>
626 <entry><type>dp</type></entry>
627 <entry>degrees to radians</entry>
628 <entry><literal>radians(45.0)</literal></entry>
629 <entry>0.785398163397448</entry>
633 <entry><function>random</function>()</entry>
634 <entry><type>dp</type></entry>
635 <entry>value between 0.0 to 1.0</entry>
636 <entry><literal>random()</literal></entry>
641 <entry><function>round</function>(<type>dp</type>)</entry>
642 <entry><type>dp</type></entry>
643 <entry>round to nearest integer</entry>
644 <entry><literal>round(42.4)</literal></entry>
649 <entry><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</entry>
650 <entry><type>numeric</type></entry>
651 <entry>round to <parameter>s</parameter> decimal places</entry>
652 <entry><literal>round(42.4382, 2)</literal></entry>
657 <entry><function>setseed</function>(<replaceable>new-seed</replaceable>)</entry>
658 <entry>set seed for subsequent random() calls</entry>
659 <entry><literal>setseed(0.54823)</literal></entry>
664 <entry><function>sign</function>(<type>numeric</type>)</entry>
665 <entry><type>numeric</type></entry>
666 <entry>sign of the argument (-1, 0, +1)</entry>
667 <entry><literal>sign(-8.4)</literal></entry>
672 <entry><function>sqrt</function>(<type>dp</type>)</entry>
673 <entry><type>dp</type></entry>
674 <entry>square root</entry>
675 <entry><literal>sqrt(2.0)</literal></entry>
676 <entry>1.4142135623731</entry>
680 <entry><function>trunc</function>(<type>dp</type>)</entry>
681 <entry><type>dp</type></entry>
682 <entry>truncate toward zero</entry>
683 <entry><literal>trunc(42.8)</literal></entry>
688 <entry><function>trunc</function>(<type>numeric</type>,
689 <parameter>r</parameter> <type>integer</type>)</entry>
690 <entry><type>numeric</type></entry>
691 <entry>truncate to <parameter>s</parameter> decimal places</entry>
692 <entry><literal>trunc(42.4382, 2)</literal></entry>
701 In the table above, <literal>dp</literal> indicates <type>double precision</type>.
702 The functions <function>exp</function>, <function>ln</function>,
703 <function>log</function>, <function>pow</function>,
704 <function>round</function> (1 argument), <function>sqrt</function>,
705 and <function>trunc</function> (1 argument) are also available for
706 the type <type>numeric</type> in place of
707 <type>double precision</type>.
708 Functions returning a <type>numeric</type> result take
709 <type>numeric</type> input arguments, unless otherwise specified.
710 Many of these functions are implemented on top
711 of the host system's C library; accuracy and behavior in boundary cases
712 could therefore vary depending on the host system.
716 <title>Trigonometric Functions</title>
721 <entry>Function</entry>
722 <entry>Description</entry>
728 <entry><function>acos</function>(<replaceable>x</replaceable>)</entry>
729 <entry>inverse cosine</entry>
733 <entry><function>asin</function>(<replaceable>x</replaceable>)</entry>
734 <entry>inverse sine</entry>
738 <entry><function>atan</function>(<replaceable>x</replaceable>)</entry>
739 <entry>inverse tangent</entry>
743 <entry><function>atan2</function>(<replaceable>x</replaceable>,
744 <replaceable>y</replaceable>)</entry>
745 <entry>inverse tangent of
746 <replaceable>a</replaceable>/<replaceable>x</replaceable></entry>
750 <entry><function>cos</function>(<replaceable>x</replaceable>)</entry>
751 <entry>cosine</entry>
755 <entry><function>cot</function>(<replaceable>x</replaceable>)</entry>
756 <entry>cotangent</entry>
760 <entry><function>sin</function>(<replaceable>x</replaceable>)</entry>
765 <entry><function>tan</function>(<replaceable>x</replaceable>)</entry>
766 <entry>tangent</entry>
773 All trigonometric functions have arguments and return values of
774 type <type>double precision</type>.
780 <sect1 id="functions-string">
781 <title>String Functions and Operators</title>
784 This section describes functions and operators for examining and
785 manipulating string values. Strings in this context include values
786 of all the types <type>CHARACTER</type>, <type>CHARACTER
787 VARYING</type>, and <type>TEXT</type>. Unless otherwise noted, all
788 of the functions listed below work on all of these types, but be
789 wary of potential effects of the automatic padding when using the
790 <type>CHARACTER</type> type. Generally, the functions described
791 here also work on data of non-string types by converting that data
792 to a string representation first. Some functions also exist
793 natively for bit-string types.
797 <acronym>SQL</acronym> defines some string functions with a special syntax where
798 certain keywords rather than commas are used to separate the
799 arguments. Details are in <xref linkend="functions-string-sql">.
800 These functions are also implemented using the regular syntax for
801 function invocation. (See <xref linkend="functions-string-other">.)
804 <table id="functions-string-sql">
805 <title><acronym>SQL</acronym> String Functions and Operators</title>
809 <entry>Function</entry>
810 <entry>Return Type</entry>
811 <entry>Description</entry>
812 <entry>Example</entry>
813 <entry>Result</entry>
819 <entry> <parameter>string</parameter> <literal>||</literal>
820 <parameter>string</parameter> </entry>
821 <entry> <type>text</type> </entry>
825 <primary>character strings</primary>
826 <secondary>concatenation</secondary>
829 <entry><literal>'Postgre' || 'SQL'</literal></entry>
830 <entry><literal>PostgreSQL</literal></entry>
834 <entry><function>bit_length</function>(<parameter>string</parameter>)</entry>
835 <entry><type>integer</type></entry>
836 <entry>number of bits in string</entry>
837 <entry><literal>bit_length('jose')</literal></entry>
838 <entry><literal>32</literal></entry>
842 <entry><function>char_length</function>(<parameter>string</parameter>) or <function>character_length</function>(<parameter>string</parameter>)</entry>
843 <entry><type>integer</type></entry>
845 number of characters in string
847 <primary>character strings</primary>
848 <secondary>length</secondary>
851 <primary>length</primary>
852 <secondary>character strings</secondary>
853 <see>character strings, length</see>
856 <entry><literal>char_length('jose')</literal></entry>
857 <entry><literal>4</literal></entry>
861 <entry><function>convert</function>(<parameter>string</parameter>
862 using <parameter>conversion_name</parameter>)</entry>
863 <entry><type>text</type></entry>
864 <entry>Change encoding using specified conversion name.
865 Conversions can be defined by <command>CREATE CONVERSION</command>.
866 Also there are some pre-defined conversion names.
867 See <xref linkend="conversion-names"> for available
868 conversion names. </entry>
869 <entry><literal>convert('PostgreSQL' using iso8859_1_to_utf8)</literal></entry>
870 <entry><literal>'PostgreSQL' in UNICODE(UTF-8) encoding</literal></entry>
874 <entry><function>lower</function>(<parameter>string</parameter>)</entry>
875 <entry><type>text</type></entry>
876 <entry>Convert string to lower case.</entry>
877 <entry><literal>lower('TOM')</literal></entry>
878 <entry><literal>tom</literal></entry>
882 <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
883 <entry><type>integer</type></entry>
884 <entry>number of bytes in string</entry>
885 <entry><literal>octet_length('jose')</literal></entry>
886 <entry><literal>4</literal></entry>
890 <entry><function>overlay</function>(<parameter>string</parameter> placing <parameter>string</parameter> from <type>integer</type> <optional>for <type>integer</type></optional>)</entry>
891 <entry><type>text</type></entry>
895 <primary>overlay</primary>
898 <entry><literal>overlay('Txxxxas' placing 'hom' from 2 for 4)</literal></entry>
899 <entry><literal>Thomas</literal></entry>
903 <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
904 <entry><type>integer</type></entry>
905 <entry>location of specified substring</entry>
906 <entry><literal>position('om' in 'Thomas')</literal></entry>
907 <entry><literal>3</literal></entry>
911 <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
912 <entry><type>text</type></entry>
916 <primary>substring</primary>
919 <entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
920 <entry><literal>hom</literal></entry>
924 <entry><function>substring</function>(<parameter>string</parameter> <optional>from <replaceable>pattern</replaceable></optional> <optional>for <replaceable>escape</replaceable></optional>)</entry>
925 <entry><type>text</type></entry>
927 extract regular expression
929 <primary>substring</primary>
932 <entry><literal>substring('Thomas' from 'mas$' for <optional>escape '\\'</optional>)</literal></entry>
933 <entry><literal>mas</literal></entry>
938 <function>trim</function>(<optional>leading | trailing | both</optional>
939 <optional><parameter>characters</parameter></optional> from
940 <parameter>string</parameter>)
942 <entry><type>text</type></entry>
944 Removes the longest string containing only the
945 <parameter>characters</parameter> (a space by default) from the
946 beginning/end/both ends of the <parameter>string</parameter>.
948 <entry><literal>trim(both 'x' from 'xTomxx')</literal></entry>
949 <entry><literal>Tom</literal></entry>
953 <entry><function>upper</function>(<parameter>string</parameter>)</entry>
954 <entry><type>text</type></entry>
955 <entry>Convert string to upper case.</entry>
956 <entry><literal>upper('tom')</literal></entry>
957 <entry><literal>TOM</literal></entry>
963 <table id="conversion-names">
964 <title>Available conversion names</title>
968 <entry>conversion</entry>
969 <entry>source encoding</entry>
970 <entry>destination encoding</entry>
976 <entry>ascii_to_mic</entry>
977 <entry>SQL_ASCII</entry>
978 <entry>MULE_INTERNAL</entry>
982 <entry>ascii_to_utf_8</entry>
983 <entry>SQL_ASCII</entry>
984 <entry>UNICODE</entry>
988 <entry>big5_to_euc_tw</entry>
990 <entry>EUC_TW</entry>
994 <entry>big5_to_mic</entry>
996 <entry>MULE_INTERNAL</entry>
1000 <entry>big5_to_utf_8</entry>
1002 <entry>UNICODE</entry>
1006 <entry>euc_cn_to_mic</entry>
1007 <entry>EUC_CN</entry>
1008 <entry>MULE_INTERNAL</entry>
1012 <entry>euc_cn_to_utf_8</entry>
1013 <entry>EUC_CN</entry>
1014 <entry>UNICODE</entry>
1018 <entry>euc_jp_to_mic</entry>
1019 <entry>EUC_JP</entry>
1020 <entry>MULE_INTERNAL</entry>
1024 <entry>euc_jp_to_sjis</entry>
1025 <entry>EUC_JP</entry>
1030 <entry>euc_jp_to_utf_8</entry>
1031 <entry>EUC_JP</entry>
1032 <entry>UNICODE</entry>
1036 <entry>euc_kr_to_mic</entry>
1037 <entry>EUC_KR</entry>
1038 <entry>MULE_INTERNAL</entry>
1042 <entry>euc_kr_to_utf_8</entry>
1043 <entry>EUC_KR</entry>
1044 <entry>UNICODE</entry>
1048 <entry>euc_tw_to_big5</entry>
1049 <entry>EUC_TW</entry>
1054 <entry>euc_tw_to_mic</entry>
1055 <entry>EUC_TW</entry>
1056 <entry>MULE_INTERNAL</entry>
1060 <entry>euc_tw_to_utf_8</entry>
1061 <entry>EUC_TW</entry>
1062 <entry>UNICODE</entry>
1066 <entry>gb18030_to_utf_8</entry>
1067 <entry>GB18030</entry>
1068 <entry>UNICODE</entry>
1072 <entry>gbk_to_utf_8</entry>
1074 <entry>UNICODE</entry>
1078 <entry>iso_8859_10_to_utf_8</entry>
1079 <entry>LATIN6</entry>
1080 <entry>UNICODE</entry>
1084 <entry>iso_8859_13_to_utf_8</entry>
1085 <entry>LATIN7</entry>
1086 <entry>UNICODE</entry>
1090 <entry>iso_8859_14_to_utf_8</entry>
1091 <entry>LATIN8</entry>
1092 <entry>UNICODE</entry>
1096 <entry>iso_8859_15_to_utf_8</entry>
1097 <entry>LATIN9</entry>
1098 <entry>UNICODE</entry>
1102 <entry>iso_8859_16_to_utf_8</entry>
1103 <entry>LATIN10</entry>
1104 <entry>UNICODE</entry>
1108 <entry>iso_8859_1_to_mic</entry>
1109 <entry>LATIN1</entry>
1110 <entry>MULE_INTERNAL</entry>
1114 <entry>iso_8859_1_to_utf_8</entry>
1115 <entry>LATIN1</entry>
1116 <entry>UNICODE</entry>
1120 <entry>iso_8859_2_to_mic</entry>
1121 <entry>LATIN2</entry>
1122 <entry>MULE_INTERNAL</entry>
1126 <entry>iso_8859_2_to_utf_8</entry>
1127 <entry>LATIN2</entry>
1128 <entry>UNICODE</entry>
1132 <entry>iso_8859_2_to_win1250</entry>
1133 <entry>LATIN2</entry>
1134 <entry>WIN1250</entry>
1138 <entry>iso_8859_3_to_mic</entry>
1139 <entry>LATIN3</entry>
1140 <entry>MULE_INTERNAL</entry>
1144 <entry>iso_8859_3_to_utf_8</entry>
1145 <entry>LATIN3</entry>
1146 <entry>UNICODE</entry>
1150 <entry>iso_8859_4_to_mic</entry>
1151 <entry>LATIN4</entry>
1152 <entry>MULE_INTERNAL</entry>
1156 <entry>iso_8859_4_to_utf_8</entry>
1157 <entry>LATIN4</entry>
1158 <entry>UNICODE</entry>
1162 <entry>iso_8859_5_to_koi8r</entry>
1163 <entry>ISO_8859_5</entry>
1168 <entry>iso_8859_5_to_mic</entry>
1169 <entry>ISO_8859_5</entry>
1170 <entry>MULE_INTERNAL</entry>
1174 <entry>iso_8859_5_to_utf_8</entry>
1175 <entry>ISO_8859_5</entry>
1176 <entry>UNICODE</entry>
1180 <entry>iso_8859_5_to_win1251</entry>
1181 <entry>ISO_8859_5</entry>
1186 <entry>iso_8859_5_to_win866</entry>
1187 <entry>ISO_8859_5</entry>
1192 <entry>iso_8859_6_to_utf_8</entry>
1193 <entry>ISO_8859_6</entry>
1194 <entry>UNICODE</entry>
1198 <entry>iso_8859_7_to_utf_8</entry>
1199 <entry>ISO_8859_7</entry>
1200 <entry>UNICODE</entry>
1204 <entry>iso_8859_8_to_utf_8</entry>
1205 <entry>ISO_8859_8</entry>
1206 <entry>UNICODE</entry>
1210 <entry>iso_8859_9_to_utf_8</entry>
1211 <entry>LATIN5</entry>
1212 <entry>UNICODE</entry>
1216 <entry>johab_to_utf_8</entry>
1217 <entry>JOHAB</entry>
1218 <entry>UNICODE</entry>
1222 <entry>koi8r_to_iso_8859_5</entry>
1224 <entry>ISO_8859_5</entry>
1228 <entry>koi8r_to_mic</entry>
1230 <entry>MULE_INTERNAL</entry>
1234 <entry>koi8r_to_utf_8</entry>
1236 <entry>UNICODE</entry>
1240 <entry>koi8r_to_win1251</entry>
1246 <entry>koi8r_to_win866</entry>
1252 <entry>mic_to_ascii</entry>
1253 <entry>MULE_INTERNAL</entry>
1254 <entry>SQL_ASCII</entry>
1258 <entry>mic_to_big5</entry>
1259 <entry>MULE_INTERNAL</entry>
1264 <entry>mic_to_euc_cn</entry>
1265 <entry>MULE_INTERNAL</entry>
1266 <entry>EUC_CN</entry>
1270 <entry>mic_to_euc_jp</entry>
1271 <entry>MULE_INTERNAL</entry>
1272 <entry>EUC_JP</entry>
1276 <entry>mic_to_euc_kr</entry>
1277 <entry>MULE_INTERNAL</entry>
1278 <entry>EUC_KR</entry>
1282 <entry>mic_to_euc_tw</entry>
1283 <entry>MULE_INTERNAL</entry>
1284 <entry>EUC_TW</entry>
1288 <entry>mic_to_iso_8859_1</entry>
1289 <entry>MULE_INTERNAL</entry>
1290 <entry>LATIN1</entry>
1294 <entry>mic_to_iso_8859_2</entry>
1295 <entry>MULE_INTERNAL</entry>
1296 <entry>LATIN2</entry>
1300 <entry>mic_to_iso_8859_3</entry>
1301 <entry>MULE_INTERNAL</entry>
1302 <entry>LATIN3</entry>
1306 <entry>mic_to_iso_8859_4</entry>
1307 <entry>MULE_INTERNAL</entry>
1308 <entry>LATIN4</entry>
1312 <entry>mic_to_iso_8859_5</entry>
1313 <entry>MULE_INTERNAL</entry>
1314 <entry>ISO_8859_5</entry>
1318 <entry>mic_to_koi8r</entry>
1319 <entry>MULE_INTERNAL</entry>
1324 <entry>mic_to_sjis</entry>
1325 <entry>MULE_INTERNAL</entry>
1330 <entry>mic_to_win1250</entry>
1331 <entry>MULE_INTERNAL</entry>
1332 <entry>WIN1250</entry>
1336 <entry>mic_to_win1251</entry>
1337 <entry>MULE_INTERNAL</entry>
1342 <entry>mic_to_win866</entry>
1343 <entry>MULE_INTERNAL</entry>
1348 <entry>sjis_to_euc_jp</entry>
1350 <entry>EUC_JP</entry>
1354 <entry>sjis_to_mic</entry>
1356 <entry>MULE_INTERNAL</entry>
1360 <entry>sjis_to_utf_8</entry>
1362 <entry>UNICODE</entry>
1366 <entry>tcvn_to_utf_8</entry>
1368 <entry>UNICODE</entry>
1372 <entry>uhc_to_utf_8</entry>
1374 <entry>UNICODE</entry>
1378 <entry>utf_8_to_ascii</entry>
1379 <entry>UNICODE</entry>
1380 <entry>SQL_ASCII</entry>
1384 <entry>utf_8_to_big5</entry>
1385 <entry>UNICODE</entry>
1390 <entry>utf_8_to_euc_cn</entry>
1391 <entry>UNICODE</entry>
1392 <entry>EUC_CN</entry>
1396 <entry>utf_8_to_euc_jp</entry>
1397 <entry>UNICODE</entry>
1398 <entry>EUC_JP</entry>
1402 <entry>utf_8_to_euc_kr</entry>
1403 <entry>UNICODE</entry>
1404 <entry>EUC_KR</entry>
1408 <entry>utf_8_to_euc_tw</entry>
1409 <entry>UNICODE</entry>
1410 <entry>EUC_TW</entry>
1414 <entry>utf_8_to_gb18030</entry>
1415 <entry>UNICODE</entry>
1416 <entry>GB18030</entry>
1420 <entry>utf_8_to_gbk</entry>
1421 <entry>UNICODE</entry>
1426 <entry>utf_8_to_iso_8859_1</entry>
1427 <entry>UNICODE</entry>
1428 <entry>LATIN1</entry>
1432 <entry>utf_8_to_iso_8859_10</entry>
1433 <entry>UNICODE</entry>
1434 <entry>LATIN6</entry>
1438 <entry>utf_8_to_iso_8859_13</entry>
1439 <entry>UNICODE</entry>
1440 <entry>LATIN7</entry>
1444 <entry>utf_8_to_iso_8859_14</entry>
1445 <entry>UNICODE</entry>
1446 <entry>LATIN8</entry>
1450 <entry>utf_8_to_iso_8859_15</entry>
1451 <entry>UNICODE</entry>
1452 <entry>LATIN9</entry>
1456 <entry>utf_8_to_iso_8859_16</entry>
1457 <entry>UNICODE</entry>
1458 <entry>LATIN10</entry>
1462 <entry>utf_8_to_iso_8859_2</entry>
1463 <entry>UNICODE</entry>
1464 <entry>LATIN2</entry>
1468 <entry>utf_8_to_iso_8859_3</entry>
1469 <entry>UNICODE</entry>
1470 <entry>LATIN3</entry>
1474 <entry>utf_8_to_iso_8859_4</entry>
1475 <entry>UNICODE</entry>
1476 <entry>LATIN4</entry>
1480 <entry>utf_8_to_iso_8859_5</entry>
1481 <entry>UNICODE</entry>
1482 <entry>ISO_8859_5</entry>
1486 <entry>utf_8_to_iso_8859_6</entry>
1487 <entry>UNICODE</entry>
1488 <entry>ISO_8859_6</entry>
1492 <entry>utf_8_to_iso_8859_7</entry>
1493 <entry>UNICODE</entry>
1494 <entry>ISO_8859_7</entry>
1498 <entry>utf_8_to_iso_8859_8</entry>
1499 <entry>UNICODE</entry>
1500 <entry>ISO_8859_8</entry>
1504 <entry>utf_8_to_iso_8859_9</entry>
1505 <entry>UNICODE</entry>
1506 <entry>LATIN5</entry>
1510 <entry>utf_8_to_johab</entry>
1511 <entry>UNICODE</entry>
1512 <entry>JOHAB</entry>
1516 <entry>utf_8_to_koi8r</entry>
1517 <entry>UNICODE</entry>
1522 <entry>utf_8_to_sjis</entry>
1523 <entry>UNICODE</entry>
1528 <entry>utf_8_to_tcvn</entry>
1529 <entry>UNICODE</entry>
1534 <entry>utf_8_to_uhc</entry>
1535 <entry>UNICODE</entry>
1540 <entry>utf_8_to_win1250</entry>
1541 <entry>UNICODE</entry>
1542 <entry>WIN1250</entry>
1546 <entry>utf_8_to_win1251</entry>
1547 <entry>UNICODE</entry>
1552 <entry>utf_8_to_win1256</entry>
1553 <entry>UNICODE</entry>
1554 <entry>WIN1256</entry>
1558 <entry>utf_8_to_win866</entry>
1559 <entry>UNICODE</entry>
1564 <entry>utf_8_to_win874</entry>
1565 <entry>UNICODE</entry>
1566 <entry>WIN874</entry>
1570 <entry>win1250_to_iso_8859_2</entry>
1571 <entry>WIN1250</entry>
1572 <entry>LATIN2</entry>
1576 <entry>win1250_to_mic</entry>
1577 <entry>WIN1250</entry>
1578 <entry>MULE_INTERNAL</entry>
1582 <entry>win1250_to_utf_8</entry>
1583 <entry>WIN1250</entry>
1584 <entry>UNICODE</entry>
1588 <entry>win1251_to_iso_8859_5</entry>
1590 <entry>ISO_8859_5</entry>
1594 <entry>win1251_to_koi8r</entry>
1600 <entry>win1251_to_mic</entry>
1602 <entry>MULE_INTERNAL</entry>
1606 <entry>win1251_to_utf_8</entry>
1608 <entry>UNICODE</entry>
1612 <entry>win1251_to_win866</entry>
1618 <entry>win1256_to_utf_8</entry>
1619 <entry>WIN1256</entry>
1620 <entry>UNICODE</entry>
1624 <entry>win866_to_iso_8859_5</entry>
1626 <entry>ISO_8859_5</entry>
1630 <entry>win866_to_koi8r</entry>
1636 <entry>win866_to_mic</entry>
1638 <entry>MULE_INTERNAL</entry>
1642 <entry>win866_to_utf_8</entry>
1644 <entry>UNICODE</entry>
1648 <entry>win866_to_win1251</entry>
1654 <entry>win874_to_utf_8</entry>
1655 <entry>WIN874</entry>
1656 <entry>UNICODE</entry>
1664 Additional string manipulation functions are available and are
1665 listed below. Some of them are used internally to implement the
1666 <acronym>SQL</acronym>-standard string functions listed above.
1669 <table id="functions-string-other">
1670 <title>Other String Functions</title>
1674 <entry>Function</entry>
1675 <entry>Return Type</entry>
1676 <entry>Description</entry>
1677 <entry>Example</entry>
1678 <entry>Result</entry>
1684 <entry><function>ascii</function>(<type>text</type>)</entry>
1685 <entry>integer</entry>
1686 <entry>Returns the <acronym>ASCII</acronym> code of the first character of the argument.</entry>
1687 <entry><literal>ascii('x')</literal></entry>
1688 <entry><literal>120</literal></entry>
1692 <entry><function>btrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>trim</parameter> <type>text</type>)</entry>
1693 <entry><type>text</type></entry>
1695 Remove (trim) the longest string consisting only of characters
1696 in <parameter>trim</parameter> from the start and end of
1697 <parameter>string</parameter>.
1699 <entry><literal>btrim('xyxtrimyyx','xy')</literal></entry>
1700 <entry><literal>trim</literal></entry>
1704 <entry><function>chr</function>(<type>integer</type>)</entry>
1705 <entry><type>text</type></entry>
1706 <entry>Returns the character with the given <acronym>ASCII</acronym> code.</entry>
1707 <entry><literal>chr(65)</literal></entry>
1708 <entry><literal>A</literal></entry>
1713 <function>convert</function>(<parameter>string</parameter>
1715 <optional><parameter>src_encoding</parameter> <type>name</type>,</optional>
1716 <parameter>dest_encoding</parameter> <type>name</type>)
1718 <entry><type>text</type></entry>
1720 Converts string using <parameter>dest_encoding</parameter>.
1721 The original encoding is specified by
1722 <parameter>src_encoding</parameter>. If
1723 <parameter>src_encoding</parameter> is omitted, database
1724 encoding is assumed.
1726 <entry><literal>convert('text_in_unicode', 'UNICODE', 'LATIN1')</literal></entry>
1727 <entry><literal>text_in_unicode</literal> represented in ISO 8859-1</entry>
1732 <function>decode</function>(<parameter>string</parameter> <type>text</type>,
1733 <parameter>type</parameter> <type>text</type>)
1735 <entry><type>bytea</type></entry>
1737 Decodes binary data from <parameter>string</parameter> previously
1738 encoded with encode(). Parameter type is same as in encode().
1740 <entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
1741 <entry><literal>123\000\001</literal></entry>
1746 <function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
1747 <parameter>type</parameter> <type>text</type>)
1749 <entry><type>text</type></entry>
1751 Encodes binary data to <acronym>ASCII</acronym>-only representation. Supported
1752 types are: 'base64', 'hex', 'escape'.
1754 <entry><literal>encode('123\\000\\001', 'base64')</literal></entry>
1755 <entry><literal>MTIzAAE=</literal></entry>
1759 <entry><function>initcap</function>(<type>text</type>)</entry>
1760 <entry><type>text</type></entry>
1761 <entry>Converts first letter of each word (whitespace separated) to upper case.</entry>
1762 <entry><literal>initcap('hi thomas')</literal></entry>
1763 <entry><literal>Hi Thomas</literal></entry>
1767 <entry><function>length</function>(<parameter>string</parameter>)</entry>
1768 <entry><type>integer</type></entry>
1772 <primary>character strings</primary>
1773 <secondary>length</secondary>
1776 <primary>length</primary>
1777 <secondary>character strings</secondary>
1778 <see>character strings, length</see>
1781 <entry><literal>length('jose')</literal></entry>
1782 <entry><literal>4</literal></entry>
1787 <function>lpad</function>(<parameter>string</parameter> <type>text</type>,
1788 <parameter>length</parameter> <type>integer</type>
1789 <optional>, <parameter>fill</parameter> <type>text</type></optional>)
1793 Fills up the <parameter>string</parameter> to length
1794 <parameter>length</parameter> by prepending the characters
1795 <parameter>fill</parameter> (a space by default). If the
1796 <parameter>string</parameter> is already longer than
1797 <parameter>length</parameter> then it is truncated (on the
1800 <entry><literal>lpad('hi', 5, 'xy')</literal></entry>
1801 <entry><literal>xyxhi</literal></entry>
1805 <entry><function>ltrim</function>(<parameter>string</parameter> <type>text</type>, <parameter>text</parameter> <type>text</type>)</entry>
1806 <entry><type>text</type></entry>
1808 Removes the longest string containing only characters from
1809 <parameter>trim</parameter> from the start of the string.
1811 <entry><literal>ltrim('zzzytrim','xyz')</literal></entry>
1812 <entry><literal>trim</literal></entry>
1816 <entry><function>pg_client_encoding</function>()</entry>
1817 <entry><type>name</type></entry>
1819 Returns current client encoding name.
1821 <entry><literal>pg_client_encoding()</literal></entry>
1822 <entry><literal>SQL_ASCII</literal></entry>
1826 <entry><function>quote_ident</function>(<parameter>string</parameter> text)</entry>
1827 <entry><type>text</type></entry>
1829 Returns the given string suitably quoted to be used as an identifier
1830 in an SQL query string.
1831 Quotes are added only if necessary (i.e., if the string contains
1832 non-identifier characters or would be case-folded).
1833 Embedded quotes are properly doubled.
1835 <entry><literal>quote_ident('Foo')</literal></entry>
1836 <entry><literal>"Foo"</literal></entry>
1840 <entry><function>quote_literal</function>(<parameter>string</parameter> text)</entry>
1841 <entry><type>text</type></entry>
1843 Returns the given string suitably quoted to be used as a literal
1844 in an SQL query string.
1845 Embedded quotes and backslashes are properly doubled.
1847 <entry><literal>quote_literal('O\'Reilly')</literal></entry>
1848 <entry><literal>'O''Reilly'</literal></entry>
1852 <entry><function>repeat</function>(<type>text</type>, <type>integer</type>)</entry>
1853 <entry><type>text</type></entry>
1854 <entry>Repeat text a number of times.</entry>
1855 <entry><literal>repeat('Pg', 4)</literal></entry>
1856 <entry><literal>PgPgPgPg</literal></entry>
1860 <entry><function>replace</function>(<parameter>string</parameter> <type>text</type>,
1861 <parameter>from</parameter> <type>text</type>,
1862 <parameter>to</parameter> <type>text</type>)</entry>
1863 <entry><type>text</type></entry>
1864 <entry>Replace all occurrences in <parameter>string</parameter> of substring
1865 <parameter>from</parameter> with substring <parameter>to</parameter>
1867 <entry><literal>replace('abcdefabcdef', 'cd', 'XX')</literal></entry>
1868 <entry><literal>abXXefabXXef</literal></entry>
1873 <function>rpad</function>(<parameter>string</parameter> <type>text</type>,
1874 <parameter>length</parameter> <type>integer</type>
1875 <optional>, <parameter>fill</parameter> <type>text</type></optional>)
1877 <entry><type>text</type></entry>
1879 Fills up the <parameter>string</parameter> to length
1880 <parameter>length</parameter> by appending the characters
1881 <parameter>fill</parameter> (a space by default). If the
1882 <parameter>string</parameter> is already longer than
1883 <parameter>length</parameter> then it is truncated.
1885 <entry><literal>rpad('hi', 5, 'xy')</literal></entry>
1886 <entry><literal>hixyx</literal></entry>
1890 <entry><function>rtrim</function>(<parameter>string</parameter>
1891 text, <parameter>trim</parameter> text)</entry>
1892 <entry><type>text</type></entry>
1894 Removes the longest string containing only characters from
1895 <parameter>trim</parameter> from the end of the string.
1897 <entry><literal>rtrim('trimxxxx','x')</literal></entry>
1898 <entry><literal>trim</literal></entry>
1902 <entry><function>split</function>(<parameter>string</parameter> <type>text</type>,
1903 <parameter>delimiter</parameter> <type>text</type>,
1904 <parameter>column</parameter> <type>integer</type>)</entry>
1905 <entry><type>text</type></entry>
1906 <entry>Split <parameter>string</parameter> on <parameter>delimiter</parameter>
1907 returning the resulting (one based) <parameter>column</parameter> number.
1909 <entry><literal>split('abc~@~def~@~ghi','~@~',2)</literal></entry>
1910 <entry><literal>def</literal></entry>
1914 <entry><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</entry>
1915 <entry><type>text</type></entry>
1917 Locates specified substring. (same as
1918 <literal>position(<parameter>substring</parameter> in
1919 <parameter>string</parameter>)</literal>, but note the reversed
1922 <entry><literal>strpos('high','ig')</literal></entry>
1923 <entry><literal>2</literal></entry>
1927 <entry><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</entry>
1928 <entry><type>text</type></entry>
1930 Extracts specified substring. (same as
1931 <literal>substring(<parameter>string</parameter> from <parameter>from</parameter> for <parameter>count</parameter>)</literal>)
1933 <entry><literal>substr('alphabet', 3, 2)</literal></entry>
1934 <entry><literal>ph</literal></entry>
1938 <entry><function>to_ascii</function>(<type>text</type>
1939 <optional>, <parameter>encoding</parameter></optional>)</entry>
1940 <entry><type>text</type></entry>
1941 <entry>Converts text from multibyte encoding to <acronym>ASCII</acronym>.</entry>
1942 <entry><literal>to_ascii('Karel')</literal></entry>
1943 <entry><literal>Karel</literal></entry>
1947 <entry><function>to_hex</function>(<parameter>number</parameter> <type>integer</type>
1948 or <type>bigint</type>)</entry>
1949 <entry><type>text</type></entry>
1950 <entry>Convert <parameter>number</parameter> to its equivalent hexadecimal
1953 <entry><literal>to_hex(9223372036854775807::bigint)</literal></entry>
1954 <entry><literal>7fffffffffffffff</literal></entry>
1959 <function>translate</function>(<parameter>string</parameter>
1961 <parameter>from</parameter> <type>text</type>,
1962 <parameter>to</parameter> <type>text</type>)
1964 <entry><type>text</type></entry>
1966 Any character in <parameter>string</parameter> that matches a
1967 character in the <parameter>from</parameter> set is replaced by
1968 the corresponding character in the <parameter>to</parameter>
1971 <entry><literal>translate('12345', '14', 'ax')</literal></entry>
1972 <entry><literal>a23x5</literal></entry>
1980 The <function>to_ascii</function> function supports conversion from
1981 LATIN1, LATIN2, WIN1250 (CP1250) only.
1986 <sect1 id="functions-binarystring">
1987 <title>Binary String Functions and Operators</title>
1990 This section describes functions and operators for examining and
1991 manipulating binary string values. Strings in this context include
1992 values of the type <type>BYTEA</type>.
1996 <acronym>SQL</acronym> defines some string functions with a
1997 special syntax where
1998 certain keywords rather than commas are used to separate the
1999 arguments. Details are in
2000 <xref linkend="functions-binarystring-sql">.
2001 Some functions are also implemented using the regular syntax for
2002 function invocation.
2003 (See <xref linkend="functions-binarystring-other">.)
2006 <table id="functions-binarystring-sql">
2007 <title><acronym>SQL</acronym> Binary String Functions and Operators</title>
2011 <entry>Function</entry>
2012 <entry>Return Type</entry>
2013 <entry>Description</entry>
2014 <entry>Example</entry>
2015 <entry>Result</entry>
2021 <entry> <parameter>string</parameter> <literal>||</literal>
2022 <parameter>string</parameter> </entry>
2023 <entry> <type>bytea</type> </entry>
2025 string concatenation
2027 <primary>binary strings</primary>
2028 <secondary>concatenation</secondary>
2031 <entry><literal>'\\\\Postgre'::bytea || '\\047SQL\\000'::bytea</literal></entry>
2032 <entry><literal>\\Postgre'SQL\000</literal></entry>
2036 <entry><function>octet_length</function>(<parameter>string</parameter>)</entry>
2037 <entry><type>integer</type></entry>
2038 <entry>number of bytes in binary string</entry>
2039 <entry><literal>octet_length('jo\\000se'::bytea)</literal></entry>
2040 <entry><literal>5</literal></entry>
2044 <entry><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</entry>
2045 <entry><type>integer</type></entry>
2046 <entry>location of specified substring</entry>
2047 <entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
2048 <entry><literal>3</literal></entry>
2052 <entry><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</entry>
2053 <entry><type>bytea</type></entry>
2057 <primary>substring</primary>
2060 <entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
2061 <entry><literal>h\000o</literal></entry>
2066 <function>trim</function>(<optional>both</optional>
2067 <parameter>characters</parameter> from
2068 <parameter>string</parameter>)
2070 <entry><type>bytea</type></entry>
2072 Removes the longest string containing only the
2073 <parameter>characters</parameter> from the
2074 beginning/end/both ends of the <parameter>string</parameter>.
2076 <entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
2077 <entry><literal>Tom</literal></entry>
2084 Additional binary string manipulation functions are available and are
2085 listed below. Some of them are used internally to implement the
2086 <acronym>SQL</acronym>-standard string functions listed above.
2089 <table id="functions-binarystring-other">
2090 <title>Other Binary String Functions</title>
2094 <entry>Function</entry>
2095 <entry>Return Type</entry>
2096 <entry>Description</entry>
2097 <entry>Example</entry>
2098 <entry>Result</entry>
2104 <entry><function>btrim</function>(<parameter>string</parameter>
2105 <type>bytea</type> <parameter>trim</parameter> <type>bytea</type>)</entry>
2106 <entry><type>bytea</type></entry>
2108 Remove (trim) the longest string consisting only of characters
2109 in <parameter>trim</parameter> from the start and end of
2110 <parameter>string</parameter>.
2112 <entry><literal>btrim('\\000trim\\000'::bytea,'\\000'::bytea)</literal></entry>
2113 <entry><literal>trim</literal></entry>
2117 <entry><function>length</function>(<parameter>string</parameter>)</entry>
2118 <entry><type>integer</type></entry>
2120 length of binary string
2122 <primary>binary strings</primary>
2123 <secondary>length</secondary>
2126 <primary>length</primary>
2127 <secondary>binary strings</secondary>
2128 <see>binary strings, length</see>
2131 <entry><literal>length('jo\\000se'::bytea)</literal></entry>
2132 <entry><literal>5</literal></entry>
2137 <function>encode</function>(<parameter>string</parameter> <type>bytea</type>,
2138 <parameter>type</parameter> <type>text</type>)
2140 <entry><type>text</type></entry>
2142 Encodes binary string to <acronym>ASCII</acronym>-only representation. Supported
2143 types are: 'base64', 'hex', 'escape'.
2145 <entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
2146 <entry><literal>123\000456</literal></entry>
2151 <function>decode</function>(<parameter>string</parameter> <type>text</type>,
2152 <parameter>type</parameter> <type>text</type>)
2154 <entry><type>bytea</type></entry>
2156 Decodes binary string from <parameter>string</parameter> previously
2157 encoded with encode(). Parameter type is same as in encode().
2159 <entry><literal>decode('123\\000456', 'escape')</literal></entry>
2160 <entry><literal>123\000456</literal></entry>
2170 <sect1 id="functions-matching">
2171 <title>Pattern Matching</title>
2174 There are two separate approaches to pattern matching provided by
2175 <productname>PostgreSQL</productname>: the traditional
2176 <acronym>SQL</acronym>
2177 <function>LIKE</function> operator and the more recent
2178 <acronym>SQL99</acronym>
2179 <function>SIMILAR TO</function> operator implementing
2180 <acronym>POSIX</acronym>-style regular expressions.
2181 Additionally, a pattern matching function,
2182 <function>SUBSTRING</function>, is available, as defined in
2183 <acronym>SQL99</acronym>.
2188 If you have pattern matching needs that go beyond this,
2189 consider writing a user-defined function in Perl or Tcl.
2194 Both <function>LIKE</function> and <function>SIMILAR TO</function>
2195 are SQL-standard operators which are also available in alternate
2196 forms as <productname>PostgreSQL</productname> operators; look at
2197 <literal>~</literal> and <literal>~~</literal> for examples.
2200 <sect2 id="functions-like">
2201 <title><function>LIKE</function></title>
2204 <primary>like</primary>
2208 <replaceable>string</replaceable> LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2209 <replaceable>string</replaceable> NOT LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2213 Every <replaceable>pattern</replaceable> defines a set of strings.
2214 The <function>LIKE</function> expression returns true if the
2215 <replaceable>string</replaceable> is contained in the set of
2216 strings represented by <replaceable>pattern</replaceable>. (As
2217 expected, the <function>NOT LIKE</function> expression returns
2218 false if <function>LIKE</function> returns true, and vice versa.
2219 An equivalent expression is
2220 <literal>NOT (<replaceable>string</replaceable> LIKE
2221 <replaceable>pattern</replaceable>)</literal>.)
2225 If <replaceable>pattern</replaceable> does not contain percent
2226 signs or underscore, then the pattern only represents the string
2227 itself; in that case <function>LIKE</function> acts like the
2228 equals operator. An underscore (<literal>_</literal>) in
2229 <replaceable>pattern</replaceable> stands for (matches) any single
2230 character; a percent sign (<literal>%</literal>) matches any string
2231 of zero or more characters.
2238 'abc' LIKE 'abc' <lineannotation>true</lineannotation>
2239 'abc' LIKE 'a%' <lineannotation>true</lineannotation>
2240 'abc' LIKE '_b_' <lineannotation>true</lineannotation>
2241 'abc' LIKE 'c' <lineannotation>false</lineannotation>
2247 <function>LIKE</function> pattern matches always cover the entire
2248 string. To match a pattern anywhere within a string, the
2249 pattern must therefore start and end with a percent sign.
2253 To match a literal underscore or percent sign without matching
2254 other characters, the respective character in
2255 <replaceable>pattern</replaceable> must be
2256 preceded by the escape character. The default escape
2257 character is the backslash but a different one may be selected by
2258 using the <literal>ESCAPE</literal> clause. To match the escape
2259 character itself, write two escape characters.
2263 Note that the backslash already has a special meaning in string
2264 literals, so to write a pattern constant that contains a backslash
2265 you must write two backslashes in the query. Thus, writing a pattern
2266 that actually matches a literal backslash means writing four backslashes
2267 in the query. You can avoid this by selecting a different escape
2268 character with <literal>ESCAPE</literal>; then backslash is not special
2269 to <function>LIKE</function> anymore. (But it is still special to the string
2270 literal parser, so you still need two of them.)
2274 It's also possible to select no escape character by writing
2275 <literal>ESCAPE ''</literal>. This effectively disables the
2276 escape mechanism, which makes it impossible to turn off the
2277 special meaning of underscore and percent signs in the pattern.
2281 The keyword <token>ILIKE</token> can be used instead of
2282 <token>LIKE</token> to make the match case insensitive according
2283 to the active locale. This is not in the <acronym>SQL</acronym> standard but is a
2284 <productname>PostgreSQL</productname> extension.
2288 The operator <literal>~~</literal> is equivalent to
2289 <function>LIKE</function>, and <literal>~~*</literal> corresponds to
2290 <function>ILIKE</function>. There are also
2291 <literal>!~~</literal> and <literal>!~~*</literal> operators that
2292 represent <function>NOT LIKE</function> and <function>NOT
2293 ILIKE</function>. All of these operators are
2294 <productname>PostgreSQL</productname>-specific.
2299 <sect2 id="functions-regexp">
2300 <title><function>SIMILAR TO</function> and <acronym>POSIX</acronym>
2301 Regular Expressions</title>
2303 <indexterm zone="functions-regexp">
2304 <primary>regular expressions</primary>
2305 <seealso>pattern matching</seealso>
2309 <title>Regular Expression Match Operators</title>
2314 <entry>Operator</entry>
2315 <entry>Description</entry>
2316 <entry>Example</entry>
2322 <entry> <literal>~</literal> </entry>
2323 <entry>Matches regular expression, case sensitive</entry>
2324 <entry><literal>'thomas' ~ '.*thomas.*'</literal></entry>
2328 <entry> <literal>~*</literal> </entry>
2329 <entry>Matches regular expression, case insensitive</entry>
2330 <entry><literal>'thomas' ~* '.*Thomas.*'</literal></entry>
2334 <entry> <literal>!~</literal> </entry>
2335 <entry>Does not match regular expression, case sensitive</entry>
2336 <entry><literal>'thomas' !~ '.*Thomas.*'</literal></entry>
2340 <entry> <literal>!~*</literal> </entry>
2341 <entry>Does not match regular expression, case insensitive</entry>
2342 <entry><literal>'thomas' !~* '.*vadim.*'</literal></entry>
2346 <entry> <literal>SIMILAR TO</literal> </entry>
2347 <entry>Matches regular expression, case sensitive</entry>
2348 <entry><literal>'thomas' SIMILAR TO '.*thomas.*'</literal></entry>
2355 <acronym>POSIX</acronym> regular expressions provide a more
2357 pattern matching than the <function>LIKE</function> function.
2358 Many Unix tools such as <command>egrep</command>,
2359 <command>sed</command>, or <command>awk</command> use a pattern
2360 matching language that is similar to the one described here.
2364 A regular expression is a character sequence that is an
2365 abbreviated definition of a set of strings (a <firstterm>regular
2366 set</firstterm>). A string is said to match a regular expression
2367 if it is a member of the regular set described by the regular
2368 expression. As with <function>LIKE</function>, pattern characters
2369 match string characters exactly unless they are special characters
2370 in the regular expression language --- but regular expressions use
2371 different special characters than <function>LIKE</function> does.
2372 Unlike <function>LIKE</function> patterns, a
2373 regular expression is allowed to match anywhere within a string, unless
2374 the regular expression is explicitly anchored to the beginning or
2382 'abc' SIMILAR TO 'abc' <lineannotation>true</lineannotation>
2383 'abc' SIMILAR TO '^a' <lineannotation>true</lineannotation>
2384 'abc' SIMILAR TO '(b|d)' <lineannotation>true</lineannotation>
2385 'abc' SIMILAR TO '^(b|c)' <lineannotation>false</lineannotation>
2390 <!-- derived from the re_format.7 man page -->
2392 Regular expressions (<quote>RE</quote>s), as defined in
2393 <acronym>POSIX</acronym>
2394 1003.2, come in two forms: modern REs (roughly those of
2395 <command>egrep</command>; 1003.2 calls these
2396 <quote>extended</quote> REs) and obsolete REs (roughly those of
2397 <command>ed</command>; 1003.2 <quote>basic</quote> REs).
2398 <productname>PostgreSQL</productname> implements the modern form.
2402 A (modern) RE is one or more non-empty
2403 <firstterm>branches</firstterm>, separated by
2404 <literal>|</literal>. It matches anything that matches one of the
2409 A branch is one or more <firstterm>pieces</firstterm>,
2410 concatenated. It matches a match for the first, followed by a
2411 match for the second, etc.
2415 A piece is an <firstterm>atom</firstterm> possibly followed by a
2416 single <literal>*</literal>, <literal>+</literal>,
2417 <literal>?</literal>, or <firstterm>bound</firstterm>. An atom
2418 followed by <literal>*</literal> matches a sequence of 0 or more
2419 matches of the atom. An atom followed by <literal>+</literal>
2420 matches a sequence of 1 or more matches of the atom. An atom
2421 followed by <literal>?</literal> matches a sequence of 0 or 1
2422 matches of the atom.
2426 A <firstterm>bound</firstterm> is <literal>{</literal> followed by
2427 an unsigned decimal integer, possibly followed by
2428 <literal>,</literal> possibly followed by another unsigned decimal
2429 integer, always followed by <literal>}</literal>. The integers
2430 must lie between 0 and <symbol>RE_DUP_MAX</symbol> (255)
2431 inclusive, and if there are two of them, the first may not exceed
2432 the second. An atom followed by a bound containing one integer
2433 <replaceable>i</replaceable> and no comma matches a sequence of
2434 exactly <replaceable>i</replaceable> matches of the atom. An atom
2435 followed by a bound containing one integer
2436 <replaceable>i</replaceable> and a comma matches a sequence of
2437 <replaceable>i</replaceable> or more matches of the atom. An atom
2438 followed by a bound containing two integers
2439 <replaceable>i</replaceable> and <replaceable>j</replaceable>
2440 matches a sequence of <replaceable>i</replaceable> through
2441 <replaceable>j</replaceable> (inclusive) matches of the atom.
2446 A repetition operator (<literal>?</literal>,
2447 <literal>*</literal>, <literal>+</literal>, or bounds) cannot
2448 follow another repetition operator. A repetition operator cannot
2449 begin an expression or subexpression or follow
2450 <literal>^</literal> or <literal>|</literal>.
2455 An <firstterm>atom</firstterm> is a regular expression enclosed in
2456 <literal>()</literal> (matching a match for the regular
2457 expression), an empty set of <literal>()</literal> (matching the
2458 null string), a <firstterm>bracket expression</firstterm> (see
2459 below), <literal>.</literal> (matching any single character),
2460 <literal>^</literal> (matching the null string at the beginning of the
2461 input string), <literal>$</literal> (matching the null string at the end
2462 of the input string), a <literal>\</literal> followed by one of the
2463 characters <literal>^.[$()|*+?{\</literal> (matching that
2464 character taken as an ordinary character), a <literal>\</literal>
2465 followed by any other character (matching that character taken as
2466 an ordinary character, as if the <literal>\</literal> had not been
2467 present), or a single character with no other significance
2468 (matching that character). A <literal>{</literal> followed by a
2469 character other than a digit is an ordinary character, not the
2470 beginning of a bound. It is illegal to end an RE with
2471 <literal>\</literal>.
2475 Note that the backslash (<literal>\</literal>) already has a special
2477 literals, so to write a pattern constant that contains a backslash
2478 you must write two backslashes in the query.
2482 A <firstterm>bracket expression</firstterm> is a list of
2483 characters enclosed in <literal>[]</literal>. It normally matches
2484 any single character from the list (but see below). If the list
2485 begins with <literal>^</literal>, it matches any single character
2486 (but see below) not from the rest of the list. If two characters
2487 in the list are separated by <literal>-</literal>, this is
2488 shorthand for the full range of characters between those two
2489 (inclusive) in the collating sequence,
2490 e.g. <literal>[0-9]</literal> in <acronym>ASCII</acronym> matches
2491 any decimal digit. It is illegal for two ranges to share an
2492 endpoint, e.g. <literal>a-c-e</literal>. Ranges are very
2493 collating-sequence-dependent, and portable programs should avoid
2498 To include a literal <literal>]</literal> in the list, make it the
2499 first character (following a possible <literal>^</literal>). To
2500 include a literal <literal>-</literal>, make it the first or last
2501 character, or the second endpoint of a range. To use a literal
2502 <literal>-</literal> as the first endpoint of a range, enclose it
2503 in <literal>[.</literal> and <literal>.]</literal> to make it a
2504 collating element (see below). With the exception of these and
2505 some combinations using <literal>[</literal> (see next
2506 paragraphs), all other special characters, including
2507 <literal>\</literal>, lose their special significance within a
2512 Within a bracket expression, a collating element (a character, a
2513 multiple-character sequence that collates as if it were a single
2514 character, or a collating-sequence name for either) enclosed in
2515 <literal>[.</literal> and <literal>.]</literal> stands for the
2516 sequence of characters of that collating element. The sequence is
2517 a single element of the bracket expression's list. A bracket
2518 expression containing a multiple-character collating element can thus
2519 match more than one character, e.g. if the collating sequence
2520 includes a <literal>ch</literal> collating element, then the RE
2521 <literal>[[.ch.]]*c</literal> matches the first five characters of
2522 <literal>chchcc</literal>.
2526 Within a bracket expression, a collating element enclosed in
2527 <literal>[=</literal> and <literal>=]</literal> is an equivalence
2528 class, standing for the sequences of characters of all collating
2529 elements equivalent to that one, including itself. (If there are
2530 no other equivalent collating elements, the treatment is as if the
2531 enclosing delimiters were <literal>[.</literal> and
2532 <literal>.]</literal>.) For example, if <literal>o</literal> and
2533 <literal>^</literal> are the members of an equivalence class, then
2534 <literal>[[=o=]]</literal>, <literal>[[=^=]]</literal>, and
2535 <literal>[o^]</literal> are all synonymous. An equivalence class
2536 may not be an endpoint of a range.
2540 Within a bracket expression, the name of a character class
2541 enclosed in <literal>[:</literal> and <literal>:]</literal> stands
2542 for the list of all characters belonging to that class. Standard
2543 character class names are: <literal>alnum</literal>,
2544 <literal>alpha</literal>, <literal>blank</literal>,
2545 <literal>cntrl</literal>, <literal>digit</literal>,
2546 <literal>graph</literal>, <literal>lower</literal>,
2547 <literal>print</literal>, <literal>punct</literal>,
2548 <literal>space</literal>, <literal>upper</literal>,
2549 <literal>xdigit</literal>. These stand for the character classes
2551 <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
2552 A locale may provide others. A character class may not be used as
2553 an endpoint of a range.
2557 There are two special cases of bracket expressions: the bracket
2558 expressions <literal>[[:<:]]</literal> and
2559 <literal>[[:>:]]</literal> match the null string at the beginning
2560 and end of a word respectively. A word is defined as a sequence
2561 of word characters which is neither preceded nor followed by word
2562 characters. A word character is an alnum character (as defined by
2563 <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
2564 or an underscore. This is an extension, compatible with but not
2565 specified by <acronym>POSIX</acronym> 1003.2, and should be used with caution in
2566 software intended to be portable to other systems.
2570 In the event that an RE could match more than one substring of a
2571 given string, the RE matches the one starting earliest in the
2572 string. If the RE could match more than one substring starting at
2573 that point, it matches the longest. Subexpressions also match the
2574 longest possible substrings, subject to the constraint that the
2575 whole match be as long as possible, with subexpressions starting
2576 earlier in the RE taking priority over ones starting later. Note
2577 that higher-level subexpressions thus take priority over their
2578 lower-level component subexpressions.
2582 Match lengths are measured in characters, not collating
2583 elements. A null string is considered longer than no match at
2584 all. For example, <literal>bb*</literal> matches the three middle
2585 characters of <literal>abbbc</literal>,
2586 <literal>(wee|week)(knights|nights)</literal> matches all ten
2587 characters of <literal>weeknights</literal>, when
2588 <literal>(.*).*</literal> is matched against
2589 <literal>abc</literal> the parenthesized subexpression matches all
2590 three characters, and when <literal>(a*)*</literal> is matched
2591 against <literal>bc</literal> both the whole RE and the
2592 parenthesized subexpression match the null string.
2596 If case-independent matching is specified, the effect is much as
2597 if all case distinctions had vanished from the alphabet. When an
2598 alphabetic that exists in multiple cases appears as an ordinary
2599 character outside a bracket expression, it is effectively
2600 transformed into a bracket expression containing both cases,
2601 e.g. <literal>x</literal> becomes <literal>[xX]</literal>. When
2602 it appears inside a bracket expression, all case counterparts of
2603 it are added to the bracket expression, so that (e.g.)
2604 <literal>[x]</literal> becomes <literal>[xX]</literal> and
2605 <literal>[^x]</literal> becomes <literal>[^xX]</literal>.
2609 There is no particular limit on the length of REs, except insofar
2610 as memory is limited. Memory usage is approximately linear in RE
2611 size, and largely insensitive to RE complexity, except for bounded
2612 repetitions. Bounded repetitions are implemented by macro
2613 expansion, which is costly in time and space if counts are large
2614 or bounded repetitions are nested. An RE like, say,
2615 <literal>((((a{1,100}){1,100}){1,100}){1,100}){1,100}</literal>
2616 will (eventually) run almost any existing machine out of swap
2620 This was written in 1994, mind you. The
2621 numbers have probably changed, but the problem
2626 <!-- end re_format.7 man page -->
2632 <sect1 id="functions-formatting">
2633 <title>Data Type Formatting Functions</title>
2635 <indexterm zone="functions-formatting">
2636 <primary>formatting</primary>
2640 <title>Author</title>
2642 Written by Karel Zak (<email>zakkr@zf.jcu.cz</email>) on 2000-01-24
2647 The <productname>PostgreSQL</productname> formatting functions
2648 provide a powerful set of tools for converting various data types
2649 (date/time, integer, floating point, numeric) to formatted strings
2650 and for converting from formatted strings to specific data types.
2651 These functions all follow a common calling convention: the first
2652 argument is the value to be formatted and the second argument is a
2653 template that defines the output or input format.
2656 <table tocentry="1">
2657 <title>Formatting Functions</title>
2661 <entry>Function</entry>
2662 <entry>Returns</entry>
2663 <entry>Description</entry>
2664 <entry>Example</entry>
2669 <entry><function>to_char</function>(<type>timestamp</type>, <type>text</type>)</entry>
2670 <entry><type>text</type></entry>
2671 <entry>convert time stamp to string</entry>
2672 <entry><literal>to_char(timestamp 'now','HH12:MI:SS')</literal></entry>
2675 <entry><function>to_char</function>(<type>interval</type>, <type>text</type>)</entry>
2676 <entry><type>text</type></entry>
2677 <entry>convert interval to string</entry>
2678 <entry><literal>to_char(interval '15h 2m 12s','HH24:MI:SS')</literal></entry>
2681 <entry><function>to_char</function>(<type>int</type>, <type>text</type>)</entry>
2682 <entry><type>text</type></entry>
2683 <entry>convert int4/int8 to string</entry>
2684 <entry><literal>to_char(125, '999')</literal></entry>
2687 <entry><function>to_char</function>(<type>double precision</type>,
2688 <type>text</type>)</entry>
2689 <entry><type>text</type></entry>
2690 <entry>convert real/double precision to string</entry>
2691 <entry><literal>to_char(125.8, '999D9')</literal></entry>
2694 <entry><function>to_char</function>(<type>numeric</type>, <type>text</type>)</entry>
2695 <entry><type>text</type></entry>
2696 <entry>convert numeric to string</entry>
2697 <entry><literal>to_char(numeric '-125.8', '999D99S')</literal></entry>
2700 <entry><function>to_date</function>(<type>text</type>, <type>text</type>)</entry>
2701 <entry><type>date</type></entry>
2702 <entry>convert string to date</entry>
2703 <entry><literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
2706 <entry><function>to_timestamp</function>(<type>text</type>, <type>text</type>)</entry>
2707 <entry><type>timestamp</type></entry>
2708 <entry>convert string to time stamp</entry>
2709 <entry><literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
2712 <entry><function>to_number</function>(<type>text</type>, <type>text</type>)</entry>
2713 <entry><type>numeric</type></entry>
2714 <entry>convert string to numeric</entry>
2715 <entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
2722 In an output template string, there are certain patterns that are
2723 recognized and replaced with appropriately-formatted data from the value
2724 to be formatted. Any text that is not a template pattern is simply
2725 copied verbatim. Similarly, in an input template string, template patterns
2726 identify the parts of the input data string to be looked at and the
2727 values to be found there.
2730 <table tocentry="1">
2731 <title>Template patterns for date/time conversions</title>
2735 <entry>Pattern</entry>
2736 <entry>Description</entry>
2741 <entry><literal>HH</literal></entry>
2742 <entry>hour of day (01-12)</entry>
2745 <entry><literal>HH12</literal></entry>
2746 <entry>hour of day (01-12)</entry>
2749 <entry><literal>HH24</literal></entry>
2750 <entry>hour of day (00-23)</entry>
2753 <entry><literal>MI</literal></entry>
2754 <entry>minute (00-59)</entry>
2757 <entry><literal>SS</literal></entry>
2758 <entry>second (00-59)</entry>
2761 <entry><literal>MS</literal></entry>
2762 <entry>millisecond (000-999)</entry>
2765 <entry><literal>US</literal></entry>
2766 <entry>microsecond (000000-999999)</entry>
2769 <entry><literal>SSSS</literal></entry>
2770 <entry>seconds past midnight (0-86399)</entry>
2773 <entry><literal>AM</literal> or <literal>A.M.</literal> or
2774 <literal>PM</literal> or <literal>P.M.</literal></entry>
2775 <entry>meridian indicator (upper case)</entry>
2778 <entry><literal>am</literal> or <literal>a.m.</literal> or
2779 <literal>pm</literal> or <literal>p.m.</literal></entry>
2780 <entry>meridian indicator (lower case)</entry>
2783 <entry><literal>Y,YYY</literal></entry>
2784 <entry>year (4 and more digits) with comma</entry>
2787 <entry><literal>YYYY</literal></entry>
2788 <entry>year (4 and more digits)</entry>
2791 <entry><literal>YYY</literal></entry>
2792 <entry>last 3 digits of year</entry>
2795 <entry><literal>YY</literal></entry>
2796 <entry>last 2 digits of year</entry>
2799 <entry><literal>Y</literal></entry>
2800 <entry>last digit of year</entry>
2803 <entry><literal>BC</literal> or <literal>B.C.</literal> or
2804 <literal>AD</literal> or <literal>A.D.</literal></entry>
2805 <entry>era indicator (upper case)</entry>
2808 <entry><literal>bc</literal> or <literal>b.c.</literal> or
2809 <literal>ad</literal> or <literal>a.d.</literal></entry>
2810 <entry>era indicator (lower case)</entry>
2813 <entry><literal>MONTH</literal></entry>
2814 <entry>full upper case month name (blank-padded to 9 chars)</entry>
2817 <entry><literal>Month</literal></entry>
2818 <entry>full mixed case month name (blank-padded to 9 chars)</entry>
2821 <entry><literal>month</literal></entry>
2822 <entry>full lower case month name (blank-padded to 9 chars)</entry>
2825 <entry><literal>MON</literal></entry>
2826 <entry>abbreviated upper case month name (3 chars)</entry>
2829 <entry><literal>Mon</literal></entry>
2830 <entry>abbreviated mixed case month name (3 chars)</entry>
2833 <entry><literal>mon</literal></entry>
2834 <entry>abbreviated lower case month name (3 chars)</entry>
2837 <entry><literal>MM</literal></entry>
2838 <entry>month number (01-12)</entry>
2841 <entry><literal>DAY</literal></entry>
2842 <entry>full upper case day name (blank-padded to 9 chars)</entry>
2845 <entry><literal>Day</literal></entry>
2846 <entry>full mixed case day name (blank-padded to 9 chars)</entry>
2849 <entry><literal>day</literal></entry>
2850 <entry>full lower case day name (blank-padded to 9 chars)</entry>
2853 <entry><literal>DY</literal></entry>
2854 <entry>abbreviated upper case day name (3 chars)</entry>
2857 <entry><literal>Dy</literal></entry>
2858 <entry>abbreviated mixed case day name (3 chars)</entry>
2861 <entry><literal>dy</literal></entry>
2862 <entry>abbreviated lower case day name (3 chars)</entry>
2865 <entry><literal>DDD</literal></entry>
2866 <entry>day of year (001-366)</entry>
2869 <entry><literal>DD</literal></entry>
2870 <entry>day of month (01-31)</entry>
2873 <entry><literal>D</literal></entry>
2874 <entry>day of week (1-7; SUN=1)</entry>
2877 <entry><literal>W</literal></entry>
2878 <entry>week of month (1-5) where first week start on the first day of the month</entry>
2881 <entry><literal>WW</literal></entry>
2882 <entry>week number of year (1-53) where first week start on the first day of the year</entry>
2885 <entry><literal>IW</literal></entry>
2886 <entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
2889 <entry><literal>CC</literal></entry>
2890 <entry>century (2 digits)</entry>
2893 <entry><literal>J</literal></entry>
2894 <entry>Julian Day (days since January 1, 4712 BC)</entry>
2897 <entry><literal>Q</literal></entry>
2898 <entry>quarter</entry>
2901 <entry><literal>RM</literal></entry>
2902 <entry>month in Roman Numerals (I-XII; I=January) - upper case</entry>
2905 <entry><literal>rm</literal></entry>
2906 <entry>month in Roman Numerals (I-XII; I=January) - lower case</entry>
2909 <entry><literal>TZ</literal></entry>
2910 <entry>timezone name - upper case</entry>
2913 <entry><literal>tz</literal></entry>
2914 <entry>timezone name - lower case</entry>
2921 Certain modifiers may be applied to any template pattern to alter its
2922 behavior. For example, <quote><literal>FMMonth</literal></quote>
2923 is the <quote><literal>Month</literal></quote> pattern with the
2924 <quote><literal>FM</literal></quote> prefix.
2927 <table tocentry="1">
2928 <title>Template pattern modifiers for date/time conversions</title>
2932 <entry>Modifier</entry>
2933 <entry>Description</entry>
2934 <entry>Example</entry>
2939 <entry><literal>FM</literal> prefix</entry>
2940 <entry>fill mode (suppress padding blanks and zeroes)</entry>
2941 <entry><literal>FMMonth</literal></entry>
2944 <entry><literal>TH</literal> suffix</entry>
2945 <entry>add upper-case ordinal number suffix</entry>
2946 <entry><literal>DDTH</literal></entry>
2949 <entry><literal>th</literal> suffix</entry>
2950 <entry>add lower-case ordinal number suffix</entry>
2951 <entry><literal>DDth</literal></entry>
2954 <entry><literal>FX</literal> prefix</entry>
2955 <entry>Fixed format global option (see below)</entry>
2956 <entry><literal>FX Month DD Day</literal></entry>
2959 <entry><literal>SP</literal> suffix</entry>
2960 <entry>spell mode (not yet implemented)</entry>
2961 <entry><literal>DDSP</literal></entry>
2973 <literal>FM</literal> suppresses leading zeroes or trailing blanks
2974 that would otherwise be added to make the output of a pattern be
2981 <function>to_timestamp</function> and <function>to_date</function>
2982 skip multiple blank spaces in the input string if the <literal>FX</literal> option
2983 is not used. <literal>FX</literal> must be specified as the first item
2984 in the template; for example
2985 <literal>to_timestamp('2000 JUN','YYYY MON')</literal> is right, but
2986 <literal>to_timestamp('2000 JUN','FXYYYY MON')</literal> returns an error,
2987 because <function>to_timestamp</function> expects one blank space only.
2993 If a backslash (<quote><literal>\</literal></quote>) is desired
2994 in a string constant, a double backslash
2995 (<quote><literal>\\</literal></quote>) must be entered; for
2996 example <literal>'\\HH\\MI\\SS'</literal>. This is true for
2997 any string constant in <productname>PostgreSQL</productname>.
3003 Ordinary text is allowed in <function>to_char</function>
3004 templates and will be output literally. You can put a substring
3005 in double quotes to force it to be interpreted as literal text
3006 even if it contains pattern keywords. For example, in
3007 <literal>'"Hello Year: "YYYY'</literal>, the <literal>YYYY</literal>
3008 will be replaced by year data, but the single <literal>Y</literal>
3015 If you want to have a double quote in the output you must
3016 precede it with a backslash, for example <literal>'\\"YYYY
3017 Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
3023 <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
3024 <type>date</type> is restricted if you use a year with more than 4 digits. You must
3025 use some non-digit character or template after <literal>YYYY</literal>,
3026 otherwise the year is always interpreted as 4 digits. For example
3028 <literal>to_date('200001131', 'YYYYMMDD')</literal> will be
3029 interpreted as a 4-digit year; better is to use a non-digit
3030 separator after the year, like
3031 <literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
3032 <literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
3038 Millisecond <literal>MS</literal> and microsecond <literal>US</literal>
3039 values in a conversion from string to time stamp are used as part of the
3040 seconds after the decimal point. For example
3041 <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
3042 but 300, because the conversion counts it as 12 + 0.3.
3043 This means for the format <literal>SS:MS</literal>, the input values
3044 <literal>12:3</literal>, <literal>12:30</literal>, and <literal>12:300</literal> specify the
3045 same number of milliseconds. To get three milliseconds, one must use
3046 <literal>12:003</literal>, which the conversion counts as
3047 12 + 0.003 = 12.003 seconds.
3053 <literal>to_timestamp('15:12:02.020.001230','HH:MI:SS.MS.US')</literal>
3054 is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
3055 1230 microseconds = 2.021230 seconds.
3061 <table tocentry="1">
3062 <title>Template patterns for numeric conversions</title>
3066 <entry>Pattern</entry>
3067 <entry>Description</entry>
3072 <entry><literal>9</literal></entry>
3073 <entry>value with the specified number of digits</entry>
3076 <entry><literal>0</literal></entry>
3077 <entry>value with leading zeros</entry>
3080 <entry><literal>.</literal> (period)</entry>
3081 <entry>decimal point</entry>
3084 <entry><literal>,</literal> (comma)</entry>
3085 <entry>group (thousand) separator</entry>
3088 <entry><literal>PR</literal></entry>
3089 <entry>negative value in angle brackets</entry>
3092 <entry><literal>S</literal></entry>
3093 <entry>negative value with minus sign (uses locale)</entry>
3096 <entry><literal>L</literal></entry>
3097 <entry>currency symbol (uses locale)</entry>
3100 <entry><literal>D</literal></entry>
3101 <entry>decimal point (uses locale)</entry>
3104 <entry><literal>G</literal></entry>
3105 <entry>group separator (uses locale)</entry>
3108 <entry><literal>MI</literal></entry>
3109 <entry>minus sign in specified position (if number < 0)</entry>
3112 <entry><literal>PL</literal></entry>
3113 <entry>plus sign in specified position (if number > 0)</entry>
3116 <entry><literal>SG</literal></entry>
3117 <entry>plus/minus sign in specified position</entry>
3120 <entry><literal>RN</literal></entry>
3121 <entry>roman numeral (input between 1 and 3999)</entry>
3124 <entry><literal>TH</literal> or <literal>th</literal></entry>
3125 <entry>convert to ordinal number</entry>
3128 <entry><literal>V</literal></entry>
3129 <entry>shift <replaceable>n</replaceable> digits (see
3133 <entry><literal>EEEE</literal></entry>
3134 <entry>scientific notation (not implemented yet)</entry>
3146 A sign formatted using <literal>SG</literal>, <literal>PL</literal>, or
3147 <literal>MI</literal> is not an anchor in
3148 the number; for example,
3149 <literal>to_char(-12, 'S9999')</literal> produces <literal>' -12'</literal>,
3150 but <literal>to_char(-12, 'MI9999')</literal> produces <literal>'- 12'</literal>.
3151 The Oracle implementation does not allow the use of
3152 <literal>MI</literal> ahead of <literal>9</literal>, but rather
3153 requires that <literal>9</literal> precede
3154 <literal>MI</literal>.
3160 <literal>9</literal> specifies a value with the same number of
3161 digits as there are <literal>9</literal>s. If a digit is
3162 not available use blank space.
3168 <literal>TH</literal> does not convert values less than zero
3169 and does not convert decimal numbers.
3175 <literal>PL</literal>, <literal>SG</literal>, and
3176 <literal>TH</literal> are <productname>PostgreSQL</productname>
3183 <literal>V</literal> effectively
3184 multiplies the input values by
3185 <literal>10^<replaceable>n</replaceable></literal>, where
3186 <replaceable>n</replaceable> is the number of digits following
3187 <literal>V</literal>.
3188 <function>to_char</function> does not support the use of
3189 <literal>V</literal> combined with a decimal point.
3190 (E.g., <literal>99.9V99</literal> is not allowed.)
3196 <table tocentry="1">
3197 <title><function>to_char</function> Examples</title>
3201 <entry>Input</entry>
3202 <entry>Output</entry>
3207 <entry><literal>to_char(now(),'Day, DD HH12:MI:SS')</literal></entry>
3208 <entry><literal>'Tuesday , 06 05:39:18'</literal></entry>
3211 <entry><literal>to_char(now(),'FMDay, FMDD HH12:MI:SS')</literal></entry>
3212 <entry><literal>'Tuesday, 6 05:39:18'</literal></entry>
3215 <entry><literal>to_char(-0.1,'99.99')</literal></entry>
3216 <entry><literal>' -.10'</literal></entry>
3219 <entry><literal>to_char(-0.1,'FM9.99')</literal></entry>
3220 <entry><literal>'-.1'</literal></entry>
3223 <entry><literal>to_char(0.1,'0.9')</literal></entry>
3224 <entry><literal>' 0.1'</literal></entry>
3227 <entry><literal>to_char(12,'9990999.9')</literal></entry>
3228 <entry><literal>' 0012.0'</literal></entry>
3231 <entry><literal>to_char(12,'FM9990999.9')</literal></entry>
3232 <entry><literal>'0012'</literal></entry>
3235 <entry><literal>to_char(485,'999')</literal></entry>
3236 <entry><literal>' 485'</literal></entry>
3239 <entry><literal>to_char(-485,'999')</literal></entry>
3240 <entry><literal>'-485'</literal></entry>
3243 <entry><literal>to_char(485,'9 9 9')</literal></entry>
3244 <entry><literal>' 4 8 5'</literal></entry>
3247 <entry><literal>to_char(1485,'9,999')</literal></entry>
3248 <entry><literal>' 1,485'</literal></entry>
3251 <entry><literal>to_char(1485,'9G999')</literal></entry>
3252 <entry><literal>' 1 485'</literal></entry>
3255 <entry><literal>to_char(148.5,'999.999')</literal></entry>
3256 <entry><literal>' 148.500'</literal></entry>
3259 <entry><literal>to_char(148.5,'999D999')</literal></entry>
3260 <entry><literal>' 148,500'</literal></entry>
3263 <entry><literal>to_char(3148.5,'9G999D999')</literal></entry>
3264 <entry><literal>' 3 148,500'</literal></entry>
3267 <entry><literal>to_char(-485,'999S')</literal></entry>
3268 <entry><literal>'485-'</literal></entry>
3271 <entry><literal>to_char(-485,'999MI')</literal></entry>
3272 <entry><literal>'485-'</literal></entry>
3275 <entry><literal>to_char(485,'999MI')</literal></entry>
3276 <entry><literal>'485'</literal></entry>
3279 <entry><literal>to_char(485,'PL999')</literal></entry>
3280 <entry><literal>'+485'</literal></entry>
3283 <entry><literal>to_char(485,'SG999')</literal></entry>
3284 <entry><literal>'+485'</literal></entry>
3287 <entry><literal>to_char(-485,'SG999')</literal></entry>
3288 <entry><literal>'-485'</literal></entry>
3291 <entry><literal>to_char(-485,'9SG99')</literal></entry>
3292 <entry><literal>'4-85'</literal></entry>
3295 <entry><literal>to_char(-485,'999PR')</literal></entry>
3296 <entry><literal>'<485>'</literal></entry>
3299 <entry><literal>to_char(485,'L999')</literal></entry>
3300 <entry><literal>'DM 485</literal></entry>
3303 <entry><literal>to_char(485,'RN')</literal></entry>
3304 <entry><literal>' CDLXXXV'</literal></entry>
3307 <entry><literal>to_char(485,'FMRN')</literal></entry>
3308 <entry><literal>'CDLXXXV'</literal></entry>
3311 <entry><literal>to_char(5.2,'FMRN')</literal></entry>
3312 <entry><literal>V</literal></entry>
3315 <entry><literal>to_char(482,'999th')</literal></entry>
3316 <entry><literal>' 482nd'</literal></entry>
3319 <entry><literal>to_char(485, '"Good number:"999')</literal></entry>
3320 <entry><literal>'Good number: 485'</literal></entry>
3323 <entry><literal>to_char(485.8,'"Pre:"999" Post:" .999')</literal></entry>
3324 <entry><literal>'Pre: 485 Post: .800'</literal></entry>
3327 <entry><literal>to_char(12,'99V999')</literal></entry>
3328 <entry><literal>' 12000'</literal></entry>
3331 <entry><literal>to_char(12.4,'99V999')</literal></entry>
3332 <entry><literal>' 12400'</literal></entry>
3335 <entry><literal>to_char(12.45, '99V9')</literal></entry>
3336 <entry><literal>' 125'</literal></entry>
3345 <sect1 id="functions-datetime">
3346 <title>Date/Time Functions and Operators</title>
3349 <xref linkend="functions-datetime-table"> shows the available
3350 functions for date/time value processing.
3351 <xref linkend="operators-datetime-table"> illustrates the
3352 behaviors of the basic arithmetic
3353 operators (<literal>+</literal>, <literal>*</literal>, etc.).
3354 For formatting functions, refer to <xref
3355 linkend="functions-formatting">. You should be familiar with the
3356 background information on date/time data types (see <xref
3357 linkend="datatype-datetime">).
3361 The date/time operators described below behave similarly for types
3362 involving time zones as well as those without.
3364 <table id="operators-datetime-table">
3365 <title>Date/Time Operators</title>
3371 <entry>Example</entry>
3372 <entry>Result</entry>
3378 <entry> <literal>+</literal> </entry>
3379 <entry><type>timestamp</type> '2001-09-28 01:00' + <type>interval</type> '23 hours'</entry>
3380 <entry><type>timestamp</type> '2001-09-29 00:00'</entry>
3384 <entry> <literal>+</literal> </entry>
3385 <entry><type>date</type> '2001-09-28' + <type>interval</type> '1 hour'</entry>
3386 <entry><type>timestamp</type> '2001-09-28 01:00'</entry>
3390 <entry> <literal>+</literal> </entry>
3391 <entry><type>time</type> '01:00' + <type>interval</type> '3 hours'</entry>
3392 <entry><type>time</type> '04:00'</entry>
3396 <entry> <literal>-</literal> </entry>
3397 <entry><type>timestamp</type> '2001-09-28 23:00' - <type>interval</type> '23 hours'</entry>
3398 <entry><type>timestamp</type> '2001-09-28'</entry>
3402 <entry> <literal>-</literal> </entry>
3403 <entry><type>date</type> '2001-09-28' - <type>interval</type> '1 hour'</entry>
3404 <entry><type>timestamp</type> '2001-09-27 23:00'</entry>
3408 <entry> <literal>-</literal> </entry>
3409 <entry><type>time</type> '05:00' - <type>interval</type> '2 hours'</entry>
3410 <entry><type>time</type> '03:00'</entry>
3414 <entry> <literal>-</literal> </entry>
3415 <entry><type>interval</type> '2 hours' - <type>time</type> '05:00'</entry>
3416 <entry><type>time</type> '03:00:00'</entry>
3420 <entry> <literal>*</literal> </entry>
3421 <entry><type>interval</type> '1 hour' * <type>int</type> '3'</entry>
3422 <entry><type>interval</type> '03:00'</entry>
3426 <entry> <literal>/</literal> </entry>
3427 <entry><type>interval</type> '1 hour' / <type>int</type> '3'</entry>
3428 <entry><type>interval</type> '00:20'</entry>
3436 The date/time functions are summarized below, with additional
3437 details in subsequent sections.
3439 <table id="functions-datetime-table">
3440 <title>Date/Time Functions</title>
3445 <entry>Return Type</entry>
3446 <entry>Description</entry>
3447 <entry>Example</entry>
3448 <entry>Result</entry>
3454 <entry><function>age</function>(<type>timestamp</type>)</entry>
3455 <entry><type>interval</type></entry>
3456 <entry>Subtract from today</entry>
3457 <entry><literal>age(timestamp '1957-06-13')</literal></entry>
3458 <entry><literal>43 years 8 mons 3 days</literal></entry>
3462 <entry><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</entry>
3463 <entry><type>interval</type></entry>
3464 <entry>Subtract arguments</entry>
3465 <entry><literal>age('2001-04-10', timestamp '1957-06-13')</literal></entry>
3466 <entry><literal>43 years 9 mons 27 days</literal></entry>
3470 <entry><function>current_date</function></entry>
3471 <entry><type>date</type></entry>
3472 <entry>Today's date; see <link linkend="functions-datetime-current">below</link>
3479 <entry><function>current_time</function></entry>
3480 <entry><type>time with time zone</type></entry>
3481 <entry>Time of day; see <link linkend="functions-datetime-current">below</link>
3488 <entry><function>current_timestamp</function></entry>
3489 <entry><type>timestamp with time zone</type></entry>
3490 <entry>Date and time; see <link linkend="functions-datetime-current">below</link>
3497 <entry><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</entry>
3498 <entry><type>double precision</type></entry>
3499 <entry>Get subfield (equivalent to
3500 <function>extract</function>); see also <link
3501 linkend="functions-datetime-datepart">below</link>
3503 <entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
3504 <entry><literal>20</literal></entry>
3508 <entry><function>date_part</function>(<type>text</type>, <type>interval</type>)</entry>
3509 <entry><type>double precision</type></entry>
3510 <entry>Get subfield (equivalent to
3511 <function>extract</function>); see also <link
3512 linkend="functions-datetime-datepart">below</link>
3514 <entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
3515 <entry><literal>3</literal></entry>
3519 <entry><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</entry>
3520 <entry><type>timestamp</type></entry>
3521 <entry>Truncate to specified precision; see also <link
3522 linkend="functions-datetime-trunc">below</link>
3524 <entry><literal>date_trunc('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
3525 <entry><literal>2001-02-16 20:00:00+00</literal></entry>
3529 <entry><function>extract</function>(<parameter>field</parameter> from
3530 <type>timestamp</type>)</entry>
3531 <entry><type>double precision</type></entry>
3532 <entry>Get subfield; see also <link
3533 linkend="functions-datetime-extract">below</link>
3535 <entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
3536 <entry><literal>20</literal></entry>
3540 <entry><function>extract</function>(<parameter>field</parameter> from
3541 <type>interval</type>)</entry>
3542 <entry><type>double precision</type></entry>
3543 <entry>Get subfield; see also <link
3544 linkend="functions-datetime-extract">below</link>
3546 <entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
3547 <entry><literal>3</literal></entry>
3551 <entry><function>isfinite</function>(<type>timestamp</type>)</entry>
3552 <entry><type>boolean</type></entry>
3553 <entry>Test for finite time stamp (neither invalid nor infinity)</entry>
3554 <entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
3555 <entry><literal>true</literal></entry>
3559 <entry><function>isfinite</function>(<type>interval</type>)</entry>
3560 <entry><type>boolean</type></entry>
3561 <entry>Test for finite interval</entry>
3562 <entry><literal>isfinite(interval '4 hours')</literal></entry>
3563 <entry><literal>true</literal></entry>
3567 <entry><function>localtime</function></entry>
3568 <entry><type>time</type></entry>
3569 <entry>Time of day; see <link linkend="functions-datetime-current">below</link>
3576 <entry><function>localtimestamp</function></entry>
3577 <entry><type>timestamp</type></entry>
3578 <entry>Date and time; see <link linkend="functions-datetime-current">below</link>
3585 <entry><function>now</function>()</entry>
3586 <entry><type>timestamp</type></entry>
3587 <entry>Current date and time (equivalent to
3588 <function>current_timestamp</function>); see <link
3589 linkend="functions-datetime-current">below</link>
3596 <entry><function>timeofday()</function></entry>
3597 <entry><type>text</type></entry>
3598 <entry>Current date and time; see <link
3599 linkend="functions-datetime-current">below</link>
3601 <entry><literal>timeofday()</literal></entry>
3602 <entry><literal>Wed Feb 21 17:01:13.000126 2001 EST</literal></entry>
3610 <sect2 id="functions-datetime-extract">
3611 <title><function>EXTRACT</function>, <function>date_part</function></title>
3614 EXTRACT (<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
3618 The <function>extract</function> function retrieves sub-fields
3619 from date/time values, such as year or hour.
3620 <replaceable>source</replaceable> is a value expression that
3621 evaluates to type <type>timestamp</type> or <type>interval</type>.
3622 (Expressions of type <type>date</type> or <type>time</type> will
3623 be cast to <type>timestamp</type> and can therefore be used as
3624 well.) <replaceable>field</replaceable> is an identifier or
3625 string that selects what field to extract from the source value.
3626 The <function>extract</function> function returns values of type
3627 <type>double precision</type>.
3628 The following are valid values:
3630 <!-- alphabetical -->
3633 <term><literal>century</literal></term>
3636 The year field divided by 100
3641 SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
3642 <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
3647 Note that the result for the century field is simply the year field
3648 divided by 100, and not the conventional definition which puts most
3649 years in the 1900's in the twentieth century.
3655 <term><literal>day</literal></term>
3658 The day (of the month) field (1 - 31)
3663 SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
3664 <lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
3671 <term><literal>decade</literal></term>
3674 The year field divided by 10
3679 SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
3680 <lineannotation>Result: </lineannotation><computeroutput>200</computeroutput>
3687 <term><literal>dow</literal></term>
3690 The day of the week (0 - 6; Sunday is 0) (for
3691 <type>timestamp</type> values only)
3696 SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
3697 <lineannotation>Result: </lineannotation><computeroutput>5</computeroutput>
3704 <term><literal>doy</literal></term>
3707 The day of the year (1 - 365/366) (for <type>timestamp</type> values only)
3711 SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
3712 <lineannotation>Result: </lineannotation><computeroutput>47</computeroutput>
3719 <term><literal>epoch</literal></term>
3722 For <type>date</type> and <type>timestamp</type> values, the
3723 number of seconds since 1970-01-01 00:00:00-00 (Result may be
3724 negative.); for <type>interval</type> values, the total number
3725 of seconds in the interval
3730 SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40');
3731 <lineannotation>Result: </lineannotation><computeroutput>982352320</computeroutput>
3733 SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
3734 <lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
3741 <term><literal>hour</literal></term>
3744 The hour field (0 - 23)
3749 SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
3750 <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
3757 <term><literal>microseconds</literal></term>
3760 The seconds field, including fractional parts, multiplied by 1
3761 000 000. Note that this includes full seconds.
3766 SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
3767 <lineannotation>Result: </lineannotation><computeroutput>28500000</computeroutput>
3774 <term><literal>millennium</literal></term>
3777 The year field divided by 1000
3782 SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
3783 <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
3788 Note that the result for the millennium field is simply the year field
3789 divided by 1000, and not the conventional definition which puts
3790 years in the 1900's in the second millennium.
3796 <term><literal>milliseconds</literal></term>
3799 The seconds field, including fractional parts, multiplied by
3800 1000. Note that this includes full seconds.
3805 SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
3806 <lineannotation>Result: </lineannotation><computeroutput>28500</computeroutput>
3813 <term><literal>minute</literal></term>
3816 The minutes field (0 - 59)
3821 SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
3822 <lineannotation>Result: </lineannotation><computeroutput>38</computeroutput>
3829 <term><literal>month</literal></term>
3832 For <type>timestamp</type> values, the number of the month
3833 within the year (1 - 12) ; for <type>interval</type> values
3834 the number of months, modulo 12 (0 - 11)
3839 SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
3840 <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
3842 SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
3843 <lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
3845 SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
3846 <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
3853 <term><literal>quarter</literal></term>
3856 The quarter of the year (1 - 4) that the day is in (for
3857 <type>timestamp</type> values only)
3862 SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
3863 <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
3870 <term><literal>second</literal></term>
3873 The seconds field, including fractional parts (0 -
3874 59<footnote><simpara>60 if leap seconds are
3875 implemented by the operating system</simpara></footnote>)
3880 SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
3881 <lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
3883 SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
3884 <lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
3891 <term><literal>timezone</literal></term>
3894 The time zone offset. XXX But in what units?
3901 <term><literal>timezone_hour</literal></term>
3904 The hour component of the time zone offset.
3910 <term><literal>timezone_minute</literal></term>
3913 The minute component of the time zone offset.
3919 <term><literal>week</literal></term>
3922 From a <type>timestamp</type> value, calculate the number of
3923 the week of the year that the day is in. By definition
3924 (<acronym>ISO</acronym> 8601), the first week of a year
3925 contains January 4 of that year. (The <acronym>ISO</acronym>
3926 week starts on Monday.) In other words, the first Thursday of
3927 a year is in week 1 of that year.
3932 SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
3933 <lineannotation>Result: </lineannotation><computeroutput>7</computeroutput>
3940 <term><literal>year</literal></term>
3948 SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
3949 <lineannotation>Result: </lineannotation><computeroutput>2001</computeroutput>
3960 The <function>extract</function> function is primarily intended
3961 for computational processing. For formatting date/time values for
3962 display, see <xref linkend="functions-formatting">.
3965 <anchor id="functions-datetime-datepart">
3967 The <function>date_part</function> function is modeled on the traditional
3968 <productname>Ingres</productname> equivalent to the
3969 <acronym>SQL</acronym>-function <function>extract</function>:
3971 date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
3973 Note that here the <replaceable>field</replaceable> value needs to
3974 be a string. The valid field values for
3975 <function>date_part</function> are the same as for
3976 <function>extract</function>.
3981 SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
3982 <lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
3984 SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
3985 <lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
3991 <sect2 id="functions-datetime-trunc">
3992 <title><function>date_trunc</function></title>
3995 The function <function>date_trunc</function> is conceptually
3996 similar to the <function>trunc</function> function for numbers.
4001 date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
4003 <replaceable>source</replaceable> is a value expression of type
4004 <type>timestamp</type> (values of type <type>date</type> and
4005 <type>time</type> are cast automatically).
4006 <replaceable>field</replaceable> selects to which precision to
4007 truncate the time stamp value. The return value is of type
4008 <type>timestamp</type> with all fields that are less than the
4009 selected one set to zero (or one, for day and month).
4013 Valid values for <replaceable>field</replaceable> are:
4015 <member>microseconds</member>
4016 <member>milliseconds</member>
4017 <member>second</member>
4018 <member>minute</member>
4019 <member>hour</member>
4020 <member>day</member>
4021 <member>month</member>
4022 <member>year</member>
4023 <member>decade</member>
4024 <member>century</member>
4025 <member>millennium</member>
4032 SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
4033 <lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00+00</computeroutput>
4035 SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
4036 <lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00+00</computeroutput>
4042 <sect2 id="functions-datetime-current">
4043 <title>Current Date/Time</title>
4046 <primary>date</primary>
4047 <secondary>current</secondary>
4051 <primary>time</primary>
4052 <secondary>current</secondary>
4056 The following functions are available to obtain the current date and/or
4062 CURRENT_TIME ( <replaceable>precision</replaceable> )
4063 CURRENT_TIMESTAMP ( <replaceable>precision</replaceable> )
4066 LOCALTIME ( <replaceable>precision</replaceable> )
4067 LOCALTIMESTAMP ( <replaceable>precision</replaceable> )
4069 <function>CURRENT_TIME</function>,
4070 <function>CURRENT_TIMESTAMP</function>,
4071 <function>LOCALTIME</function>, and
4072 <function>LOCALTIMESTAMP</function>
4073 can optionally be given
4074 a precision parameter, which causes the result to be rounded
4075 to that many fractional digits. Without a precision parameter,
4076 the result is given to the full available precision.
4081 Prior to <productname>PostgreSQL</productname> 7.2, the precision
4082 parameters were unimplemented, and the result was always given
4089 SELECT CURRENT_TIME;
4090 <computeroutput>14:39:53.662522-05</computeroutput>
4092 SELECT CURRENT_DATE;
4093 <computeroutput>2001-12-23</computeroutput>
4095 SELECT CURRENT_TIMESTAMP;
4096 <computeroutput>2001-12-23 14:39:53.662522-05</computeroutput>
4098 SELECT CURRENT_TIMESTAMP(2);
4099 <computeroutput>2001-12-23 14:39:53.66-05</computeroutput>
4101 SELECT LOCALTIMESTAMP;
4102 <computeroutput>2001-12-23 14:39:53.662522</computeroutput>
4107 The function <function>now()</function> is the traditional
4108 <productname>PostgreSQL</productname> equivalent to
4109 <function>CURRENT_TIMESTAMP</function>.
4113 There is also <function>timeofday()</function>, which for historical
4114 reasons returns a text string rather than a <type>timestamp</type> value:
4120 Sat Feb 17 19:07:32.000126 2001 EST
4125 It is quite important to realize that
4126 <function>CURRENT_TIMESTAMP</function> and related functions all return
4127 the time as of the start of the current transaction; their values do not
4128 increment while a transaction is running. But
4129 <function>timeofday()</function> returns the actual current time.
4133 All the date/time data types also accept the special literal value
4134 <literal>now</literal> to specify the current date and time. Thus,
4135 the following three all return the same result:
4137 SELECT CURRENT_TIMESTAMP;
4139 SELECT TIMESTAMP 'now';
4143 You do not want to use the third form when specifying a DEFAULT
4144 value while creating a table. The system will convert <literal>now</literal>
4145 to a <type>timestamp</type> as soon as the constant is parsed, so that when
4146 the default value is needed,
4147 the time of the table creation would be used! The first two
4148 forms will not be evaluated until the default value is used,
4149 because they are function calls. Thus they will give the desired
4150 behavior of defaulting to the time of row insertion.
4158 <sect1 id="functions-geometry">
4159 <title>Geometric Functions and Operators</title>
4162 The geometric types <type>point</type>, <type>box</type>,
4163 <type>lseg</type>, <type>line</type>, <type>path</type>,
4164 <type>polygon</type>, and <type>circle</type> have a large set of
4165 native support functions and operators.
4169 <title>Geometric Operators</title>
4173 <entry>Operator</entry>
4174 <entry>Description</entry>
4175 <entry>Usage</entry>
4181 <entry>Translation</entry>
4182 <entry><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></entry>
4186 <entry>Translation</entry>
4187 <entry><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></entry>
4191 <entry>Scaling/rotation</entry>
4192 <entry><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></entry>
4196 <entry>Scaling/rotation</entry>
4197 <entry><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></entry>
4201 <entry>Intersection</entry>
4202 <entry><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></entry>
4206 <entry>Number of points in path or polygon</entry>
4207 <entry><literal># '((1,0),(0,1),(-1,0))'</literal></entry>
4211 <entry>Point of closest proximity</entry>
4212 <entry><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></entry>
4215 <entry> && </entry>
4216 <entry>Overlaps?</entry>
4217 <entry><literal>box '((0,0),(1,1))' && box '((0,0),(2,2))'</literal></entry>
4220 <entry> &< </entry>
4221 <entry>Overlaps to left?</entry>
4222 <entry><literal>box '((0,0),(1,1))' &< box '((0,0),(2,2))'</literal></entry>
4225 <entry> &> </entry>
4226 <entry>Overlaps to right?</entry>
4227 <entry><literal>box '((0,0),(3,3))' &> box '((0,0),(2,2))'</literal></entry>
4230 <entry> <-> </entry>
4231 <entry>Distance between</entry>
4232 <entry><literal>circle '((0,0),1)' <-> circle '((5,0),1)'</literal></entry>
4235 <entry> << </entry>
4236 <entry>Left of?</entry>
4237 <entry><literal>circle '((0,0),1)' << circle '((5,0),1)'</literal></entry>
4240 <entry> <^ </entry>
4241 <entry>Is below?</entry>
4242 <entry><literal>circle '((0,0),1)' <^ circle '((0,5),1)'</literal></entry>
4245 <entry> >> </entry>
4246 <entry>Is right of?</entry>
4247 <entry><literal>circle '((5,0),1)' >> circle '((0,0),1)'</literal></entry>
4250 <entry> >^ </entry>
4251 <entry>Is above?</entry>
4252 <entry><literal>circle '((0,5),1)' >^ circle '((0,0),1)'</literal></entry>
4256 <entry>Intersects or overlaps</entry>
4257 <entry><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></entry>
4261 <entry>Is horizontal?</entry>
4262 <entry><literal>point '(1,0)' ?- point '(0,0)'</literal></entry>
4265 <entry> ?-| </entry>
4266 <entry>Is perpendicular?</entry>
4267 <entry><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></entry>
4270 <entry> @-@ </entry>
4271 <entry>Length or circumference</entry>
4272 <entry><literal>@-@ path '((0,0),(1,0))'</literal></entry>
4276 <entry>Is vertical?</entry>
4277 <entry><literal>point '(0,1)' ?| point '(0,0)'</literal></entry>
4280 <entry> ?|| </entry>
4281 <entry>Is parallel?</entry>
4282 <entry><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></entry>
4286 <entry>Contained or on</entry>
4287 <entry><literal>point '(1,1)' @ circle '((0,0),2)'</literal></entry>
4291 <entry>Center of</entry>
4292 <entry><literal>@@ circle '((0,0),10)'</literal></entry>
4296 <entry>Same as</entry>
4297 <entry><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></entry>
4304 <title>Geometric Functions</title>
4308 <entry>Function</entry>
4309 <entry>Returns</entry>
4310 <entry>Description</entry>
4311 <entry>Example</entry>
4316 <entry><function>area</function>(object)</entry>
4317 <entry><type>double precision</type></entry>
4318 <entry>area of item</entry>
4319 <entry><literal>area(box '((0,0),(1,1))')</literal></entry>
4322 <entry><function>box</function>(box, box)</entry>
4323 <entry><type>box</type></entry>
4324 <entry>intersection box</entry>
4325 <entry><literal>box(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
4328 <entry><function>center</function>(object)</entry>
4329 <entry><type>point</type></entry>
4330 <entry>center of item</entry>
4331 <entry><literal>center(box '((0,0),(1,2))')</literal></entry>
4334 <entry><function>diameter</function>(circle)</entry>
4335 <entry><type>double precision</type></entry>
4336 <entry>diameter of circle</entry>
4337 <entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
4340 <entry><function>height</function>(box)</entry>
4341 <entry><type>double precision</type></entry>
4342 <entry>vertical size of box</entry>
4343 <entry><literal>height(box '((0,0),(1,1))')</literal></entry>
4346 <entry><function>isclosed</function>(path)</entry>
4347 <entry><type>boolean</type></entry>
4348 <entry>a closed path?</entry>
4349 <entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
4352 <entry><function>isopen</function>(path)</entry>
4353 <entry><type>boolean</type></entry>
4354 <entry>an open path?</entry>
4355 <entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
4358 <entry><function>length</function>(object)</entry>
4359 <entry><type>double precision</type></entry>
4360 <entry>length of item</entry>
4361 <entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
4364 <entry><function>npoints</function>(path)</entry>
4365 <entry><type>integer</type></entry>
4366 <entry>number of points</entry>
4367 <entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
4370 <entry><function>npoints</function>(polygon)</entry>
4371 <entry><type>integer</type></entry>
4372 <entry>number of points</entry>
4373 <entry><literal>npoints(polygon '((1,1),(0,0))')</literal></entry>
4376 <entry><function>pclose</function>(path)</entry>
4377 <entry><type>path</type></entry>
4378 <entry>convert path to closed</entry>
4379 <entry><literal>popen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
4382 <!-- Not defined by this name. Implements the intersection operator '#' -->
4384 <entry><function>point</function>(lseg,lseg)</entry>
4385 <entry><type>point</type></entry>
4386 <entry>intersection</entry>
4387 <entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
4391 <entry><function>popen</function>(path)</entry>
4392 <entry><type>path</type></entry>
4393 <entry>convert path to open path</entry>
4394 <entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
4397 <entry><function>radius</function>(circle)</entry>
4398 <entry><type>double precision</type></entry>
4399 <entry>radius of circle</entry>
4400 <entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
4403 <entry><function>width</function>(box)</entry>
4404 <entry><type>double precision</type></entry>
4405 <entry>horizontal size</entry>
4406 <entry><literal>width(box '((0,0),(1,1))')</literal></entry>
4414 <title>Geometric Type Conversion Functions</title>
4418 <entry>Function</entry>
4419 <entry>Returns</entry>
4420 <entry>Description</entry>
4421 <entry>Example</entry>
4426 <entry><function>box</function>(<type>circle</type>)</entry>
4427 <entry><type>box</type></entry>
4428 <entry>circle to box</entry>
4429 <entry><literal>box(circle '((0,0),2.0)')</literal></entry>
4432 <entry><function>box</function>(<type>point</type>, <type>point</type>)</entry>
4433 <entry><type>box</type></entry>
4434 <entry>points to box</entry>
4435 <entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
4438 <entry><function>box</function>(<type>polygon</type>)</entry>
4439 <entry><type>box</type></entry>
4440 <entry>polygon to box</entry>
4441 <entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
4444 <entry><function>circle</function>(<type>box</type>)</entry>
4445 <entry><type>circle</type></entry>
4446 <entry>to circle</entry>
4447 <entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
4450 <entry><function>circle</function>(<type>point</type>, <type>double precision</type>)</entry>
4451 <entry><type>circle</type></entry>
4452 <entry>point to circle</entry>
4453 <entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
4456 <entry><function>lseg</function>(<type>box</type>)</entry>
4457 <entry><type>lseg</type></entry>
4458 <entry>box diagonal to <type>lseg</type></entry>
4459 <entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
4462 <entry><function>lseg</function>(<type>point</type>, <type>point</type>)</entry>
4463 <entry><type>lseg</type></entry>
4464 <entry>points to <type>lseg</type></entry>
4465 <entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
4468 <entry><function>path</function>(<type>polygon</type>)</entry>
4469 <entry><type>point</type></entry>
4470 <entry>polygon to path</entry>
4471 <entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
4474 <entry><function>point</function>(<type>circle</type>)</entry>
4475 <entry><type>point</type></entry>
4476 <entry>center</entry>
4477 <entry><literal>point(circle '((0,0),2.0)')</literal></entry>
4480 <entry><function>point</function>(<type>lseg</type>, <type>lseg</type>)</entry>
4481 <entry><type>point</type></entry>
4482 <entry>intersection</entry>
4483 <entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
4486 <entry><function>point</function>(<type>polygon</type>)</entry>
4487 <entry><type>point</type></entry>
4488 <entry>center</entry>
4489 <entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
4492 <entry><function>polygon</function>(<type>box</type>)</entry>
4493 <entry><type>polygon</type></entry>
4494 <entry>4-point polygon</entry>
4495 <entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
4498 <entry><function>polygon</function>(<type>circle</type>)</entry>
4499 <entry><type>polygon</type></entry>
4500 <entry>12-point polygon</entry>
4501 <entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
4504 <entry><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</entry>
4505 <entry><type>polygon</type></entry>
4506 <entry><replaceable class="parameter">npts</replaceable> polygon</entry>
4507 <entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
4510 <entry><function>polygon</function>(<type>path</type>)</entry>
4511 <entry><type>polygon</type></entry>
4512 <entry>path to polygon</entry>
4513 <entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
4520 It is possible to access the two component numbers of a <type>point</>
4521 as though it were an array with subscripts 0,1. For example, if
4522 <literal>t.p</> is a <type>point</> column then
4523 <literal>SELECT p[0] FROM t</> retrieves the X coordinate;
4524 <literal>UPDATE t SET p[1] = ...</> changes the Y coordinate.
4525 In the same way, a <type>box</> or an <type>lseg</> may be treated
4526 as an array of two <type>point</>s.
4532 <sect1 id="functions-net">
4533 <title>Network Address Type Functions</title>
4536 <table tocentry="1" id="cidr-inet-operators-table">
4537 <title><type>cidr</type> and <type>inet</type> Operators</title>
4541 <entry>Operator</entry>
4542 <entry>Description</entry>
4543 <entry>Usage</entry>
4548 <entry> < </entry>
4549 <entry>Less than</entry>
4550 <entry><literal>inet '192.168.1.5' < inet '192.168.1.6'</literal></entry>
4553 <entry> <= </entry>
4554 <entry>Less than or equal</entry>
4555 <entry><literal>inet '192.168.1.5' <= inet '192.168.1.5'</literal></entry>
4559 <entry>Equals</entry>
4560 <entry><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></entry>
4563 <entry> >= </entry>
4564 <entry>Greater or equal</entry>
4565 <entry><literal>inet '192.168.1.5' >= inet '192.168.1.5'</literal></entry>
4568 <entry> > </entry>
4569 <entry>Greater</entry>
4570 <entry><literal>inet '192.168.1.5' > inet '192.168.1.4'</literal></entry>
4573 <entry> <> </entry>
4574 <entry>Not equal</entry>
4575 <entry><literal>inet '192.168.1.5' <> inet '192.168.1.4'</literal></entry>
4578 <entry> << </entry>
4579 <entry>is contained within</entry>
4580 <entry><literal>inet '192.168.1.5' << inet '192.168.1/24'</literal></entry>
4583 <entry> <<= </entry>
4584 <entry>is contained within or equals</entry>
4585 <entry><literal>inet '192.168.1/24' <<= inet '192.168.1/24'</literal></entry>
4588 <entry> >> </entry>
4589 <entry>contains</entry>
4590 <entry><literal>inet'192.168.1/24' >> inet '192.168.1.5'</literal></entry>
4593 <entry> >>= </entry>
4594 <entry>contains or equals</entry>
4595 <entry><literal>inet '192.168.1/24' >>= inet '192.168.1/24'</literal></entry>
4602 All of the operators for <type>inet</type> can be applied to
4603 <type>cidr</type> values as well. The operators
4604 <literal><<</literal>, <literal><<=</literal>,
4605 <literal>>></literal>, <literal>>>=</literal>
4606 test for subnet inclusion: they consider only the network parts
4607 of the two addresses, ignoring any host part, and determine whether
4608 one network part is identical to or a subnet of the other.
4612 <table tocentry="1" id="cidr-inet-functions">
4613 <title><type>cidr</type> and <type>inet</type> Functions</title>
4617 <entry>Function</entry>
4618 <entry>Returns</entry>
4619 <entry>Description</entry>
4620 <entry>Example</entry>
4621 <entry>Result</entry>
4626 <entry><function>broadcast</function>(<type>inet</type>)</entry>
4627 <entry><type>inet</type></entry>
4628 <entry>broadcast address for network</entry>
4629 <entry><literal>broadcast('192.168.1.5/24')</literal></entry>
4630 <entry><literal>192.168.1.255/24</literal></entry>
4633 <entry><function>host</function>(<type>inet</type>)</entry>
4634 <entry><type>text</type></entry>
4635 <entry>extract IP address as text</entry>
4636 <entry><literal>host('192.168.1.5/24')</literal></entry>
4637 <entry><literal>192.168.1.5</literal></entry>
4640 <entry><function>masklen</function>(<type>inet</type>)</entry>
4641 <entry><type>integer</type></entry>
4642 <entry>extract netmask length</entry>
4643 <entry><literal>masklen('192.168.1.5/24')</literal></entry>
4644 <entry><literal>24</literal></entry>
4647 <entry><function>set_masklen</function>(<type>inet</type>,<type>integer</type>)</entry>
4648 <entry><type>inet</type></entry>
4649 <entry>set netmask length for <type>inet</type> value</entry>
4650 <entry><literal>set_masklen('192.168.1.5/24',16)</literal></entry>
4651 <entry><literal>192.168.1.5/16</literal></entry>
4654 <entry><function>netmask</function>(<type>inet</type>)</entry>
4655 <entry><type>inet</type></entry>
4656 <entry>construct netmask for network</entry>
4657 <entry><literal>netmask('192.168.1.5/24')</literal></entry>
4658 <entry><literal>255.255.255.0</literal></entry>
4661 <entry><function>network</function>(<type>inet</type>)</entry>
4662 <entry><type>cidr</type></entry>
4663 <entry>extract network part of address</entry>
4664 <entry><literal>network('192.168.1.5/24')</literal></entry>
4665 <entry><literal>192.168.1.0/24</literal></entry>
4668 <entry><function>text</function>(<type>inet</type>)</entry>
4669 <entry><type>text</type></entry>
4670 <entry>extract IP address and masklen as text</entry>
4671 <entry><literal>text(inet '192.168.1.5')</literal></entry>
4672 <entry><literal>192.168.1.5/32</literal></entry>
4675 <entry><function>abbrev</function>(<type>inet</type>)</entry>
4676 <entry><type>text</type></entry>
4677 <entry>extract abbreviated display as text</entry>
4678 <entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
4679 <entry><literal>10.1/16</literal></entry>
4686 All of the functions for <type>inet</type> can be applied to
4687 <type>cidr</type> values as well. The <function>host</function>(),
4688 <function>text</function>(), and <function>abbrev</function>() functions are primarily
4689 intended to offer alternative display formats. You can cast a text
4690 field to inet using normal casting syntax: <literal>inet(expression)</literal> or
4691 <literal>colname::inet</literal>.
4694 <table tocentry="1" id="macaddr-functions">
4695 <title><type>macaddr</type> Functions</title>
4699 <entry>Function</entry>
4700 <entry>Returns</entry>
4701 <entry>Description</entry>
4702 <entry>Example</entry>
4703 <entry>Result</entry>
4708 <entry><function>trunc</function>(<type>macaddr</type>)</entry>
4709 <entry><type>macaddr</type></entry>
4710 <entry>set last 3 bytes to zero</entry>
4711 <entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
4712 <entry><literal>12:34:56:00:00:00</literal></entry>
4719 The function <function>trunc</function>(<type>macaddr</type>) returns a MAC
4720 address with the last 3 bytes set to 0. This can be used to
4721 associate the remaining prefix with a manufacturer. The directory
4722 <filename>contrib/mac</filename> in the source distribution contains some
4723 utilities to create and maintain such an association table.
4727 The <type>macaddr</type> type also supports the standard relational
4728 operators (<literal>></literal>, <literal><=</literal>, etc.) for
4729 lexicographical ordering.
4735 <sect1 id="functions-sequence">
4736 <title>Sequence-Manipulation Functions</title>
4739 <primary>sequences</primary>
4742 <primary>nextval</primary>
4745 <primary>currval</primary>
4748 <primary>setval</primary>
4752 <title>Sequence Functions</title>
4755 <row><entry>Function</entry> <entry>Returns</entry> <entry>Description</entry></row>
4760 <entry><function>nextval</function>(<type>text</type>)</entry>
4761 <entry><type>bigint</type></entry>
4762 <entry>Advance sequence and return new value</entry>
4765 <entry><function>currval</function>(<type>text</type>)</entry>
4766 <entry><type>bigint</type></entry>
4767 <entry>Return value most recently obtained with <function>nextval</function></entry>
4770 <entry><function>setval</function>(<type>text</type>,<type>bigint</type>)</entry>
4771 <entry><type>bigint</type></entry>
4772 <entry>Set sequence's current value</entry>
4775 <entry><function>setval</function>(<type>text</type>,<type>bigint</type>,<type>boolean</type>)</entry>
4776 <entry><type>bigint</type></entry>
4777 <entry>Set sequence's current value and <literal>is_called</literal> flag</entry>
4784 This section describes <productname>PostgreSQL</productname>'s functions
4785 for operating on <firstterm>sequence objects</firstterm>.
4786 Sequence objects (also called sequence generators or
4787 just sequences) are special single-row tables created with
4788 <command>CREATE SEQUENCE</command>. A sequence object is usually used to
4789 generate unique identifiers for rows of a table. The sequence functions
4790 provide simple, multiuser-safe methods for obtaining successive
4791 sequence values from sequence objects.
4795 For largely historical reasons, the sequence to be operated on by
4796 a sequence-function call is specified by a text-string argument.
4797 To achieve some compatibility with the handling of ordinary SQL
4798 names, the sequence functions convert their argument to lower case
4799 unless the string is double-quoted. Thus
4801 nextval('foo') <lineannotation>operates on sequence </><literal>foo</literal>
4802 nextval('FOO') <lineannotation>operates on sequence </><literal>foo</literal>
4803 nextval('"Foo"') <lineannotation>operates on sequence </><literal>Foo</literal>
4805 The sequence name can be schema-qualified if necessary:
4807 nextval('myschema.foo') <lineannotation>operates on </><literal>myschema.foo</literal>
4808 nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
4809 nextval('foo') <lineannotation>searches search path for
4810 </><literal>foo</literal>
4812 Of course, the text argument can be the result of an expression,
4813 not only a simple literal, which is occasionally useful.
4817 The available sequence functions are:
4821 <term><function>nextval</function></term>
4824 Advance the sequence object to its next value and return that
4825 value. This is done atomically: even if multiple server processes
4826 execute <function>nextval</function> concurrently, each will safely receive
4827 a distinct sequence value.
4833 <term><function>currval</function></term>
4836 Return the value most recently obtained by <function>nextval</function>
4837 for this sequence in the current server process. (An error is
4838 reported if <function>nextval</function> has never been called for this
4839 sequence in this process.) Notice that because this is returning
4840 a process-local value, it gives a predictable answer even if other
4841 server processes are executing <function>nextval</function> meanwhile.
4847 <term><function>setval</function></term>
4850 Reset the sequence object's counter value. The two-parameter
4851 form sets the sequence's <literal>last_value</literal> field to the specified
4852 value and sets its <literal>is_called</literal> field to <literal>true</literal>,
4853 meaning that the next <function>nextval</function> will advance the sequence
4854 before returning a value. In the three-parameter form,
4855 <literal>is_called</literal> may be set either <literal>true</literal> or
4856 <literal>false</literal>. If it's set to <literal>false</literal>,
4857 the next <function>nextval</function> will return exactly the specified
4858 value, and sequence advancement commences with the following
4859 <function>nextval</function>. For example,
4864 SELECT setval('foo', 42); <lineannotation>Next nextval() will return 43</lineannotation>
4865 SELECT setval('foo', 42, true); <lineannotation>Same as above</lineannotation>
4866 SELECT setval('foo', 42, false); <lineannotation>Next nextval() will return 42</lineannotation>
4871 The result returned by <function>setval</function> is just the value of its
4881 To avoid blocking of concurrent transactions that obtain numbers from the
4882 same sequence, a <function>nextval</function> operation is never rolled back;
4883 that is, once a value has been fetched it is considered used, even if the
4884 transaction that did the <function>nextval</function> later aborts. This means
4885 that aborted transactions may leave unused <quote>holes</quote> in the
4886 sequence of assigned values. <function>setval</function> operations are never
4887 rolled back, either.
4892 If a sequence object has been created with default parameters,
4893 <function>nextval()</function> calls on it will return successive values
4894 beginning with one. Other behaviors can be obtained by using
4895 special parameters in the <command>CREATE SEQUENCE</command> command;
4896 see its command reference page for more information.
4902 <sect1 id="functions-conditional">
4903 <title>Conditional Expressions</title>
4906 <primary>case</primary>
4910 <primary>conditionals</primary>
4914 This section describes the <acronym>SQL</acronym>-compliant conditional expressions
4915 available in <productname>PostgreSQL</productname>.
4920 If your needs go beyond the capabilities of these conditional
4921 expressions you might want to consider writing a stored procedure
4922 in a more expressive programming language.
4926 <bridgehead renderas="sect2">CASE</bridgehead>
4929 CASE WHEN <replaceable>condition</replaceable> THEN <replaceable>result</replaceable>
4930 <optional>WHEN ...</optional>
4931 <optional>ELSE <replaceable>result</replaceable></optional>
4936 The <acronym>SQL</acronym> <token>CASE</token> expression is a
4937 generic conditional expression, similar to if/else statements in
4938 other languages. <token>CASE</token> clauses can be used wherever
4939 an expression is valid. <replaceable>condition</replaceable> is an
4940 expression that returns a <type>boolean</type> result. If the result is true
4941 then the value of the <token>CASE</token> expression is
4942 <replaceable>result</replaceable>. If the result is false any
4943 subsequent <token>WHEN</token> clauses are searched in the same
4944 manner. If no <token>WHEN</token>
4945 <replaceable>condition</replaceable> is true then the value of the
4946 case expression is the <replaceable>result</replaceable> in the
4947 <token>ELSE</token> clause. If the <token>ELSE</token> clause is
4948 omitted and no condition matches, the result is NULL.
4955 <prompt>=></prompt> <userinput>SELECT * FROM test;</userinput>
4964 <prompt>=></prompt> <userinput>SELECT a,
4965 CASE WHEN a=1 THEN 'one'
4969 FROM test;</userinput>
4982 The data types of all the <replaceable>result</replaceable>
4983 expressions must be coercible to a single output type.
4984 See <xref linkend="typeconv-union-case"> for more detail.
4988 CASE <replaceable>expression</replaceable>
4989 WHEN <replaceable>value</replaceable> THEN <replaceable>result</replaceable>
4990 <optional>WHEN ...</optional>
4991 <optional>ELSE <replaceable>result</replaceable></optional>
4996 This <quote>simple</quote> <token>CASE</token> expression is a
4997 specialized variant of the general form above. The
4998 <replaceable>expression</replaceable> is computed and compared to
4999 all the <replaceable>value</replaceable>s in the
5000 <token>WHEN</token> clauses until one is found that is equal. If
5001 no match is found, the <replaceable>result</replaceable> in the
5002 <token>ELSE</token> clause (or NULL) is returned. This is similar
5003 to the <function>switch</function> statement in C.
5008 The example above can be written using the simple
5009 <token>CASE</token> syntax:
5011 <prompt>=></prompt> <userinput>SELECT a,
5012 CASE a WHEN 1 THEN 'one'
5016 FROM test;</userinput>
5028 <bridgehead renderas="sect2">COALESCE</bridgehead>
5031 <function>COALESCE</function>(<replaceable>value</replaceable>
5032 <optional>, ...</optional>)
5036 The <function>COALESCE</function> function returns the first of its
5037 arguments that is not NULL. This is often useful to substitute a
5038 default value for NULL values when data is retrieved for display,
5041 SELECT COALESCE(description, short_description, '(none)') ...
5045 <bridgehead renderas="sect2">NULLIF</bridgehead>
5048 <primary>nullif</primary>
5052 <function>NULLIF</function>(<replaceable>value1</replaceable>,
5053 <replaceable>value2</replaceable>)
5057 The <function>NULLIF</function> function returns NULL if and only
5058 if <replaceable>value1</replaceable> and
5059 <replaceable>value2</replaceable> are equal. Otherwise it returns
5060 <replaceable>value1</replaceable>. This can be used to perform the
5061 inverse operation of the <function>COALESCE</function> example
5064 SELECT NULLIF(value, '(none)') ...
5070 <function>COALESCE</function> and <function>NULLIF</function> are
5071 just shorthand for <token>CASE</token> expressions. They are actually
5072 converted into <token>CASE</token> expressions at a very early stage
5073 of processing, and subsequent processing thinks it is dealing with
5074 <token>CASE</token>. Thus an incorrect <function>COALESCE</function> or
5075 <function>NULLIF</function> usage may draw an error message that
5076 refers to <token>CASE</token>.
5083 <sect1 id="functions-misc">
5084 <title>Miscellaneous Functions</title>
5087 <title>Session Information Functions</title>
5090 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5095 <entry><function>current_user</function></entry>
5096 <entry><type>name</type></entry>
5097 <entry>user name of current execution context</entry>
5100 <entry><function>session_user</function></entry>
5101 <entry><type>name</type></entry>
5102 <entry>session user name</entry>
5105 <entry><function>user</function></entry>
5106 <entry><type>name</type></entry>
5107 <entry>equivalent to <function>current_user</function></entry>
5110 <entry><function>current_schema()</function></entry>
5111 <entry><type>name</type></entry>
5112 <entry>name of current schema</entry>
5115 <entry><function>current_schemas(boolean)</function></entry>
5116 <entry><type>name[]</type></entry>
5117 <entry>names of schemas in search path optionally including implicit schemas</entry>
5120 <entry><function>current_database()</function></entry>
5121 <entry><type>name</type></entry>
5122 <entry>name of current database</entry>
5128 <indexterm zone="functions-misc">
5129 <primary>user</primary>
5130 <secondary>current</secondary>
5133 <indexterm zone="functions-misc">
5134 <primary>schema</primary>
5135 <secondary>current</secondary>
5138 <indexterm zone="functions-misc">
5139 <primary>search path</primary>
5140 <secondary>current</secondary>
5144 The <function>session_user</function> is the user that initiated a
5145 database connection; it is fixed for the duration of that
5146 connection. The <function>current_user</function> is the user identifier
5147 that is applicable for permission checking. Normally, it is equal
5148 to the session user, but it changes during the execution of
5149 functions with the attribute <literal>SECURITY DEFINER</literal>.
5150 In Unix parlance, the session user is the <quote>real user</quote> and
5151 the current user is the <quote>effective user</quote>.
5156 <function>current_user</function>, <function>session_user</function>, and
5157 <function>user</function> have special syntactic status in <acronym>SQL</acronym>:
5158 they must be called without trailing parentheses.
5163 <title>Deprecated</title>
5165 The function <function>getpgusername()</function> is an obsolete equivalent
5166 of <function>current_user</function>.
5171 <function>current_schema</function> returns the name of the schema that is
5172 at the front of the search path (or NULL if the search path is
5173 empty). This is the schema that will be used for any tables or
5174 other named objects that are created without specifying a target schema.
5175 <function>current_schemas(boolean)</function> returns an array of the names of all
5176 schemas presently in the search path. The boolean option determines whether or not
5177 implicitly included system schemas such as pg_catalog are included in the search
5181 <para id=set-search-path xreflabel="SET SEARCH_PATH">
5183 <primary>search path</primary>
5184 <secondary>changing at runtime</secondary>
5186 The search path may be altered by a runtime-alterable GUC setting. The
5187 command to use is <command>
5188 SET SEARCH_PATH '<varname>schema</varname>'[,'<varname>schema</varname>']...
5193 <title>System Information Functions</title>
5196 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5201 <entry><function>version</function></entry>
5202 <entry><type>text</type></entry>
5203 <entry>PostgreSQL version information</entry>
5209 <indexterm zone="functions-misc">
5210 <primary>version</primary>
5214 <function>version()</function> returns a string describing the PostgreSQL
5219 <title>Configuration Settings Information Functions</title>
5222 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5228 <function>current_setting</function>(<parameter>setting_name</parameter>)
5230 <entry><type>text</type></entry>
5231 <entry>value of current setting</entry>
5235 <function>set_config(<parameter>setting_name</parameter>,
5236 <parameter>new_value</parameter>,
5237 <parameter>is_local</parameter>)</function>
5239 <entry><type>text</type></entry>
5240 <entry>new value of current setting</entry>
5246 <indexterm zone="functions-misc">
5247 <primary>setting</primary>
5248 <secondary>current</secondary>
5251 <indexterm zone="functions-misc">
5252 <primary>setting</primary>
5253 <secondary>set</secondary>
5257 The <function>current_setting</function> is used to obtain the current
5258 value of the <parameter>setting_name</parameter> setting, as a query
5259 result. It is the equivalent to the SQL <command>SHOW</command> command.
5262 select current_setting('DateStyle');
5264 ---------------------------------------
5265 ISO with US (NonEuropean) conventions
5271 <function>set_config</function> allows the <parameter>setting_name
5272 </parameter> setting to be changed to <parameter>new_value</parameter>.
5273 If <parameter>is_local</parameter> is set to <literal>true</literal>,
5274 the new value will only apply to the current transaction. If you want
5275 the new value to apply for the current session, use
5276 <literal>false</literal> instead. It is the equivalent to the SQL
5277 <command>SET</command> command. For example:
5279 select set_config('show_statement_stats','off','f');
5288 <title>Access Privilege Inquiry Functions</title>
5291 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5296 <entry><function>has_table_privilege</function>(<parameter>user</parameter>,
5297 <parameter>table</parameter>,
5298 <parameter>access</parameter>)
5300 <entry><type>boolean</type></entry>
5301 <entry>does user have access to table</entry>
5304 <entry><function>has_table_privilege</function>(<parameter>table</parameter>,
5305 <parameter>access</parameter>)
5307 <entry><type>boolean</type></entry>
5308 <entry>does current user have access to table</entry>
5311 <entry><function>has_database_privilege</function>(<parameter>user</parameter>,
5312 <parameter>database</parameter>,
5313 <parameter>access</parameter>)
5315 <entry><type>boolean</type></entry>
5316 <entry>does user have access to database</entry>
5319 <entry><function>has_database_privilege</function>(<parameter>database</parameter>,
5320 <parameter>access</parameter>)
5322 <entry><type>boolean</type></entry>
5323 <entry>does current user have access to database</entry>
5326 <entry><function>has_function_privilege</function>(<parameter>user</parameter>,
5327 <parameter>function</parameter>,
5328 <parameter>access</parameter>)
5330 <entry><type>boolean</type></entry>
5331 <entry>does user have access to function</entry>
5334 <entry><function>has_function_privilege</function>(<parameter>function</parameter>,
5335 <parameter>access</parameter>)
5337 <entry><type>boolean</type></entry>
5338 <entry>does current user have access to function</entry>
5341 <entry><function>has_language_privilege</function>(<parameter>user</parameter>,
5342 <parameter>language</parameter>,
5343 <parameter>access</parameter>)
5345 <entry><type>boolean</type></entry>
5346 <entry>does user have access to language</entry>
5349 <entry><function>has_language_privilege</function>(<parameter>language</parameter>,
5350 <parameter>access</parameter>)
5352 <entry><type>boolean</type></entry>
5353 <entry>does current user have access to language</entry>
5356 <entry><function>has_schema_privilege</function>(<parameter>user</parameter>,
5357 <parameter>schema</parameter>,
5358 <parameter>access</parameter>)
5360 <entry><type>boolean</type></entry>
5361 <entry>does user have access to schema</entry>
5364 <entry><function>has_schema_privilege</function>(<parameter>schema</parameter>,
5365 <parameter>access</parameter>)
5367 <entry><type>boolean</type></entry>
5368 <entry>does current user have access to schema</entry>
5374 <indexterm zone="functions-misc">
5375 <primary>has_table_privilege</primary>
5377 <indexterm zone="functions-misc">
5378 <primary>has_database_privilege</primary>
5380 <indexterm zone="functions-misc">
5381 <primary>has_function_privilege</primary>
5383 <indexterm zone="functions-misc">
5384 <primary>has_language_privilege</primary>
5386 <indexterm zone="functions-misc">
5387 <primary>has_schema_privilege</primary>
5391 <function>has_table_privilege</function> checks whether a user
5392 can access a table in a particular way. The user can be
5393 specified by name or by ID
5394 (<classname>pg_user</classname>.<structfield>usesysid</structfield>), or if the argument is
5396 <function>current_user</function> is assumed. The table can be specified
5397 by name or by OID. (Thus, there are actually six variants of
5398 <function>has_table_privilege</function>, which can be distinguished by
5399 the number and types of their arguments.) When specifying by name,
5400 the name can be schema-qualified if necessary.
5401 The desired access type
5402 is specified by a text string, which must evaluate to one of the
5403 values <literal>SELECT</literal>, <literal>INSERT</literal>, <literal>UPDATE</literal>,
5404 <literal>DELETE</literal>, <literal>RULE</literal>, <literal>REFERENCES</literal>, or
5405 <literal>TRIGGER</literal>. (Case of the string is not significant, however.)
5408 SELECT has_table_privilege('myschema.mytable', 'select');
5413 <function>has_database_privilege</function> checks whether a user
5414 can access a database in a particular way. The possibilities for its
5415 arguments are analogous to <function>has_table_privilege</function>.
5416 The desired access type must evaluate to
5417 <literal>CREATE</literal>,
5418 <literal>TEMPORARY</literal>, or
5419 <literal>TEMP</literal> (which is equivalent to
5420 <literal>TEMPORARY</literal>).
5424 <function>has_function_privilege</function> checks whether a user
5425 can access a function in a particular way. The possibilities for its
5426 arguments are analogous to <function>has_table_privilege</function>.
5427 When specifying a function by a text string rather than by OID,
5428 the allowed input is the same as for the <type>regprocedure</> datatype.
5429 The desired access type must currently evaluate to
5430 <literal>EXECUTE</literal>.
5434 <function>has_language_privilege</function> checks whether a user
5435 can access a procedural language in a particular way. The possibilities
5436 for its arguments are analogous to <function>has_table_privilege</function>.
5437 The desired access type must currently evaluate to
5438 <literal>USAGE</literal>.
5442 <function>has_schema_privilege</function> checks whether a user
5443 can access a schema in a particular way. The possibilities for its
5444 arguments are analogous to <function>has_table_privilege</function>.
5445 The desired access type must evaluate to
5446 <literal>CREATE</literal> or
5447 <literal>USAGE</literal>.
5451 <title>Schema Visibility Inquiry Functions</title>
5454 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5459 <entry><function>pg_table_is_visible</function>(<parameter>tableOID</parameter>)
5461 <entry><type>boolean</type></entry>
5462 <entry>is table visible in search path</entry>
5465 <entry><function>pg_type_is_visible</function>(<parameter>typeOID</parameter>)
5467 <entry><type>boolean</type></entry>
5468 <entry>is type visible in search path</entry>
5471 <entry><function>pg_function_is_visible</function>(<parameter>functionOID</parameter>)
5473 <entry><type>boolean</type></entry>
5474 <entry>is function visible in search path</entry>
5477 <entry><function>pg_operator_is_visible</function>(<parameter>operatorOID</parameter>)
5479 <entry><type>boolean</type></entry>
5480 <entry>is operator visible in search path</entry>
5483 <entry><function>pg_opclass_is_visible</function>(<parameter>opclassOID</parameter>)
5485 <entry><type>boolean</type></entry>
5486 <entry>is operator class visible in search path</entry>
5492 <indexterm zone="functions-misc">
5493 <primary>pg_table_is_visible</primary>
5495 <indexterm zone="functions-misc">
5496 <primary>pg_type_is_visible</primary>
5498 <indexterm zone="functions-misc">
5499 <primary>pg_function_is_visible</primary>
5501 <indexterm zone="functions-misc">
5502 <primary>pg_operator_is_visible</primary>
5504 <indexterm zone="functions-misc">
5505 <primary>pg_opclass_is_visible</primary>
5509 <function>pg_table_is_visible</function> checks whether a table
5510 (or view, or any other kind of <structname>pg_class</> entry) is
5511 <firstterm>visible</> in the current schema search path. A table
5512 is said to be visible if its containing schema is in the search path
5513 and no table of the same name appears earlier in the search path.
5514 This is equivalent to the statement that the table can be referenced
5515 by name without explicit schema qualification.
5516 For example, to list the names of all visible tables:
5518 SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);
5523 <function>pg_type_is_visible</function>,
5524 <function>pg_function_is_visible</function>,
5525 <function>pg_operator_is_visible</function>, and
5526 <function>pg_opclass_is_visible</function> perform the same sort of
5527 visibility check for types, functions, operators, and operator classes,
5528 respectively. For functions and operators, an object in the search path
5529 is visible if there is no object of the same name <emphasis>and argument
5530 datatype(s)</> earlier in the path. For operator classes,
5531 both name and associated index access method are considered.
5535 All these functions require object OIDs to identify the object to be
5536 checked. If you want to test an object by name, it is convenient to use
5537 the OID alias types (<type>regclass</>, <type>regtype</>,
5538 <type>regprocedure</>, or <type>regoperator</>), for example
5540 SELECT pg_type_is_visible('myschema.widget'::regtype);
5542 Note that it would not make much sense to test an unqualified name in
5543 this way --- if the name can be recognized at all, it must be visible.
5547 <title>Catalog Information Functions</title>
5550 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5555 <entry><function>pg_get_viewdef</function>(<parameter>viewname</parameter>)</entry>
5556 <entry><type>text</type></entry>
5557 <entry>Get CREATE VIEW command for view</entry>
5560 <entry><function>pg_get_viewdef</function>(<parameter>viewOID</parameter>)</entry>
5561 <entry><type>text</type></entry>
5562 <entry>Get CREATE VIEW command for view</entry>
5565 <entry><function>pg_get_ruledef</function>(<parameter>ruleOID</parameter>)</entry>
5566 <entry><type>text</type></entry>
5567 <entry>Get CREATE RULE command for rule</entry>
5570 <entry><function>pg_get_indexdef</function>(<parameter>indexOID</parameter>)</entry>
5571 <entry><type>text</type></entry>
5572 <entry>Get CREATE INDEX command for index</entry>
5575 <entry><function>pg_get_constraintdef</function>(<parameter>constraintOID</parameter>)</entry>
5576 <entry><type>text</type></entry>
5577 <entry>Get definition of a constraint</entry>
5580 <entry><function>pg_get_userbyid</function>(<parameter>userid</parameter>)</entry>
5581 <entry><type>name</type></entry>
5582 <entry>Get user name given ID</entry>
5588 <indexterm zone="functions-misc">
5589 <primary>pg_get_viewdef</primary>
5592 <indexterm zone="functions-misc">
5593 <primary>pg_get_ruledef</primary>
5596 <indexterm zone="functions-misc">
5597 <primary>pg_get_indexdef</primary>
5600 <indexterm zone="functions-misc">
5601 <primary>pg_get_constraintdef</primary>
5604 <indexterm zone="functions-misc">
5605 <primary>pg_get_userbyid</primary>
5609 These functions extract information from the system catalogs.
5610 <function>pg_get_viewdef()</function>,
5611 <function>pg_get_ruledef()</function>,
5612 <function>pg_get_indexdef()</function>, and
5613 <function>pg_get_constraintdef()</function> respectively reconstruct the
5614 creating command for a view, rule, index, or constraint.
5615 (Note that this is a decompiled
5616 reconstruction, not the verbatim text of the command.)
5617 At present <function>pg_get_constraintdef()</function> only works for
5618 foreign-key constraints.
5619 <function>pg_get_userbyid()</function> extracts a user's name given a
5620 <structfield>usesysid</structfield> value.
5624 <title>Comment Information Functions</title>
5627 <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
5632 <entry><function>obj_description</function>(<parameter>objectOID</parameter>, <parameter>tablename</parameter>)</entry>
5633 <entry><type>text</type></entry>
5634 <entry>Get comment for a database object</entry>
5637 <entry><function>obj_description</function>(<parameter>objectOID</parameter>)</entry>
5638 <entry><type>text</type></entry>
5639 <entry>Get comment for a database object (<emphasis>deprecated</emphasis>)</entry>
5642 <entry><function>col_description</function>(<parameter>tableOID</parameter>, <parameter>columnnumber</parameter>)</entry>
5643 <entry><type>text</type></entry>
5644 <entry>Get comment for a table column</entry>
5650 <indexterm zone="functions-misc">
5651 <primary>obj_description</primary>
5654 <indexterm zone="functions-misc">
5655 <primary>col_description</primary>
5659 These functions extract comments previously stored with the
5660 <command>COMMENT</command> command. <literal>NULL</literal> is returned if
5661 no comment can be found matching the specified parameters.
5665 The two-parameter form of <function>obj_description()</function> returns the
5666 comment for a database object specified by its OID and the name of the
5667 containing system catalog. For example,
5668 <literal>obj_description(123456,'pg_class')</literal>
5669 would retrieve the comment for a table with OID 123456.
5670 The one-parameter form of <function>obj_description()</function> requires only
5671 the object OID. It is now deprecated since there is no guarantee that
5672 OIDs are unique across different system catalogs; therefore, the wrong
5673 comment could be returned.
5677 <function>col_description()</function> returns the comment for a table column,
5678 which is specified by the OID of its table and its column number.
5679 <function>obj_description()</function> cannot be used for table columns since
5680 columns do not have OIDs of their own.
5686 <sect1 id="functions-aggregate">
5687 <title>Aggregate Functions</title>
5690 <title>Author</title>
5692 Written by Isaac Wilcox <email>isaac@azartmedia.com</email> on 2000-06-16
5697 <firstterm>Aggregate functions</firstterm> compute a single result
5698 value from a set of input values. The special syntax
5699 considerations for aggregate functions are explained in <xref
5700 linkend="syntax-aggregates">. Consult the <citetitle>PostgreSQL
5701 Tutorial</citetitle> for additional introductory information.
5704 <table tocentry="1">
5705 <title>Aggregate Functions</title>
5710 <entry>Function</entry>
5711 <entry>Description</entry>
5712 <entry>Notes</entry>
5718 <entry>avg(<replaceable class="parameter">expression</replaceable>)</entry>
5719 <entry>the average (arithmetic mean) of all input values</entry>
5722 <primary>average</primary>
5723 <secondary>function</secondary>
5725 Finding the average value is available on the following data
5726 types: <type>smallint</type>, <type>integer</type>,
5727 <type>bigint</type>, <type>real</type>, <type>double
5728 precision</type>, <type>numeric</type>, <type>interval</type>.
5729 The result is of type <type>numeric</type> for any integer type
5730 input, <type>double precision</type> for floating-point input,
5731 otherwise the same as the input data type.
5736 <entry><function>count</function>(*)</entry>
5737 <entry>number of input values</entry>
5738 <entry>The return value is of type <type>bigint</type>.</entry>
5742 <entry><function>count</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5744 Counts the input values for which the value of <replaceable
5745 class="parameter">expression</replaceable> is not NULL.
5747 <entry>The return value is of type <type>bigint</type>.</entry>
5751 <entry><function>max</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5752 <entry>the maximum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
5754 Available for all numeric, string, and date/time types. The
5755 result has the same type as the input expression.
5760 <entry><function>min</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5761 <entry>the minimum value of <replaceable class="parameter">expression</replaceable> across all input values</entry>
5763 Available for all numeric, string, and date/time types. The
5764 result has the same type as the input expression.
5769 <entry><function>stddev</function>(<replaceable
5770 class="parameter">expression</replaceable>)</entry>
5771 <entry>the sample standard deviation of the input values</entry>
5774 <primary>standard deviation</primary>
5776 Finding the standard deviation is available on the following
5777 data types: <type>smallint</type>, <type>integer</type>,
5778 <type>bigint</type>, <type>real</type>, <type>double
5779 precision</type>, <type>numeric</type>. The result is of type
5780 <type>double precision</type> for floating-point input,
5781 otherwise <type>numeric</type>.
5786 <entry><function>sum</function>(<replaceable class="parameter">expression</replaceable>)</entry>
5787 <entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
5789 Summation is available on the following data types:
5790 <type>smallint</type>, <type>integer</type>,
5791 <type>bigint</type>, <type>real</type>, <type>double
5792 precision</type>, <type>numeric</type>, <type>interval</type>.
5793 The result is of type <type>bigint</type> for <type>smallint</type>
5794 or <type>integer</type> input, <type>numeric</type> for
5796 input, <type>double precision</type> for floating-point input,
5797 otherwise the same as the input data type.
5802 <entry><function>variance</function>(<replaceable
5803 class="parameter">expression</replaceable>)</entry>
5804 <entry>the sample variance of the input values</entry>
5807 <primary>variance</primary>
5809 The variance is the square of the standard deviation. The
5810 supported data types and result types are the same as for
5820 It should be noted that except for <function>COUNT</function>,
5821 these functions return NULL when no rows are selected. In
5822 particular, <function>SUM</function> of no rows returns NULL, not
5823 zero as one might expect. <function>COALESCE</function> may be
5824 used to substitute zero for NULL when necessary.
5830 <sect1 id="functions-subquery">
5831 <title>Subquery Expressions</title>
5834 <primary>exists</primary>
5838 <primary>in</primary>
5842 <primary>not in</primary>
5846 <primary>any</primary>
5850 <primary>all</primary>
5854 <primary>some</primary>
5858 <primary>subqueries</primary>
5862 This section describes the <acronym>SQL</acronym>-compliant subquery
5863 expressions available in <productname>PostgreSQL</productname>.
5864 All of the expression forms documented in this section return
5865 Boolean (true/false) results.
5868 <bridgehead renderas="sect2">EXISTS</bridgehead>
5871 EXISTS ( <replaceable>subquery</replaceable> )
5875 The argument of <token>EXISTS</token> is an arbitrary SELECT statement,
5876 or <firstterm>subquery</firstterm>. The
5877 subquery is evaluated to determine whether it returns any rows.
5878 If it returns at least one row, the result of <token>EXISTS</token> is
5879 TRUE; if the subquery returns no rows, the result of <token>EXISTS</token>
5884 The subquery can refer to variables from the surrounding query,
5885 which will act as constants during any one evaluation of the subquery.
5889 The subquery will generally only be executed far enough to determine
5890 whether at least one row is returned, not all the way to completion.
5891 It is unwise to write a subquery that has any side-effects (such as
5892 calling sequence functions); whether the side-effects occur or not
5893 may be difficult to predict.
5897 Since the result depends only on whether any rows are returned,
5898 and not on the contents of those rows, the output list of the
5899 subquery is normally uninteresting. A common coding convention is
5900 to write all EXISTS tests in the form
5901 <literal>EXISTS(SELECT 1 WHERE ...)</literal>. There are exceptions to
5902 this rule however, such as subqueries that use <token>INTERSECT</token>.
5906 This simple example is like an inner join on col2, but it produces at
5907 most one output row for each tab1 row, even if there are multiple matching
5910 SELECT col1 FROM tab1
5911 WHERE EXISTS(SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
5915 <bridgehead renderas="sect2">IN (scalar form)</bridgehead>
5918 <replaceable>expression</replaceable> IN
5919 <replaceable>value</replaceable><optional>, ...</optional>)
5923 The right-hand side of this form of <token>IN</token> is a parenthesized list
5924 of scalar expressions. The result is TRUE if the left-hand expression's
5925 result is equal to any of the right-hand expressions. This is a shorthand
5929 <replaceable>expression</replaceable> = <replaceable>value1</replaceable>
5931 <replaceable>expression</replaceable> = <replaceable>value2</replaceable>
5936 Note that if the left-hand expression yields NULL, or if there are
5937 no equal right-hand values and at least one right-hand expression yields
5938 NULL, the result of the <token>IN</token> construct will be NULL, not FALSE.
5939 This is in accordance with SQL's normal rules for Boolean combinations
5945 This form of <token>IN</token> is not truly a subquery expression, but it
5946 seems best to document it in the same place as subquery <token>IN</token>.
5950 <bridgehead renderas="sect2">IN (subquery form)</bridgehead>
5953 <replaceable>expression</replaceable> IN (<replaceable>subquery</replaceable>)
5957 The right-hand side of this form of <token>IN</token> is a parenthesized
5958 subquery, which must return exactly one column. The left-hand expression
5959 is evaluated and compared to each row of the subquery result.
5960 The result of <token>IN</token> is TRUE if any equal subquery row is found.
5961 The result is FALSE if no equal row is found (including the special
5962 case where the subquery returns no rows).
5966 Note that if the left-hand expression yields NULL, or if there are
5967 no equal right-hand values and at least one right-hand row yields
5968 NULL, the result of the <token>IN</token> construct will be NULL, not FALSE.
5969 This is in accordance with SQL's normal rules for Boolean combinations
5974 As with <token>EXISTS</token>, it's unwise to assume that the subquery will
5975 be evaluated completely.
5979 (<replaceable>expression</replaceable>,
5980 <replaceable>expres</replaceable><optional>,</optional>)
5981 IN (<replaceable>subquery</replaceable>)
5985 The right-hand side of this form of <token>IN</token> is a parenthesized
5986 subquery, which must return exactly as many columns as there are
5987 expressions in the left-hand list. The left-hand expressions are
5988 evaluated and compared row-wise to each row of the subquery result.
5989 The result of <token>IN</token> is TRUE if any equal subquery row is found.
5990 The result is FALSE if no equal row is found (including the special
5991 case where the subquery returns no rows).
5995 As usual, NULLs in the expressions or subquery rows are combined per
5996 the normal rules of SQL Boolean expressions. Two rows are considered
5997 equal if all their corresponding members are non-null and equal; the rows
5998 are unequal if any corresponding members are non-null and unequal;
5999 otherwise the result of that row comparison is unknown (NULL).
6000 If all the row results are either unequal or NULL, with at least one NULL,
6001 then the result of <token>IN</token> is NULL.
6004 <bridgehead renderas="sect2">NOT IN (scalar form)</bridgehead>
6007 <replaceable>expression</replaceable> NOT IN
6008 <replaceable>value</replaceable><optional>, ...</optional>)
6012 The right-hand side of this form of <token>NOT IN</token> is a parenthesized list
6013 of scalar expressions. The result is TRUE if the left-hand expression's
6014 result is unequal to all of the right-hand expressions. This is a shorthand
6018 <replaceable>expression</replaceable> <> <replaceable>value1</replaceable>
6020 <replaceable>expression</replaceable> <> <replaceable>value2</replaceable>
6025 Note that if the left-hand expression yields NULL, or if there are
6026 no equal right-hand values and at least one right-hand expression yields
6027 NULL, the result of the <token>NOT IN</token> construct will be NULL, not TRUE
6028 as one might naively expect.
6029 This is in accordance with SQL's normal rules for Boolean combinations
6035 <literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
6036 cases. However, NULLs are much more likely to trip up the novice when
6037 working with <token>NOT IN</token> than when working with <token>IN</token>.
6038 It's best to express your condition positively if possible.
6042 <bridgehead renderas="sect2">NOT IN (subquery form)</bridgehead>
6045 <replaceable>expression</replaceable> NOT IN (<replaceable>subquery</replaceable>)
6049 The right-hand side of this form of <token>NOT IN</token> is a parenthesized
6050 subquery, which must return exactly one column. The left-hand expression
6051 is evaluated and compared to each row of the subquery result.
6052 The result of <token>NOT IN</token> is TRUE if only unequal subquery rows
6053 are found (including the special case where the subquery returns no rows).
6054 The result is FALSE if any equal row is found.
6058 Note that if the left-hand expression yields NULL, or if there are
6059 no equal right-hand values and at least one right-hand row yields
6060 NULL, the result of the <token>NOT IN</token> construct will be NULL, not TRUE.
6061 This is in accordance with SQL's normal rules for Boolean combinations
6066 As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6067 be evaluated completely.
6071 (<replaceable>expression</replaceable>,
6072 <replaceable>expres</replaceable><optional>,</optional>)
6073 NOT IN (<replaceable>subquery</replaceable>)
6077 The right-hand side of this form of <token>NOT IN</token> is a parenthesized
6078 subquery, which must return exactly as many columns as there are
6079 expressions in the left-hand list. The left-hand expressions are
6080 evaluated and compared row-wise to each row of the subquery result.
6081 The result of <token>NOT IN</token> is TRUE if only unequal subquery rows
6082 are found (including the special case where the subquery returns no rows).
6083 The result is FALSE if any equal row is found.
6087 As usual, NULLs in the expressions or subquery rows are combined per
6088 the normal rules of SQL Boolean expressions. Two rows are considered
6089 equal if all their corresponding members are non-null and equal; the rows
6090 are unequal if any corresponding members are non-null and unequal;
6091 otherwise the result of that row comparison is unknown (NULL).
6092 If all the row results are either unequal or NULL, with at least one NULL,
6093 then the result of <token>NOT IN</token> is NULL.
6096 <bridgehead renderas="sect2">ANY</bridgehead>
6099 <replaceable>expression</replaceable>
6100 <replaceable>oper</replaceable> ANY (<replaceable>subquery</replaceable>)
6101 <replaceable>expression</replaceable>
6102 <replaceable>oper</replaceable> SOME (<replaceable>subquery</replaceable>)
6106 The right-hand side of this form of <token>ANY</token> is a parenthesized
6107 subquery, which must return exactly one column. The left-hand expression
6108 is evaluated and compared to each row of the subquery result using the
6109 given <replaceable>operator</replaceable>, which must yield a Boolean
6111 The result of <token>ANY</token> is TRUE if any true result is obtained.
6112 The result is FALSE if no true result is found (including the special
6113 case where the subquery returns no rows).
6117 <token>SOME</token> is a synonym for <token>ANY</token>.
6118 <token>IN</token> is equivalent to <literal>= ANY</literal>.
6122 Note that if there are no successes and at least one right-hand row yields
6123 NULL for the operator's result, the result of the <token>ANY</token> construct
6124 will be NULL, not FALSE.
6125 This is in accordance with SQL's normal rules for Boolean combinations
6130 As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6131 be evaluated completely.
6135 (<replaceable>expression</replaceable>,
6136 <replaceable>expres</replaceable><optional>,</optional>optiona<replaceable></replaceable> ANY (<replaceable>subquery</replaceable>)
6137 (<replaceable>expression</replaceable>,
6138 <replaceable>expres</replaceable><optional>,</optional>optiona<replaceable></replaceable> SOME (<replaceable>subquery</replaceable>)
6142 The right-hand side of this form of <token>ANY</token> is a parenthesized
6143 subquery, which must return exactly as many columns as there are
6144 expressions in the left-hand list. The left-hand expressions are
6145 evaluated and compared row-wise to each row of the subquery result,
6146 using the given <replaceable>operator</replaceable>. Presently,
6147 only <literal>=</literal> and <literal><></literal> operators are allowed
6148 in row-wise <token>ANY</token> queries.
6149 The result of <token>ANY</token> is TRUE if any equal or unequal row is
6150 found, respectively.
6151 The result is FALSE if no such row is found (including the special
6152 case where the subquery returns no rows).
6156 As usual, NULLs in the expressions or subquery rows are combined per
6157 the normal rules of SQL Boolean expressions. Two rows are considered
6158 equal if all their corresponding members are non-null and equal; the rows
6159 are unequal if any corresponding members are non-null and unequal;
6160 otherwise the result of that row comparison is unknown (NULL).
6161 If there is at least one NULL row result, then the result of <token>ANY</token>
6162 cannot be FALSE; it will be TRUE or NULL.
6165 <bridgehead renderas="sect2">ALL</bridgehead>
6168 <replaceable>expression</replaceable>
6169 <replaceable>oper</replaceable> ALL (<replaceable>subquery</replaceable>)
6173 The right-hand side of this form of <token>ALL</token> is a parenthesized
6174 subquery, which must return exactly one column. The left-hand expression
6175 is evaluated and compared to each row of the subquery result using the
6176 given <replaceable>operator</replaceable>, which must yield a Boolean
6178 The result of <token>ALL</token> is TRUE if all rows yield TRUE
6179 (including the special case where the subquery returns no rows).
6180 The result is FALSE if any false result is found.
6184 <token>NOT IN</token> is equivalent to <literal><> ALL</literal>.
6188 Note that if there are no failures but at least one right-hand row yields
6189 NULL for the operator's result, the result of the <token>ALL</token> construct
6190 will be NULL, not TRUE.
6191 This is in accordance with SQL's normal rules for Boolean combinations
6196 As with <token>EXISTS</token>, it's unwise to assume that the subquery will
6197 be evaluated completely.
6201 (<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
6205 The right-hand side of this form of <token>ALL</token> is a parenthesized
6206 subquery, which must return exactly as many columns as there are
6207 expressions in the left-hand list. The left-hand expressions are
6208 evaluated and compared row-wise to each row of the subquery result,
6209 using the given <replaceable>operator</replaceable>. Presently,
6210 only <literal>=</literal> and <literal><></literal> operators are allowed
6211 in row-wise <token>ALL</token> queries.
6212 The result of <token>ALL</token> is TRUE if all subquery rows are equal
6213 or unequal, respectively (including the special
6214 case where the subquery returns no rows).
6215 The result is FALSE if any row is found to be unequal or equal,
6220 As usual, NULLs in the expressions or subquery rows are combined per
6221 the normal rules of SQL Boolean expressions. Two rows are considered
6222 equal if all their corresponding members are non-null and equal; the rows
6223 are unequal if any corresponding members are non-null and unequal;
6224 otherwise the result of that row comparison is unknown (NULL).
6225 If there is at least one NULL row result, then the result of <token>ALL</token>
6226 cannot be TRUE; it will be FALSE or NULL.
6229 <bridgehead renderas="sect2">Row-wise comparison</bridgehead>
6232 (<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> (<replaceable>subquery</replaceable>)
6233 (<replaceable>expression</replaceable>, <replaceable>expression</replaceable><optional>, ...</optional>) <replaceable>operator</replaceable> (<replaceable>expression</replaceable> <replaceable>expression</replaceable><optional>, ...</optional>)
6237 The left-hand side is a list of scalar expressions. The right-hand side
6238 can be either a list of scalar expressions of the same length, or a
6239 parenthesized subquery, which must return exactly as many columns as there
6240 are expressions on the left-hand side. Furthermore, the subquery cannot
6241 return more than one row. (If it returns zero rows, the result is taken to
6242 be NULL.) The left-hand side is evaluated and compared row-wise to the
6243 single subquery result row, or to the right-hand expression list.
6244 Presently, only <literal>=</literal> and <literal><></literal> operators are allowed
6245 in row-wise comparisons.
6246 The result is TRUE if the two rows are equal or unequal, respectively.
6250 As usual, NULLs in the expressions or subquery rows are combined per
6251 the normal rules of SQL Boolean expressions. Two rows are considered
6252 equal if all their corresponding members are non-null and equal; the rows
6253 are unequal if any corresponding members are non-null and unequal;
6254 otherwise the result of the row comparison is unknown (NULL).
6261 <!-- Keep this comment at the end of the file
6266 sgml-minimize-attributes:nil
6267 sgml-always-quote-attributes:t
6270 sgml-parent-document:nil
6271 sgml-default-dtd-file:"./reference.ced"
6272 sgml-exposed-tags:nil
6273 sgml-local-catalogs:("/usr/lib/sgml/catalog")
6274 sgml-local-ecat-files:nil