SQL RPAD函数

SQL RPAD函数

这个字符串函数在原始字符串的右侧添加符号或字符串。在结构化查询语言中,我们可以在字符串和数字上使用这个函数。

RPAD字符串函数的语法

语法1: 这个语法使用SQL表的列名和RPAD函数:

SELECT RPAD(Column_Name, size, rpad_string) AS Alias_Name FROM Table_Name;

在这个语法中,Column_Name是需要填充的列的名称,size是填充后列值的总长度,rpad_string是要添加在右侧的字符串。

语法2: 这个语法使用RPAD函数和字符集合(字符串):

SELECT RPAD(String, size, rpad_string);

在这个语法中,String是要填充的值,size是填充后列值的总长度,rpad_string是要添加到给定原始字符串右侧的字符串。

RPAD字符串函数的示例

示例1:示例1: 以下SELECT查询将“#”符号添加到NEW字符串的右侧三次:

SELECT RPAD( 'NEW', 6, '#');

输出:

NEW###

示例 2: 下面的SELECT查询将“站点”字符串添加到给定原始字符串的右侧:

SELECT RPAD( 'JavaTpoint ', 15 , 'Site');

输出:

JavaTpoint Site

示例3: 以下SELECT查询将数字’9’添加到给定的数字8的右侧四次:

SELECT RPAD( 8, 5 , 9);

输出:

89999

示例3: 下面的SELECT查询将字符串’yz’添加到给定字符x的右侧:

SELECT RPAD( 'x', 3, 'yz');

输出:

xyz

示例 5:这个例子使用RPAD函数与SQL表

在这个例子中,我们将创建一个新的表,该表将与RPAD函数一起使用。

CREATE TABLE table_name
(
1st_Column Data Type (character_size of 1st Column),  
2nd_Column Data Type (character_size of the 2nd column ),  
3rd_Column Data Type (character_size of the 3rd column),  
...  

Nth_Column Data Type (character_size of the Nth column)
);  

以下是创建表 Faculty_Info 的 CREATE 语句:

CREATE TABLE Faculty_Info
(
Faculty_ID INT NOT NULL PRIMARY KEY,  
Faculty_First_Name VARCHAR (100),  
Faculty_Last_Name VARCHAR (100),  
Faculty_Dept_Id INT NOT NULL,
Faculty_Address Varchar(120),
Faculty_City Varchar (80),
Faculty_Salary INT 
);

下面的INSERT查询将大学教职员工的记录插入到 Faculty_Info 表中:

INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1001, arush, sharma, 4001, aman vihar, delhi, 20000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1002, bulbul, roy, 4002, nirman vihar, delhi, 38000 );
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1004, saurabh, sharma, 4001, sector 128, mumbai, 45000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1005, shivani, singhania, 4001, vivek vihar, kolkata, 42000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1006, avinash, sharma, 4002, sarvodya calony, delhi, 28000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary)VALUES (1007, shyam, besas, 4003, krishna nagar, lucknow, 35000);

以下SELECT语句显示上述插入的记录表中的 Faculty_Info 表:

SELECT * FROM Faculty_Info;
Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Address Faculty_City Faculty_Salary
1001 ARUSH SHARMA 4001 AMAN VIHAR DELHI 20000
1002 BULBUL ROY 4002 NIRMAN VIHAR DELHI 38000
1004 SAURABH ROY 4001 SECTOR 128 MUMBAI 45000
1005 SHIVANI SINGHANIA 4001 VIVEK VIHAR KOLKATA 42000
1006 AVINASH SHARMA 4002 SARVODYA CALONY DELHI 28000
1007 SHYAM BESAS 4003 KRISHNA NAGAR LUCKNOW 35000

下面的SELECT查询在上面的Faculty_Info表的Faculty_Last_Name列中使用了RPAD函数:

SELECT Faculty_Last_Name, RPAD(Faculty_Last_Name, 11, '*' ) AS RPAD_LastName FROM Faculty_Info;

这个SELECT语句在每个教职工的Last_Name右侧添加了*符号:

输出:

Faculty_Last_Name RPAD_LastName
SHARMA sharma*****
ROY roy********
ROY roy********
SINGHANIA singhania**
SHARMA sharma*****
BESAS besas******

以下SELECT查询使用RPAD函数,它将Faculty_Info表中Faculty_Id大于1002的教职员工的Faculty_First_Name,Faculty_City和Faculty_Address列连接起来:

SELECT Faculty_Id, RPAD(Faculty_First_Name, 10, '@' ), RPAD(Faculty_Address, 17, '#' ), RPAD(Faculty_City, 9 , '$') FROM Faculty_Info WHERE Faculty_Id >1002;

输出:

Faculty_Id RPAD(Faculty_First_Name) RPAD(Faculty_Address) RPAD(Faculty_City)
1004 saurabh@@@ Sector 128####### mumbai$$$
1005 shivani@@@ vivek vihar###### kolkata$$
1006 avinash@@@ sarvodya calony## delhi$$$$
1007 @@@@@shyam krishna nagar#### lucknow$$

以下SELECT查询使用RPAD函数与上述Faculty_Info表中的Faculty_Salary列:

SELECT Faculty_Salary, RPAD(Faculty_Salary, 8, 0 ) AS RPAD_Salary FROM Faculty_Info;

这个SELECT语句将每位员工的薪水右边加上5:

输出结果:

Faculty_Salary RPAD_Salary
20000 20000000
38000 38000000
45000 45000000
42000 42000000
28000 28000000
35000 35000000

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程