From 13dbc7a824b3f905904cab51840d37f31a07a9ef Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 18 Mar 2015 16:01:34 -0300 Subject: array_offset() and array_offsets() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These functions return the offset position or positions of a value in an array. Author: Pavel Stěhule Reviewed by: Jim Nasby --- doc/src/sgml/array.sgml | 19 +++++++++++++++++++ doc/src/sgml/func.sgml | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) (limited to 'doc/src') diff --git a/doc/src/sgml/array.sgml b/doc/src/sgml/array.sgml index 9ea10682a56..092013b83b5 100644 --- a/doc/src/sgml/array.sgml +++ b/doc/src/sgml/array.sgml @@ -600,6 +600,25 @@ SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000]; index, as described in . + + You can also search for specific values in an array using the array_offset + and array_offsets functions. The former returns the position of + the first occurrence of a value in an array; the latter returns an array with the + positions of all occurrences of the value in the array. For example: + + +SELECT array_offset(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon'); + array_offset +-------------- + 2 + +SELECT array_offsets(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1); + array_offsets +--------------- + {1,4,8} + + + Arrays are not sets; searching for specific array elements diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index c198beaf396..5843eaa9ffe 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -11479,6 +11479,12 @@ SELECT NULLIF(value, '(none)') ... array_lower + + array_offset + + + array_offsets + array_prepend @@ -11596,6 +11602,32 @@ SELECT NULLIF(value, '(none)') ... array_lower('[0:2]={1,2,3}'::int[], 1) 0 + + + + array_offset(anyarray, anyelement , int) + + + int + returns the offset of the first occurrence of the second + argument in the array, starting at the element indicated by the third + argument or at the first element (array must be one-dimensional) + array_offset(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon') + 2 + + + + + array_offsets(anyarray, anyelement) + + + int[] + returns an array of offsets of all occurrences of the second + argument in the array given as first argument (array must be + one-dimensional) + array_offsets(ARRAY['A','A','B','A'], 'A') + {1,2,4} + @@ -11707,6 +11739,23 @@ NULL baz(3 rows) + + In array_offset and array_offsets, + each array element is compared to the searched value using + IS NOT DISTINCT FROM semantics. + + + + In array_offset, NULL is returned + if the value is not found. + + + + In array_offsets, NULL is returned + only if the array is NULL; if the value is not found in + the array, an empty array is returned instead. + + In string_to_array, if the delimiter parameter is NULL, each character in the input string will become a separate element in -- cgit v1.2.3