1 2 Previous Next 17 Replies Latest reply: May 16, 2008 11:07 AM by MichaelS RSS

    Alignment in PL/SQL

    626082
      Hi everyone,

      Could you tell me how to do left and right alignment at displayed text outputs in PL/SQL, please?One more thing, Why leaving space doesn't have any effect at the output?
           DBMS_OUTPUT.PUT_LINE('    ' || Something||',');
      AND How can I leave a blank line?

      Thanks.
        • 1. Re: Alignment in PL/SQL
          639412
          Hi,

          Use lpad or rpad functions.
          • 2. Re: Alignment in PL/SQL
            602725
            Hi,

            U can use LPAD and RPAD features in PL/SQL to perform alignments

            Regards
            Nagesh
            • 3. Re: Alignment in PL/SQL
              626082
              Thanks for the replies, but how about leaving a blank line and leaving a space at the beginning of the word. Just like the example in my first message?
              • 4. Re: Alignment in PL/SQL
                593465
                Hi
                You leave a blank space using
                dbms_output_put_line('');



                Thanks
                • 5. Re: Alignment in PL/SQL
                  593465
                  to print space in beging use this
                  set serveroutput on format wrapped

                  i think this will solve your problem


                  thanks
                  • 6. Re: Alignment in PL/SQL
                    626082
                    I didn't really understand how to use LPAD or RPAD. Could you show me how to use it on this example please?For example how can I get the output aligned to the right?
                    DECLARE 
                         Date_of_today DATE; 
                    BEGIN 
                         Date_of_today := SYSDATE; 
                        DBMS_OUTPUT.PUT_LINE('The date is: ' || Date_of_Today);  
                    END; 
                    /
                    Thanks
                    • 7. Re: Alignment in PL/SQL
                      589667
                      I didn't really understand how to use LPAD or RPAD.
                      test@ORA10G>
                      test@ORA10G> DECLARE
                        2    n_maxlen  number := 30;
                        3  BEGIN
                        4    for i in 1..n_maxlen - length(to_char(sysdate,'mm/dd/yyyy')) + 1
                        5    loop
                        6      DBMS_OUTPUT.PUT_LINE('The date is: ' || lpad(rpad(to_char(sysdate,'mm/dd/yyyy'),n_maxlen-i+1,'*'),30,'*'));
                        7    end loop;
                        8  END;
                        9  /
                      The date is: 05/16/2008********************
                      The date is: *05/16/2008*******************
                      The date is: **05/16/2008******************
                      The date is: ***05/16/2008*****************
                      The date is: ****05/16/2008****************
                      The date is: *****05/16/2008***************
                      The date is: ******05/16/2008**************
                      The date is: *******05/16/2008*************
                      The date is: ********05/16/2008************
                      The date is: *********05/16/2008***********
                      The date is: **********05/16/2008**********
                      The date is: ***********05/16/2008*********
                      The date is: ************05/16/2008********
                      The date is: *************05/16/2008*******
                      The date is: **************05/16/2008******
                      The date is: ***************05/16/2008*****
                      The date is: ****************05/16/2008****
                      The date is: *****************05/16/2008***
                      The date is: ******************05/16/2008**
                      The date is: *******************05/16/2008*
                      The date is: ********************05/16/2008
                      
                      PL/SQL procedure successfully completed.
                      
                      test@ORA10G>
                      test@ORA10G>
                      HTH,
                      pratz
                      • 8. Re: Alignment in PL/SQL
                        170207
                        Leaving a blank line:
                        SQL> BEGIN
                          2    DBMS_Output.Put_Line(CHR(10));
                          3    DBMS_Output.Put_Line('Line');
                          4    DBMS_Output.Put_Line(CHR(10));
                          5    DBMS_Output.Put_Line('Line');
                          6    DBMS_Output.Put_Line(CHR(10));
                          7    DBMS_Output.Put_Line('Line');
                          8  END;
                          9  /
                        
                        
                        Line
                        
                        
                        Line
                        
                        
                        Line
                        
                        PL/SQL procedure successfully completed.
                        DBMS_Output and/or SQL*Plus trim blanks before displaying. So it seems not possible to get the right-aligned output unless the padding characted is not a space.

                        Regards,
                        Dima
                        • 9. Re: Alignment in PL/SQL
                          626082
                          Thank you for the reply is this the only way do it?I mean by a loop.
                          • 10. Re: Alignment in PL/SQL
                            589667
                            Thank you for the reply is this the only way do it?I mean by a loop.
                            The rpad and the lpad functions are doing the right and left padding respectively.

                            The loop is to show you how the display string changes as the number of characters to pad, changes.

                            You don't have to use a loop to right pad or left pad.

                            pratz
                            • 11. Re: Alignment in PL/SQL
                              290833
                              2 DBMS_Output.Put_Line(CHR(10));
                              That outputs TWO empty lines. Why not just:
                              SQL> exec dbms_output.put_line('');


                              PL/SQL procedure successfully completed.
                              DBMS_Output and/or SQL*Plus trim blanks before
                              displaying. So it seems not possible to get the
                              right-aligned output unless the padding characted is
                              not a space.
                              Bollocks.
                              SQL> exec dbms_output.put_line(lpad('hello', 80, ' '));
                                                                                                         hello

                              PL/SQL procedure successfully completed.
                              cheers,
                              Anthony
                              • 12. Re: Alignment in PL/SQL
                                626082
                                Thank you for the replies.

                                So how many characters in 1 line?

                                if I use this is it going to be correctly aligned to the right?
                                SQL> DECLARE
                                  2  BEGIN
                                  3  DBMS_OUTPUT.PUT_LINE(lpad('The date is: ',30) || lpad(to_char(sysdate,
                                'mm/dd/yyyy'),10));
                                  4  END;
                                  5  /
                                                 The date is: 05/17/2008
                                
                                PL/SQL procedure successfully completed.
                                I tried this but I got different output then yours.
                                SQL> exec dbms_output.put_line(lpad('hello', 80, ' '));
                                
                                hello
                                
                                PL/SQL procedure successfully completed.
                                
                                SQL> exec dbms_output.put_line(rpad('hello', 80, ' '));
                                hello
                                
                                PL/SQL procedure successfully completed.
                                This is worked
                                SQL> exec dbms_output.put_line(lpad('hello', 75, ' '));
                                                                                                      hello
                                
                                PL/SQL procedure successfully completed.
                                Message was edited by:
                                BoneXXX
                                • 13. Re: Alignment in PL/SQL
                                  589667
                                  if I use this is it going to be correctly aligned to the right?
                                  Sorry - I am unable to understand your question. Don't you see the alignment in your output ?
                                  SQL> DECLARE
                                  2  BEGIN
                                  3  DBMS_OUTPUT.PUT_LINE(lpad('The date is: ',30) || lpad(to_char(sysdate, 'mm/dd/yyyy'),10));
                                  4  END;
                                  5  /
                                  The date is: 05/17/2008
                                  PL/SQL procedure successfully completed.
                                  pratz
                                  • 14. Re: Alignment in PL/SQL
                                    626082
                                    I mean is it fully aligned to the right? it looks like it is at the middle of the line.
                                    1 2 Previous Next