Oracle's lpad and rpad functions

1. Usage of lpad() function:

The lpad function fills the string on the left with some specific characters, and its syntax format is as follows:   

     lpad(string,n,[pad_string])

     string: but a character or parameter

     n: The length of the character is the number of the returned string. If this number is shorter than the length of the original string, the lpad function will intercept the string into n characters from left to right;

     pad_string: is an optional parameter. This string is to be pasted to the left of the string. If this parameter is not written, the lpad function will paste a space on the left of the string.

    E.g:

lpad('tech', 7); will return'tech'

lpad('tech', 2); will return'te'

lpad('tech', 8, '0'); will return '0000tech'

lpad('tech on the net', 15,'z'); will return'tech on the net'

lpad('tech on the net', 16,'z'); will return'ztech on the net'

Examples: 

SQL>SELECT lpad(' ', 4*(LEVEL-1)) || ename name, empno, mgr, LEVEL
  2  FROM emp
  3  START WITH mgr IS NULL
  4  CONNECT BY PRIOR empno = mgr
  5  ename != 'BLANKE'
  6  /

2. Usage of rpad() function:

The rpad function fills the string on the right with some specific characters, and its syntax format is as follows:   

     rpad(string,n,[pad_string])

     string: but a character or parameter

     n: The length of the character is the number of the returned string. If this number is shorter than the length of the original string, the lpad function will intercept the string into n characters from left to right;

     pad_string: is an optional parameter. This string is to be pasted to the right of the string. If this parameter is not written, the lpad function will paste a space on the right of the string.

E.g:

rpad('tech', 7); will return'tech'

rpad('tech', 2); will return'te'

rpad('tech', 8, '0'); will return'tech0000'

rpad('tech on the net', 15,'z'); will return'tech on the net'

rpad('tech on the net', 16,'z'); will return'tech on the netz'

Guess you like

Origin blog.csdn.net/joyksk/article/details/81366940