summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/functions-range.html
blob: 6d7c4b15608166afd7d7bf296c3a8691cf6795a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.20. Range Functions and Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="functions-array.html" title="9.19. Array Functions and Operators" /><link rel="next" href="functions-aggregate.html" title="9.21. Aggregate Functions" /></head><body id="docContent" class="container-fluid col-10"><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.20. Range Functions and Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-array.html" title="9.19. Array Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-aggregate.html" title="9.21. Aggregate Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-RANGE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.20. Range Functions and Operators</h2></div></div></div><p>
   See <a class="xref" href="rangetypes.html" title="8.17. Range Types">Section 8.17</a> for an overview of range types.
  </p><p>
   <a class="xref" href="functions-range.html#RANGE-OPERATORS-TABLE" title="Table 9.53. Range Operators">Table 9.53</a> shows the specialized operators
   available for range types.
   In addition to those, the usual comparison operators shown in
   <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE" title="Table 9.1. Comparison Operators">Table 9.1</a> are available for range
   types.  The comparison operators order first by the range lower bounds, and
   only if those are equal do they compare the upper bounds.  This does not
   usually result in a useful overall ordering, but the operators are provided
   to allow unique indexes to be constructed on ranges.
  </p><div class="table" id="RANGE-OPERATORS-TABLE"><p class="title"><strong>Table 9.53. Range Operators</strong></p><div class="table-contents"><table class="table" summary="Range Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
        Operator
       </p>
       <p>
        Description
       </p>
       <p>
        Example(s)
       </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">@&gt;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Does the first range contain the second?
       </p>
       <p>
        <code class="literal">int4range(2,4) @&gt; int4range(2,3)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">@&gt;</code> <code class="type">anyelement</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Does the range contain the element?
       </p>
       <p>
        <code class="literal">'[2011-01-01,2011-03-01)'::tsrange @&gt; '2011-01-10'::timestamp</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&lt;@</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the first range contained by the second?
       </p>
       <p>
        <code class="literal">int4range(2,4) &lt;@ int4range(1,7)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyelement</code> <code class="literal">&lt;@</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the element contained in the range?
       </p>
       <p>
        <code class="literal">42 &lt;@ int4range(1,7)</code><code class="returnvalue">f</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&amp;&amp;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Do the ranges overlap, that is, have any elements in common?
       </p>
       <p>
        <code class="literal">int8range(3,7) &amp;&amp; int8range(4,12)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&lt;&lt;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the first range strictly left of the second?
       </p>
       <p>
        <code class="literal">int8range(1,10) &lt;&lt; int8range(100,110)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&gt;&gt;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the first range strictly right of the second?
       </p>
       <p>
        <code class="literal">int8range(50,60) &gt;&gt; int8range(20,30)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&amp;&lt;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Does the first range not extend to the right of the second?
       </p>
       <p>
        <code class="literal">int8range(1,20) &amp;&lt; int8range(18,20)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">&amp;&gt;</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Does the first range not extend to the left of the second?
       </p>
       <p>
        <code class="literal">int8range(7,20) &amp;&gt; int8range(5,10)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">-|-</code> <code class="type">anyrange</code><code class="returnvalue">boolean</code>
       </p>
       <p>
        Are the ranges adjacent?
       </p>
       <p>
        <code class="literal">numrange(1.1,2.2) -|- numrange(2.2,3.3)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">+</code> <code class="type">anyrange</code><code class="returnvalue">anyrange</code>
       </p>
       <p>
        Computes the union of the ranges.  The ranges must overlap or be
        adjacent, so that the union is a single range (but
        see <code class="function">range_merge()</code>).
       </p>
       <p>
        <code class="literal">numrange(5,15) + numrange(10,20)</code><code class="returnvalue">[5,20)</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">*</code> <code class="type">anyrange</code><code class="returnvalue">anyrange</code>
       </p>
       <p>
        Computes the intersection of the ranges.
       </p>
       <p>
        <code class="literal">int8range(5,15) * int8range(10,20)</code><code class="returnvalue">[10,15)</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <code class="type">anyrange</code> <code class="literal">-</code> <code class="type">anyrange</code><code class="returnvalue">anyrange</code>
       </p>
       <p>
        Computes the difference of the ranges.  The second range must not be
        contained in the first in such a way that the difference would not be
        a single range.
       </p>
       <p>
        <code class="literal">int8range(5,15) - int8range(10,20)</code><code class="returnvalue">[5,10)</code>
       </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
   The left-of/right-of/adjacent operators always return false when an empty
   range is involved; that is, an empty range is not considered to be either
   before or after any other range.
  </p><p>
   <a class="xref" href="functions-range.html#RANGE-FUNCTIONS-TABLE" title="Table 9.54. Range Functions">Table 9.54</a> shows the functions
   available for use with range types.
  </p><div class="table" id="RANGE-FUNCTIONS-TABLE"><p class="title"><strong>Table 9.54. Range Functions</strong></p><div class="table-contents"><table class="table" summary="Range Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
        Function
       </p>
       <p>
        Description
       </p>
       <p>
        Example(s)
       </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.1.1.1.1" class="indexterm"></a>
        <code class="function">lower</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">anyelement</code>
       </p>
       <p>
        Extracts the lower bound of the range (<code class="literal">NULL</code> if the
        range is empty or the lower bound is infinite).
       </p>
       <p>
        <code class="literal">lower(numrange(1.1,2.2))</code><code class="returnvalue">1.1</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.2.1.1.1" class="indexterm"></a>
        <code class="function">upper</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">anyelement</code>
       </p>
       <p>
        Extracts the upper bound of the range (<code class="literal">NULL</code> if the
        range is empty or the upper bound is infinite).
       </p>
       <p>
        <code class="literal">upper(numrange(1.1,2.2))</code><code class="returnvalue">2.2</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.3.1.1.1" class="indexterm"></a>
        <code class="function">isempty</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the range empty?
       </p>
       <p>
        <code class="literal">isempty(numrange(1.1,2.2))</code><code class="returnvalue">f</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.4.1.1.1" class="indexterm"></a>
        <code class="function">lower_inc</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the range's lower bound inclusive?
       </p>
       <p>
        <code class="literal">lower_inc(numrange(1.1,2.2))</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.5.1.1.1" class="indexterm"></a>
        <code class="function">upper_inc</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the range's upper bound inclusive?
       </p>
       <p>
        <code class="literal">upper_inc(numrange(1.1,2.2))</code><code class="returnvalue">f</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.6.1.1.1" class="indexterm"></a>
        <code class="function">lower_inf</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the range's lower bound infinite?
       </p>
       <p>
        <code class="literal">lower_inf('(,)'::daterange)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.7.1.1.1" class="indexterm"></a>
        <code class="function">upper_inf</code> ( <code class="type">anyrange</code> )
        → <code class="returnvalue">boolean</code>
       </p>
       <p>
        Is the range's upper bound infinite?
       </p>
       <p>
        <code class="literal">upper_inf('(,)'::daterange)</code><code class="returnvalue">t</code>
       </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
        <a id="id-1.5.8.26.7.2.2.8.1.1.1" class="indexterm"></a>
        <code class="function">range_merge</code> ( <code class="type">anyrange</code>, <code class="type">anyrange</code> )
        → <code class="returnvalue">anyrange</code>
       </p>
       <p>
        Computes the smallest range that includes both of the given ranges.
       </p>
       <p>
        <code class="literal">range_merge('[1,2)'::int4range, '[3,4)'::int4range)</code><code class="returnvalue">[1,4)</code>
       </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
   The <code class="function">lower_inc</code>, <code class="function">upper_inc</code>,
   <code class="function">lower_inf</code>, and <code class="function">upper_inf</code>
   functions all return false for an empty range.
  </p></div><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-array.html" title="9.19. Array Functions and Operators">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-aggregate.html" title="9.21. Aggregate Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.19. Array Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.21. Aggregate Functions</td></tr></table></div></body></html>