Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13dbc7a

Browse files
committed
array_offset() and array_offsets()
These functions return the offset position or positions of a value in anarray.Author: Pavel StěhuleReviewed by: Jim Nasby
1 parentf9dead5 commit13dbc7a

File tree

10 files changed

+525
-8
lines changed

10 files changed

+525
-8
lines changed

‎doc/src/sgml/array.sgml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,25 @@ SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000];
600600
index, as described in <xref linkend="indexes-types">.
601601
</para>
602602

603+
<para>
604+
You can also search for specific values in an array using the <function>array_offset</>
605+
and <function>array_offsets</> functions. The former returns the position of
606+
the first occurrence of a value in an array; the latter returns an array with the
607+
positions of all occurrences of the value in the array. For example:
608+
609+
<programlisting>
610+
SELECT array_offset(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
611+
array_offset
612+
--------------
613+
2
614+
615+
SELECT array_offsets(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
616+
array_offsets
617+
---------------
618+
{1,4,8}
619+
</programlisting>
620+
</para>
621+
603622
<tip>
604623
<para>
605624
Arrays are not sets; searching for specific array elements

‎doc/src/sgml/func.sgml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11479,6 +11479,12 @@ SELECT NULLIF(value, '(none)') ...
1147911479
<indexterm>
1148011480
<primary>array_lower</primary>
1148111481
</indexterm>
11482+
<indexterm>
11483+
<primary>array_offset</primary>
11484+
</indexterm>
11485+
<indexterm>
11486+
<primary>array_offsets</primary>
11487+
</indexterm>
1148211488
<indexterm>
1148311489
<primary>array_prepend</primary>
1148411490
</indexterm>
@@ -11596,6 +11602,32 @@ SELECT NULLIF(value, '(none)') ...
1159611602
<entry><literal>array_lower('[0:2]={1,2,3}'::int[], 1)</literal></entry>
1159711603
<entry><literal>0</literal></entry>
1159811604
</row>
11605+
<row>
11606+
<entry>
11607+
<literal>
11608+
<function>array_offset</function>(<type>anyarray</type>, <type>anyelement</type> <optional>, <type>int</type></optional>)
11609+
</literal>
11610+
</entry>
11611+
<entry><type>int</type></entry>
11612+
<entry>returns the offset of the first occurrence of the second
11613+
argument in the array, starting at the element indicated by the third
11614+
argument or at the first element (array must be one-dimensional)</entry>
11615+
<entry><literal>array_offset(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon')</literal></entry>
11616+
<entry><literal>2</literal></entry>
11617+
</row>
11618+
<row>
11619+
<entry>
11620+
<literal>
11621+
<function>array_offsets</function>(<type>anyarray</type>, <type>anyelement</type>)
11622+
</literal>
11623+
</entry>
11624+
<entry><type>int[]</type></entry>
11625+
<entry>returns an array of offsets of all occurrences of the second
11626+
argument in the array given as first argument (array must be
11627+
one-dimensional)</entry>
11628+
<entry><literal>array_offsets(ARRAY['A','A','B','A'], 'A')</literal></entry>
11629+
<entry><literal>{1,2,4}</literal></entry>
11630+
</row>
1159911631
<row>
1160011632
<entry>
1160111633
<literal>
@@ -11707,6 +11739,23 @@ NULL baz</literallayout>(3 rows)</entry>
1170711739
</tgroup>
1170811740
</table>
1170911741

11742+
<para>
11743+
In <function>array_offset</function> and <function>array_offsets</>,
11744+
each array element is compared to the searched value using
11745+
<literal>IS NOT DISTINCT FROM</literal> semantics.
11746+
</para>
11747+
11748+
<para>
11749+
In <function>array_offset</function>, <literal>NULL</literal> is returned
11750+
if the value is not found.
11751+
</para>
11752+
11753+
<para>
11754+
In <function>array_offsets</function>, <literal>NULL</literal> is returned
11755+
only if the array is <literal>NULL</literal>; if the value is not found in
11756+
the array, an empty array is returned instead.
11757+
</para>
11758+
1171011759
<para>
1171111760
In <function>string_to_array</function>, if the delimiter parameter is
1171211761
NULL, each character in the input string will become a separate element in

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp