The TRIM method removes one or more elements from the end of a collection.

Syntax

The syntax for the TRIM method is:

<collection>.TRIM[(<count>)]

Where:

collection is the name of a collection.

count is the number of elements removed from the end of the collection. EDB Postgres Advanced Server returns an error if count is less than 0 or greater than the number of elements in the collection.

Example

This example uses 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, there were five elements in the collection. After the TRIM method is invoked, the collection contains four elements.

You can also specify the number of elements to remove from the end of the collection using 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, there were five elements in the collection. After the TRIM method is invoked, the collection contains three elements.