SQL LPAD 函数

一、SQL LPAD 函数 语法

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

语法1:此语法使用带有 SQL 表列名的 LPAD 函数:

SELECT LPAD(Column_Name, size, lpad_string) AS Alias_Name FROM Table_Name;  

在此语法中,Column_Name 是要填充其值的列的名称,size 是填充后的列值的总长度,lpad_string 是要在左侧添加的字符串。

语法2:此语法使用带有字符集(字符串)的 LPAD 函数:

SELECT LPAD(String, size, lpad_string);  

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

二、SQL LPAD 函数 示例

示例 1:以下 SELECT 查询在 NEW 字符串的左侧添加了三次 # 符号:

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

输出结果为:

###NEW

示例 2:以下 SELECT 查询将“Good”字符串添加到给定原始字符串的左侧:

SELECT LPAD( ' Yiidian', 15 , 'Good');  

输出结果为:

Good Yiidian

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

SELECT LPAD( 8, 5 , 9);  

输出结果为:

99998

示例 4以下 SELECT 查询将 xy 字符串添加到给定字符 z 的左侧:

SELECT LPAD( 'z', 3, 'xy');  

输出结果为:

xyz

示例 5:此示例将 LPAD 函数与 SQL 表一起使用

在此示例中,我们将创建一个新表,该表与 LPAD 函数一起使用。

在 SQL 数据库中创建新表的语法如下:

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 查询将 LPAD 函数与上述 Faculty_Info 表的 Faculty_Last_Name 列一起使用:

SELECT Faculty_Last_Name, LPAD(Faculty_Last_Name, 11, '*' ) AS LPAD_LastName FROM Faculty_Info;  

此 SELECT 语句在每个教员的 Last_Name 左侧添加 * 符号:

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

以下 SELECT 查询将 LPAD 函数与上面 Faculty_Info 表中的 Faculty_Id 大于 1002 的那些学院的 Faculty_First_Name、Faculty_City 和 Faculty_Address 列一起使用:

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

输出结果为:

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

以下 SELECT 查询将 LPAD 函数与上述 Faculty_Info 表的 Faculty_Salary 列一起使用:

SELECT Faculty_Salary, LPAD(Faculty_Salary, 8, 5 ) AS LPAD_Salary FROM Faculty_Info;  

输出结果为:

Faculty_Salary LPAD_Salary
20000 55520000
38000 55538000
45000 55545000
42000 55542000
28000 55528000
35000 55535000

热门文章

优秀文章