SQL RPAD 函数
一、SQL RPAD 函数 语法
SQL RPAD 函数 将符号或字符串添加到原始字符串的右侧。在结构化查询语言中,我们可以在字符串和数字上使用这个函数。
语法1:此语法使用 RPAD 函数和 SQL 表的列名:
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 是要添加到给定原始字符串右侧的字符串。
二、SQL RPAD 函数 示例
示例 1:以下 SELECT 查询在 NEW 字符串的右侧添加了三次“#”符号:
SELECT RPAD( 'NEW', 6, '#');
输出结果为:
NEW###
示例 2:以下 SELECT 查询将“Site”字符串添加到给定原始字符串的右侧:
SELECT RPAD( 'Yiidian ', 15 , 'Site');
输出结果为:
Yiidian Site
示例 3:以下 SELECT 查询在给定数字 8 的右侧添加了四次“9”数字:
SELECT RPAD( 8, 5 , 9);
输出结果为:
89999
示例 4:以下 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)
);
以下 CREATE 语句创建Faculty_Info表:
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 语句将大学 Faculties 的记录插入到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 查询将 RPAD 函数与上述 Faculty_Info 表的 Faculty_Last_Name 列一起使用:
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 |
热门文章
优秀文章