SQL LTRIM 函数
一、SQL LTRIM 函数 语法
SQL LTRIM 函数 字符串函数从给定原始字符串的左侧截断给定字符或子字符串。它还截断指定字符串左侧的空格。
语法1:此语法使用带有 SQL 表列名的 LTRIM 函数:
SELECT LTRIM(Column_Name, string) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定要在其上运行 LTRIM 函数的列的名称。
语法2:此语法使用带有字符集(字符串)的 LTRIM 函数:
SELECT LTRIM(Original_String, trimmed_string);
语法3:此语法使用带有单个字符的 LTRIM 函数:
SELECT LTRIM(Original_String, trimmed_character);
二、SQL LTRIM 函数 示例
示例 1:以下 SELECT 查询根据 LTRIM 函数从指定字符串中截断给定空格:
SELECT LTRIM( ' YIIDIAN',' ');
输出结果为:
'YIIDIAN'
示例 2:以下 SELECT 查询根据 LTRIM 函数从指定字符串中截断空格:
SELECT LTRIM( ' JAVATPOINT ');
输出结果为:
'JAVATPOINT '
示例 3:以下 SELECT 查询从指定字符串中修剪 CAPITAL OF INDIA 子字符串:
SELECT LTRIM( 'NEW DELHI IS THE CAPITAL OF INDIA', 'NEW DELHI IS THE ');
输出结果为:
CAPITAL OF INDIA
示例 4:以下 SELECT 查询从指定字符串中修剪给定符号:
SELECT LTRIM( '####98221545###', '#');
输出结果为:
98221545###
示例 5:以下 SELECT 查询从指定字符串中修剪给定的一组数字:
SELECT LTRIM( '2021Yiidian2021', '2021');
输出结果为:
Yiidian2021
示例 6:以下 SELECT 查询从指定字符串中修剪给定的一组数字:
SELECT LTRIM( '202120212021Yiidian', '2021');
输出结果为:
Yiidian
示例 7:以下 SELECT 查询从字符串左侧修剪修剪后的字符串中存在的所有数字:
SELECT LTRIM( '90287Yiidian', '0123456789');
此命令实际上删除了修剪后的字符串中单独出现的数字。
输出结果为:
Yiidian
示例 8:此示例对SQL语言中的表使用 LTRIM 函数。
首先,我们必须创建新的 SQL 表,这有助于理解 LTRIM 字符串函数。在 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 查询将 LTRIM 函数与上述 Faculty_Info 表的 Faculty_Last_Name 列一起使用:
SELECT Faculty_Last_Name, LTRIM(Faculty_Last_Name) AS LTRIM_LastName FROM Faculty_Info;
此 SQL 语句从每个教员的姓氏左侧修剪空格:
输出结果为:
Faculty_Last_Name | LTRIM_LastName |
---|---|
' Sharma ' | 'Sharma ' |
' Roy ' | 'Roy ' |
' Roy ' | 'Roy ' |
' Singhania ' | 'Singhania ' |
' Sharma ' | 'Sharma ' |
' Besas ' | 'Besas ' |
以下 SELECT 查询将 LTRIM 函数与上面 Faculty_Info 表中的 Faculty_Id 大于 1002 的那些学院的 Faculty_First_Name 和 Faculty_Address 列一起使用:
SELECT Faculty_Id, LTRIM(Faculty_First_Name), LTRIM(Faculty_Address) FROM Faculty_Info WHERE Faculty_Id >1002;
该 SQL 语句从 Id 大于 1002 的院系的名字和地址的左侧修剪空格。
输出结果为:
Faculty_Id | LTRIM(Faculty_First_Name) | LTRIM(Faculty_Address) |
---|---|---|
1004 | 'Saurabh ' | 'Sector 128 ' |
1005 | 'Shivani ' | 'Vivek Vihar ' |
1006 | 'Avinash ' | 'Sarvodya Calony ' |
1007 | 'Shyam ' | 'Krishna Nagar ' |
热门文章
优秀文章