0

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;
William Robertson
  • 15,273
  • 4
  • 38
  • 44
Adolfok3
  • 5
  • 1
  • 5
  • Why `char` array? Ordinary strings are generally easier to work with. Also PL/SQL has no double-bracket syntax requirement after `if`, because it has `then`. You can just write `if x = length(name) then`. Although you could probably work out `length(name)` once at the start. – William Robertson Sep 01 '17 at 23:05

1 Answers1

1

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);
Tony Andrews
  • 129,880
  • 21
  • 220
  • 259