Name
position — returns the index of an element within an array or string
Synopsis
position
(
|
element any , |
| array any , | |
| start_index integer (optional, default 1) , | |
step
integer (optional, default 1)
); |
Description
position returns the one-based index for the first occurrence of element within an array or string, beginning from offset start_index and iterating with step. If the second argument is a string, the first argument must be a string with a single character. If the second argument is an array of any, then depending of type of it's elements same type is expected as the first argument.
Parameters
element
any, depends of type of elements in second argument.
array
array, vector or string.
start_index
integer index beginning from 1; from where to start search.
step
integer the step between elements to be tested; default is 1 (every element)
Return Values
An integer index in array starting from 1; if not found returns zero.
Errors
Table 24.67. Errors signalled by position
| SQLState | Error Code | Error Text | Description |
|---|---|---|---|
| 22003 | SR061 | position: expects a vector whose length is divisible by [step], not of length [actual len]. | |
| 22023 | SR000 | position expects an array or vector, not an arg of type %d. |
Examples
Example 24.269. Simple examples
SQL> select position ('b', 'Abacus');
callret
VARCHAR
_______________________________________________________________________________
2
1 Rows. -- 3 msec.
SQL> select position (2, vector ('Primero', null, 2, 3.333));
callret
VARCHAR
_______________________________________________________________________________
3
1 Rows. -- 4 msec.
SQL> select position ('a', 'babaab', 1, 2);
^--- matches 5-th as other 'a' are skipped as step is 2 chars
callret
INTEGER
_______________________________________________________________________________
5
1 Rows. -- 4 msec.