lpad/rpad函式

不一樣的天空w發表於2017-08-17
一、Lpad函式

lpad函式將左邊的字串填充一些特定的字元其語法格式如下:lpad(string,n,[pad_string])

string:字元或者引數

n:字元的長度,是返回的字串的數量如果這個數量比原字串的長度要短,lpad函式將會把字串擷取成從左到右的n個字元;

pad_string:可選引數這個字串是要貼上到string的左邊,若這個引數未寫,lpad函式將會在string的左邊貼上空格。

select lpad('tech', 7) from dual; 將返回' tech'
select lpad('tech', 2) from dual; 將返回'te'
select lpad('tech', 8, '0') from dual; 將返回'0000tech'
select lpad('tech on the net', 15, 'z') from dual; 將返回'tech on the net'
select lpad('tech on the net', 16, 'z') from dual; 將返回'ztech on the net'

SQL> select lpad('tech', 7) from dual;

LPAD('T
-------
   tech

SQL> select lpad('tech', 2) from dual;

LP
--
te

SQL> select lpad('tech', 8, '0') from dual;

LPAD('TE
--------
0000tech

SQL> select lpad('tech on the net', 15, 'z') from dual;

LPAD('TECHONTHE
---------------
tech on the net

SQL> select lpad('tech on the net', 16, 'z') from dual;

LPAD('TECHONTHEN
----------------
ztech on the net

SQL>


二、Rpad函式

rpad函式將右邊的字串填充一些特定的字元其語法格式如下:rpad(string,n,[pad_string])

string:字元或者引數

n:字元的長度,是返回的字串的數量,如果這個數量比原字串的長度要短,lpad函式將會把字串擷取成從左到右的n個字元;

pad_string:可選引數,這個字串是要貼上到string的右邊,如果這個引數未寫,lpad函式將會在string的右邊貼上空格。

select rpad('tech', 7) from dual; 將返回' tech'
select rpad('tech', 2) from dual; 將返回'te'
select rpad('tech', 8, '0') from dual; 將返回'tech0000'
select rpad('tech on the net', 15, 'z') from dual; 將返回'tech on the net'
select rpad('tech on the net', 16, 'z') from dual; 將返回'tech on the netz'

SQL> select rpad('tech', 7) from dual;

RPAD('T
-------
tech

SQL> select rpad('tech', 2) from dual;

RP
--
te

SQL> select rpad('tech', 8, '0') from dual;

RPAD('TE
--------
tech0000

SQL> select rpad('tech on the net', 15, 'z') from dual;

RPAD('TECHONTHE
---------------
tech on the net

SQL> select rpad('tech on the net', 16, 'z') from dual;

RPAD('TECHONTHEN
----------------
tech on the netz

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31397003/viewspace-2143737/,如需轉載,請註明出處,否則將追究法律責任。

相關文章