Chapter 13. Array Support
Index:
- Declaration of Array Types
- Array Functions
- Array sample
Declaration of Array Types
An array data type is named by appending square brackets ([]) to the data type name of the array elements, and all the elements are of the same data type. The array subscript numbers are written within square brackets. It uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].
DECLARE v_pay_by_month decimal(12,2)[12];
set v_pay_by_month[2]=123.45;
select v_pay_by_month[2] as feb;
select v_pay_by_month;
Array Functions
- CAST(expression AS array): converts expression to array.
For instance, cast('{20000, 25000, 25000, 25000}' as array).
- CONVERT(expression, data_type): converts expression to another data type (json and jsonb).
For instance, convert('{{"breakfast", "consulting"}, {"meeting", "lunch"}}', array).
- to_array(expression): Returns the value as array.
For instance, to_array('{{"breakfast", "consulting"}, {"meeting", "lunch"}}').
- array_assign(anyArrayElement,expression): Returns an array with supplied value.
For instance, array_assign(to_array('{{"breakfast", "consulting"}, {"meeting", "lunch"}}')[2],to_array('{"lunch", "meeting"}')).
- array_ndims(anyArrayElement): Returns the number of dimensions of the array.
- SPLIT(string1, string2): split string1 according to delimiter string2, and return an String[] object (Types.ARRAY). Special SPLIT(expression,'') will return strings which contains only Letter and Digit. Special SPLIT(expression,null) will split string into length=1 strings, which is only Letter or Digit.
- CINT(arrayExpr): converts an array object to a int[] object (Types.ARRAY).
- CLNG(arrayExpr): converts an array object to a long[] object (Types.ARRAY).
- CDBL(arrayExpr): converts an array object to a double[] object (Types.ARRAY).
- VAL(arrayExpr): converts an array object to an Object[] object (Types.ARRAY) which contains numeric values.
Array sample
SELECT cast('{20000, 25000, 25000, 25000}' as array);
SELECT convert('{{"breakfast", "consulting"}, {"meeting", "lunch"}}', array);
select to_array('{{"meeting", "lunch"}, {"training", "presentation"}}');
select to_array('{{"meeting", "lunch"}, {"training", "presentation"}}')[1][2];
select array_assign(to_array('{{"breakfast", "consulting"}, {"meeting", "lunch"}}')[2],to_array('{"lunch", "meeting"}'));
Copyright © 2003-2019 Heng Xing Tian Tai Lab | All Rights Reserved. |