Search code examples
arraysoracle-databaseplsqlcharvarchar2

PLSQL - Convert Char Array To Varchar2


I have a function that remove last character from a varchar2, but I needed to convert it to char array first. Now I cant find anything to convert it back to varchar2.

My function:

DECLARE
    TYPE CHAR_ARRAY IS TABLE OF CHAR(1) INDEX BY PLS_INTEGER;
    NAME VARCHAR2(100) := '&vname';
    NAME_CHAR CHAR_ARRAY;
BEGIN
    FOR X IN 1..LENGTH(NAME) LOOP
        IF((X = LENGTH(NAME))) THEN
            NAME_CHAR(X) := '';
        ELSE
            NAME_CHAR(X) := SUBSTR(NAME, X , 1);
        END IF;
    END LOOP;
    -- Now I need to convert it back to varchar2
END;

Solution

  • How about:

    name := '';
    for x in 1..name_char.count loop
      name := name || name_char(x);
    end loop;
    

    Though why you would do any of this eludes me! If I wanted to remove the last character from a string I would do this:

    name := substr (name, 1, length(name)-1);