溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何理解lpad和rpad函數(shù)

發(fā)布時間:2021-11-12 15:45:39 來源:億速云 閱讀:186 作者:柒染 欄目:關系型數(shù)據(jù)庫

這篇文章將為大家詳細講解有關如何理解lpad和rpad函數(shù),文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

一、Lpad函數(shù)

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

string:字符或者參數(shù)

n:字符的長度,是返回的字符串的數(shù)量,如果這個數(shù)量比原字符串的長度要短,lpad函數(shù)將會把字符串截取成從左到右的n個字符;

pad_string:可選參數(shù),這個字符串是要粘貼到string的左邊,若這個參數(shù)未寫,lpad函數(shù)將會在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函數(shù)

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

string:字符或者參數(shù)

n:字符的長度,是返回的字符串的數(shù)量,如果這個數(shù)量比原字符串的長度要短,lpad函數(shù)將會把字符串截取成從左到右的n個字符;

pad_string:可選參數(shù),這個字符串是要粘貼到string的右邊,如果這個參數(shù)未寫,lpad函數(shù)將會在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

關于如何理解lpad和rpad函數(shù)就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI