lpad与rpad函数

函数介绍

lpad函数是Oracle数据库函数,lpad函数从左边对字符串使用指定的字符进行填充。

从其字面意思也可以理解,l是left的简写,pad是填充的意思,所以lpad就是从左边填充的意思。

语法

lpad

语法格式

lpad(string,padded_length,[pad_string])

string

准备被填充的字符串

padded_length

填充之后的字符串长度,也就是该函数返回的字符串长度,如果这个数量比原字符串的长度要短,lpad函数将会把字符串截取成从左到右的n个字符。

pad_string

填充字符串,是个可选参数,这个字符串是要粘贴到string的左边,如果这个参数未写,lpad函数将会在string的左边粘贴空格。

例:lpad('tech', 7); 将返回' tech'
        lpad('tech', 2); 将返回'te'
        lpad('tech', 8, '0'); 将返回'0000tech'
        lpad('tech on the net', 15, 'z'); 将返回'tech on the net'
        lpad('tech on the net', 16, 'z'); 将返回'ztech on the net'

与lpad函数对应的是rpad函数:

rpad

rpad函数从右边对字符串使用指定的字符进行填充,语法格式与lpad格式相同:

rpad(string,padded_length,[pad_string])

string

准备被填充的字符串

padded_length

字符的长度,也就是返回的字符串的数量,如果这个数量比原字符串的长度要短,rpad函数将会把字符串截取成从左到右的n个字符。

pad_string

填充字符串,是个可选参数,这个字符串是要粘贴到string的右边,如果这个参数未写,lpad函数将会在string的右边粘贴空格。

例: rpad('tech', 7); 将返回' tech'
        rpad('tech', 2); 将返回'te'
        rpad('tech', 8, '0'); 将返回'tech0000'
        rpad('tech on the net', 15, 'z'); 将返回'tech on the net'
        rpad('tech on the net', 16, 'z'); 将返回'tech on the netz'

猜你喜欢

转载自blog.csdn.net/shizhudufou/article/details/79900252