SQL LEFT函数
LEFT字符串函数从结构化查询语言中的原始字符串的左侧检索到给定索引值处的字符。
LEFT字符串函数的语法
语法1: 该语法使用SQL表的列名与LEFT函数一起使用:
SELECT LEFT(Column_Name, Index_position) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要执行LEFT函数的列的名称。
语法3: 此语法使用LEFT函数与字符串:
SELECT LEFT(Original_String, Index_position);
LEFT字符串函数示例
示例1: 以下SELECT查询显示了指定单词左侧的四个带有空格的字符:
SELECT LEFT( ' JAVATPOINT', 4)AS Left_4_characters;
输出:
Left_4_characters
‘ JA’
示例2: 下面的SELECT查询显示给定字符串的前20个字符:
SELECT LEFT( 'JAVATPOINT is a good website ', 20) AS Left_20_characters;
输出:
Left_20_characters
‘JAVATPOINT is a good’
示例3: 以下的SELECT查询显示给定句子的前13个字符:
SELECT LEFT( 'NEW DELHI IS THE CAPITAL OF INDIA', 13) AS Left_13_characters;;
输出:
Left_20_characters
‘NEW DELHI IS’
例子 4: 下面的SELECT查询显示了给定字符串左边的5个字符:
SELECT LEFT( '####98221545###', 5) AS Left_5_characters;
输出:
####9
例子 5: 以下 SELECT 查询展示了指定字符串左边的100个字符:
SELECT LEFT( 'String Functions', 100) AS Left_100_characters;
输出:
Left_100_characters
String Functions
示例8:此示例使用带有结构化查询语言中的表的LEFT函数。
要理解带有SQL的LEFT函数,我们首先必须使用CREATE语句创建SQL表。在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语句创建了 Worker_Info 表:
CREATE TABLE Worker_Info
(
Worker_ID INT NOT NULL PRIMARY KEY,
Worker_First_Name VARCHAR (100),
Worker_Last_Name VARCHAR (100),
Worker_Dept_Id INT NOT NULL,
Worker_Address Varchar(120),
Worker_City Varchar (80),
Worker_Salary INT
);
下面的INSERT查询将学院教师的记录插入到 Worker_Info 表中:
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1001, Arush, Sharma, 4001, Aman Vihar, Delhi, 20000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1002, Bulbul, Roy, 4002, Nirman Vihar, Delhi, 38000 );
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1004, Saurabh, Sharma, 4001, Sector 128, Mumbai, 45000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1005, Shivani, Singhania, 4001, Vivek Vihar, Kolkata, 42000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary) VALUES (1006, Avinash, Sharma, 4002, Sarvodya Calony, Delhi, 28000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Address, Worker_City, Worker_Salary)VALUES (1007, Shyam, Besas, 4003, Krishna Nagar, Lucknow, 35000);
以下SELECT语句显示了上述 Worker_Info 表中插入的记录:
SELECT * FROM Worker_Info;
Worker_Id | Worker_First_Name | Worker_Last_Name | Worker_Dept_Id | Worker_Address | Worker_City | Worker_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 |
查询1: 以下SELECT查询使用上述Worker_Info表中的Worker_First_Name列的LEFT函数:
SELECT Worker_First_Name, LEFT(Worker_First_Name, 3) AS LEFT_3FirstName FROM Worker_Info;
这个SQL语句显示每个工人的名字的前三个字符。
输出:
Worker_First_Name | LEFT_3FirstName |
---|---|
Arush | Aru |
Bulbul | Bul |
Saurabh | Sau |
Shivani | Shi |
Avinash | Avi |
Shyam | Shy |
查询 2: 下面的 SELECT 查询使用了上述 Worker_Info 表的 Worker_Last_Name 列的 LEFT 函数:
SELECT Worker_Last_Name, LEFT(Worker_Last_Name, 2) AS LEFT_2LastName FROM Worker_Info;
这个SQL语句显示了每个工人姓氏的前三个字符。
输出结果:
Worker_Last_Name | LEFT_2LastName |
---|---|
夏尔玛 | Sh |
罗伊 | Ro |
罗伊 | Ro |
辛格尼亚 | Si |
夏尔玛 | Sh |
贝萨斯 | Be |
查询3: 以下SELECT查询使用上述Worker_Info表的Worker_Address列的LEFT函数:
SELECT Worker_Address, LEFT(Worker_Address, 4) AS LEFT_4Address FROM Worker_Info;
此SQL语句显示每个工人地址左边的四个字符。
输出:
Worker_Address | LEFT_4Address |
---|---|
Aman Vihar | Aman |
Nirman Vihar | Nirm |
Sector 128 | Sect |
Vivek Vihar | Vive |
Sarvodya Calony | Sarv |
Krishna Nagar | Kris |
查询4: 下面的SELECT查询使用了上述Worker_Info表中的Worker_City列的LEFT函数:
SELECT Worker_City, LEFT(Worker_City, 1) AS LEFT_1City FROM Worker_Info;
这个SQL语句显示了每个员工所在城市的第一个字符。
输出结果:
Worker_City | LEFT_1City |
---|---|
Delhi | D |
Delhi | D |
Mumbai | M |
Kolkata | K |
Delhi | D |
Lucknow | L |
下面的SELECT查询使用了上述Worker_Info表中的Worker_First_Name和Worker_Address列的LEFT函数:
SELECT Worker_First_Name, LEFT(Worker_First_Name, 2) AS LEFT_2character, Worker_Address, LEFT(Worker_Address, 6) AS LEFT_6character FROM Worker_Info WHERE Worker_Salary > 30000;
输出:
Worker_First_Name | LEFT_2character | Worker_Address | LEFT_6character |
---|---|---|---|
Bulbul | Bu | Nirman Vihar | Nirman |
Saurabh | Sa | Sector 128 | Sector |
Shivani | Sh | Vivek Vihar | Vivek |
Shyam | Sh | Krishna Nagar | Krishn |