0

Im building this string:

text_ := 'NAME           LNAME         AGE'||chr(13)||chr(10);

FOR person_ IN list LOOP

text_ := text_ ||person_.name||'     '||person_.lname||'      '||person_.age||chr(13)||chr(10);

END LOOP;

Loop will result in:

NAME           LNAME            AGE
name           lname            18
namename            lname           18

Desired result:

NAME           LNAME            AGE
name           lname            18
namename       lname            18

Is there a way to tell a string to fill itself with whitespace if the value within it doesn't reach a fixed value?

Using RPAD:

  text_ := text_||rpad('Name', 30)||rpad('Date', 12)||chr(13)||chr(10)||chr(13)||chr(10);

  FOR op_ IN get_op LOOP
    text_ := text_||rpad(op_.NAME, 30)||rpad(op_.DATE,12)||chr(13)||chr(10);
  END LOOP;

Results in:

Name                          Date      

UserUserUser                    2014-04-03                                                   
UserUser                      2014-04-04
UserUser                      2014-04-03
3
  • Is this the exact output in 'Results in:'. Did you copy paste it? First line of date has 2 extra spaces while Name is 4 characters longer. Commented Apr 4, 2014 at 14:52
  • No it's not an exact copy paste. But it looks like that. Commented Apr 7, 2014 at 6:30
  • As commented in answer below I think it looks like that because you use a proportional typeface. Open an editor (Notepad) , choose a monospace font (Lucida Console) and copy-paste output into editor. Commented Apr 7, 2014 at 7:32

1 Answer 1

1

Use rpad

text_ := text_ ||rpad(person_.name,40)||rpad(person_.lname,40)||rpad(person_.age,40)||chr(13)||chr(10);

Test Case

DECLARE
   t VARCHAR2(1000);
BEGIN
   t := RPAD('NAME',10)||RPAD('LNAME',10)||RPAD('AGE',10)||chr(13)||chr(10);
   FOR i IN 8..11
   LOOP
      t := t ||RPAD('Name'||i,10)||RPAD('lname'||i,10)||RPAD(i,10)||chr(13)||chr(10);
   END LOOP;
   dbms_output.put_line(t);
END;

Output:

NAME      LNAME     AGE       
Name8     lname8    8         
Name9     lname9    9         
Name10    lname10   10        
Name11    lname11   11 
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't work, everything still get shifted to the left.
Are you using a monospace font to display the results?
Actually you're right copy/pasting it gives a correct layout in notepad. I'm sending my text_ into a mail function that mail it to my mail. Somewhere there's the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.