The TRIM method removes an element or elements from the end of a collection.
The syntax for the TRIM method is as follows:
collection.TRIM[(count)]
collection is the name of a collection.
count is the number of elements removed from the end of the collection. PolarDB for PostgreSQL(Compatible with Oracle) will return an error if count is less than 0 or greater than the number of elements in the collection.
The following example demonstrates how to use the TRIM method to remove an element from the end of a collection:
DECLARE
TYPE sparse_arr_typ IS TABLE OF NUMBER;
sparse_arr sparse_arr_typ := sparse_arr_typ(-100,-10,0,10,100);
BEGIN
DBMS_OUTPUT.PUT_LINE('COUNT: ' || sparse_arr.COUNT);
sparse_arr.TRIM;
DBMS_OUTPUT.PUT_LINE('COUNT: ' || sparse_arr.COUNT);
END;
COUNT: 5
COUNT: 4
COUNT indicates that before the TRIM method, the collection has five elements. After the TRIM method was invoked, the collection contains four elements.
You can also specify the number of elements to remove from the end of the collection with the TRIM method:
DECLARE
TYPE sparse_arr_typ IS TABLE OF NUMBER;
sparse_arr sparse_arr_typ := sparse_arr_typ(-100,-10,0,10,100);
v_results VARCHAR2(50);
BEGIN
DBMS_OUTPUT.PUT_LINE('COUNT: ' || sparse_arr.COUNT);
sparse_arr.TRIM(2);
DBMS_OUTPUT.PUT_LINE('COUNT: ' || sparse_arr.COUNT);
FOR i IN sparse_arr.FIRST .. sparse_arr.LAST LOOP
IF sparse_arr(i) IS NULL THEN
v_results := v_results || 'NULL ';
ELSE
v_results := v_results || sparse_arr(i) || ' ';
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Results: ' || v_results);
END;
COUNT: 5
COUNT: 3
Results: -100 -10 0
COUNT indicates that before the TRIM method, the collection has five elements. After the TRIM method was invoked, the collection contains three elements.