SQL LTRIM函数

SQL LTRIM函数

该字符串函数从给定原始字符串的左侧截取给定字符或子字符串。它还会从指定字符串的左侧截取空格。

LTRIM字符串函数的语法

语法1: 此语法使用LTRIM函数与SQL表的列名:

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);

LTRIM字符串函数示例

示例1:以下SELECT查询根据LTRIM函数从指定字符串中截去给定的空格:

SELECT LTRIM(  '          JAVATPOINT','  ');

输出:

'JAVATPOINT'

示例2:下面的SELECT查询根据LTRIM函数从指定的字符串中去除空格。

SELECT LTRIM(  '              JAVATPOINT           ');

输出:

'JAVATPOINT           '

例3:以下SELECT查询从指定字符串中删除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(  '2021JavaTpoint2021', '2021');

输出:

JavaTpoint2021

示例6:下面的SELECT查询从指定的字符串中删除给定的一组数字:

SELECT LTRIM(  '202120212021JavaTpoint', '2021');

输出:

JavaTpoint

示例7:以下SELECT查询从修剪后的字符串中删除左侧的所有数字:

SELECT LTRIM(  '90287JavaTpoint', '0123456789');

此命令实际上是删除修剪字符串中的个别数字出现。

输出:

JavaTpoint

示例8:此示例使用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查询将学院的教职人员记录插入到 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查询在上述Faculty_Info表中使用LTRIM函数与Faculty_First_Name和Faculty_Address列,并筛选出faculty_Id大于1002的教师数据:

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 ‘

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程