SQL LPAD函数

SQL LPAD函数

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

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是要添加在给定原始字符串左侧的字符串。

LPAD字符串函数示例

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

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

输出:

###NEW

例2: 下面的SELECT查询在给定的原始字符串的左侧添加了“Good”字符串:

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

输出:

Good JavaTpoint

示例 3: 以下SELECT查询在给定数字8的左侧添加了9个数字4:

SELECT LPAD( 8, 5 , 9);

输出:

99998

示例三: 下面的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 
);

以下是将学院教职工的记录插入到Faculty_Info表中的INSERT查询:

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列的LPAD函数:

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查询在上面的Faculty_Info表中使用LPAD函数与Faculty_First_Name、Faculty_City和Faculty_Address列,对那些faculty_Id大于1002的系别进行操作:

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;

这个 SELECT 语句在每个教职员工的工资左边增加了数字 5:

输出:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程