Search code examples
plsqloracle-sqldeveloper

Convert integer to varchar and assign to variable pl/sql


I am trying to assign a convert an INTEGER value into STRING and assign that to another variable, then use that variable later to display the value in text. Something is not going right and I'm getting an "String length constraints must be in range (1-32767)" error.

CREATE OR REPLACE PROCEDURE 
numdocs(usr_input IN VARCHAR)
AS
    user_name VARCHAR(20);
    num_docs INTEGER;
    numdoctext VARCHAR(20);
    num_converted STRING;
BEGIN
    user_name := usr_input;
    SELECT COUNT(*)
    INTO num_docs FROM UserPermissions WHERE 
    UserName = user_name;
    SELECT to_char(to_date(num_docs,'j','jsp')) INTO num_converted from dual;
    dbms_output.put_line('converted is ' || num_converted);
    IF num_docs = 0 THEN dbms_output.put_line('No documents for ' || user_name);
    ELSIF num_docs > 0 THEN dbms_output.put_line(user_name || ' has permission to view ' || num_converted || ' documents.');
    END IF;
END;
/

Solution

  • Based on your last comment:

    SELECT to_char( to_date( :number, 'j' ), 'jsp' ) FROM DUAL; was the example I was going off of to convert the integer to a word.

    And the code you actually display in the question, it looks like the issue is the placement of your closing parentheses in the code. You put then both at the end of the "to_char", while the example has one after the "to_date".

    So your select should read:

    SELECT to_char(to_date(num_docs,'j'),'jsp') INTO num_converted from dual;