Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LTRIM() Function in MySQL

  • Last Updated : 03 Dec, 2020

LTRIM() :
This function in MySQL is used to remove leading spaces from a string.

Syntax :

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

LTRIM(str)

Parameter :
It accepts one parameter as mentioned above and described below as follows.

  • str – The string from which we want to remove leading spaces.

Returns :
It returns a string after truncating all leading spaces.



Example-1 :
Removing all the leading spaces of a given string using LTRIM Function.

SELECT '    geeksforgeeks'  As OriginalString,
LTRIM ('    geeksforgeeks')  AS LeftTrimmedString;

Output :

OriginalStringLeftTrimmedString
geeksforgeeksgeeksforgeeks

Example-2 :
Removing all the leading spaces of a given string using LTRIM Function.

SELECT '    MySQL' AS String, 
RTRIM ('MySQL')  AS Tstring;

Output :

StringTstring
MySQLMySQL

Example-3 :
LTRIM Function can also be used to remove all the leading spaces of column data. To demonstrate create a table named Employee.

CREATE TABLE Employee
(
 Employee_id INT AUTO_INCREMENT,  
 Employee_name VARCHAR(100) NOT NULL,
 Joining_Date DATE NOT NULL,
 PRIMARY KEY(Employee_id )
);

Inserting some data to the Employee table :

INSERT INTO Employee
(Employee_name, Joining_Date )
VALUES
 ('     Ananya Majumdar', '2000-01-11'),
 ('   Anushka Samanta', '2002-11-10' ),
 ('   Aniket Sharma ', '2005-06-11' ),
 ('   Anik Das', '2008-01-21'  ),
 ('  Riya Jain', '2008-02-01' ),
 ('    Tapan Samanta', '2010-01-11' ),
 ('   Deepak Sharma', '2014-12-01'  ),
 ('   Ankana Jana', '2018-08-17'),
 ('  Shreya Ghosh', '2020-09-10') ;

So, the Employee Table is as follows.

select * from Employee ;

Output :

Employee_idEmployee_nameJoining_Date
1Ananya Majumdar2000-01-11
2Anushka Samanta2002-11-10
3Aniket Sharma2005-06-11
4Anik Das2008-01-21
5Riya Jain2008-02-01
6Tapan Samanta2010-01-11
7Deepak Sharma2014-12-01
8Ankana Jana2018-08-17
9Shreya Ghosh2020-09-10

Now, we are going to remove all leading spaces from the Employee_name column.

 SELECT  
 Employee_id,  Employee_name,
 LTRIM( Employee_name) AS  TrimmedEname  
 FROM  Employee ;

Output :

Employee_idEmployee_nameTrimmedEname
1Ananya MajumdarAnanya Majumdar
2Anushka SamantaAnushka Samanta
3Aniket SharmaAniket Sharma
4Anik DasAnik Das
5Riya JainRiya Jain
6Tapan SamantaTapan Samanta
7Deepak SharmaDeepak Sharma
8Ankana JanaAnkana Jana
9Shreya GhoshShreya Ghosh
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!