As their name implies, array functions allow you to work with arrays. One important thing to keep in mind is that in the Data Virtuality Server, array indexes start at position 1. This means that my_array[1] will return the first element in the array, whereas my_array[0] will return NULL.

ARRAY_GET

This function returns the element of an array at the given index as object.

Syntax

ARRAY_GET(<array>, <index>)
SQL
  • <array> is the array you want to extract an element from, and it has to be of type object;
  • <index> is the position of the element to be extracted (starting from position 1), and it must be of type integer.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');
SELECT ARRAY_GET(favourite_animals, 2);
END
--Outcome: Goat
SQL

ARRAY_LENGTH

This function returns the length for a given array as integer.

Syntax

ARRAY_LENGTH(<array>)
SQL
  • <array> is the array the length of which you want to determine, and it is of type object.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');
SELECT ARRAY_LENGTH(favourite_animals);
END
--Outcome: 5
SQL

ARRAY_ADD

This function adds an array or a single value to a given array.

Syntax

newArray = ARRAY_ADD(<array>, <array or single_value>)
SQL
  • <array> is the array you want to add an element to, and it has to be of type object;
  • <array or single_value> is a single element or an array of elements that you want to add to an array, and both have to be of the same type that the elements in the given array are of;
  • An exception will be thrown if the given parameters mismatch the required types;
  • The function returns a new, merged array of type object.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose');

SELECT ARRAY_ADD(favourite_animals, 'sloth');
--Outcome: [penguin, goat, goose, sloth]

SELECT ARRAY_ADD(favourite_animals, ('sloth', 'mule'));
--Outcome: [penguin, goat, goose, sloth, mule]
END
SQL

ARRAY_SUB

This function returns an array with the elements of the given array for the given amount starting at the given position.

Syntax

ARRAY_SUB(<array>, <start>, <count>)
SQL
  • <array> is the array you want to extract subelements from, and it has to be of type object;
  • <start> is the position in the array to start from (please remember that the first position is position 1!), and it has to be of type integer. If <start> exceeds the length of the array, NULL is returned;
  • <count> is the number of elements you want to extract from the array, starting from <start>. It has to be of type integer and needs to be a positive number. If <count> exceeds the remaining elements after <start>, the function will return all elements it finds till the end is reached;
  • An exception will be thrown if the given parameters mismatch the required types;
  • The function returns a new, merged array of the type object.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');

SELECT ARRAY_SUB(favourite_animals, 3, 2);
--Outcome: [goose, sloth]
SELECT ARRAY_SUB(favourite_animals, 3, 10);
--Outcome: [goose, sloth, mule]
SELECT ARRAY_SUB(favourite_animals, 7, 10);
--Outcome: NULL
SELECT ARRAY_SUB(favourite_animals, 0, 5);
--Outcome: NULL
END
SQL

ARRAY_IN

This function determines whether the given value matches an element in the array or not. Please note that it is case-sensitive.

Syntax

ARRAY_IN(<array>, <value>)
SQL
  • <array> is the array you want to check for a subelement, and it has to be of type object;
  • <value> is the subelement you want to know of if it is present in the array; it can be of any type, but should be of the same type the elements in the array are of (e.g. 1 won't match '1'), and it is case-sensitive;
  • The function returns TRUE if a match has been found; otherwise, it returns FALSE.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');

SELECT ARRAY_IN(favourite_animals, 'goat');
--Outcome: TRUE
SELECT ARRAY_IN(favourite_animals, 'Goat');
--Outcome: FALSE
SELECT ARRAY_IN(favourite_animals, 'penguin, goat');
--Outcome: FALSE
SELECT ARRAY_IN(favourite_animals, ('penguin, goat'));
--Outcome: FALSE
END
SQL

ARRAY_LIKE

This function performs a LIKE search on an array and determines whether a matching element was found or not. Please note that it is case-sensitive.

Syntax

ARRAY_LIKE(<array>, <like-string>)
SQL
  • <array> is the array you want to perform the search on, and it has to be of type object;
  • <like-string> is the string you want to look for in the array; it has to be of type string and is case-sensitive. Please note that you would use the <like-string> just like you would use it in a WHERE LIKE statement;
  • The function returns TRUE if a match has been found; otherwise, it returns FALSE.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');

SELECT ARRAY_LIKE(favourite_animals, 'peng%');
--Outcome: TRUE
SELECT ARRAY_LIKE(favourite_animals, 'penguin');
--Outcome: TRUE
SELECT ARRAY_LIKE(favourite_animals, 'peng');
--Outcome: FALSE
SELECT ARRAY_LIKE(favourite_animals, 'Peng%');
--Outcome: FALSE

DECLARE object favourite_numbers = array(1, 3, 5);

SELECT array_like(favourite_numbers, '1');
--Outcome: TRUE
END
SQL

ARRAY_LIKE_REGEX

This function performs a RegEx search on an array and determines whether a matching element was found or not.

Syntax

ARRAY_LIKE_REGEX(<array>, <regex-string>)
SQL
  • <array> is the array you want to perform the search on, and it has to be of type object;
  • <regex-string> is the regular expression you want to check against the array's elements, and it has to be of type string;
  • This function returns TRUE if a match has been found; otherwise, it returns FALSE.

Usage

BEGIN
DECLARE OBJECT favourite_animals = ARRAY('penguin', 'goat', 'goose', 'sloth', 'mule');

SELECT ARRAY_LIKE_REGEX(favourite_animals, 'penguin');
--Outcome: TRUE
SELECT ARRAY_LIKE_REGEX(favourite_animals, 'pen[a-z]*n');
--Outcome: TRUE
SELECT ARRAY_LIKE_REGEX(favourite_animals, 'pen');
--Outcome: TRUE
SELECT ARRAY_LIKE_REGEX(favourite_animals, 'PENGUIN');
--Outcome: FALSE
SELECT ARRAY_LIKE_REGEX(favourite_animals, '(?i)PENGUIN');
--Outcome: TRUE
END
SQL