PRO_ELEMENT function
- Last Updated: April 26, 2018
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
PRO_ELEMENT function
Extracts one or more elements from an array
column and returns the NVARCHAR or VARCHAR string
between the specified positions, including any internal separator
characters and any internal escape characters.
Syntax
|
Parameters
- array_style_expression
-
A string of data type
VARCHARorCHAR, with a semicolon ( ; ) separating each element of the array. - start_position
-
The numeric position in the string marking the beginning of the element
PRO_ELEMENTis to extract. - end_position
-
The numeric position in the string marking the end of the element to be extracted.
Notes
- The array_style_expression must
be data type
NVARCHAR,VARCHAR, orCHAR. - The returned string does not include the leading separator of the first element, or the trailing separator ( ; ) of the last element.
- Even when extracting only one element, the escape characters are included in the result.
- You must invoke
PRO_ARR_DESCAPEto remove any escape characters. - If the expression evaluates to
NULL, the result isNULL. - When accessing a sequence of multiple array elements if all the chosen
array elements are
NULL, then, the value ofPRO_ELEMENT()isNULL. As any expression involvingNULLevaluates toNULL, you should be careful when using thePRO_ELEMENTin this way when all the referenced array elements may beNULL. As an example, for a column named "col1" is an array of five elements of Date datatype, where, col1[2]..col5] areNULL, then, the value ofPRO_ELEMENT("col1", 2, 5)isNULL. - When the Authorized Data Truncation (ADT) is set to ON or OUTPUT , using the
PRO_ELEMENTfor var array might return the error “invalid array element reference”. To understand this better, refer Example 2 of this topic. - If an application wants to obtain a sequence of
'?'values for a sequence of array elements each equal toNULL, use an expression combining the individual array elements. For example:PRO_ELEMENT("col1",2,2) || ';' || PRO_ELEMENT("col1",3,3) || ';' || PRO_ELEMENT("col1",4,4) || ';' || PRO_ELEMENT("col1",5,5) - While invoking
PRO_ELEMENT, ifStart=End, and the argument isNULLthe result is (that is, the referenced array element isNULL) the result is'?'. For example, in the following function call, ifsales_history[2]isNULL, then thePRO_ELEMENTreturns‘?’.PRO_ELEMENT(sales_history, 2, 2)
Example 1
The
following example returns the string 'bb':
|
The next example returns the string 'aa;bb':
|
This example returns the string 'aa~;aa':
|
Example 2
Consider a var array ( cVar VARCHAR(5)
VARARRAY[2]) into which ABL users insert the data (‘123456789012345’;’1234567890’).
Now, SQL users execute the SQL query select
pro_elemenet( cvar,2,2) from table when the Authorized Data Truncation (ADT) is
set to on/output. The expected output with the current SQL behavior is to throw the error
: invalid array element reference.
The reason for this behavior is that in case of a var array, while converting it into to string format, the truncation should happen on the entire array rather than on each element. The VARARRAY data type allows the size of an individual element value to exceed its declared size as long as the total size of the array is smaller than the array's SQL width.
So, for a var array, each element will be read incrementally until the entire data for the element is read. While doing so, if the total array length is exhausted, further data will not be read and the string produced so far will be returned.
In the above example, the first element is read first even though the
second element is required and the array length is exhausted. So, the returned string does
not have a reference to second element and it returns invalid array
element reference.
This is an expected behavior. However, to get the second element, SQL
users can and should use the subscript notation: my_array[2].