The Wayback Machine - https://web.archive.org/web/20240905203927/https://www.geeksforgeeks.org/ltrim-function-in-sql/
Open In App

SQL LTRIM() Function

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The SQL LTRIM function removes the leading space from the left of a string or string expression.

LTRIM() Function in SQL

The LTRIM() function in SQL is an inbuilt string function that is used to remove the leading empty spaces from the left side of our data. The LTRIM function can also be used to remove a specific character or substring from the left side of the string.

The LTRIM function is primarily used for data cleaning purposes where the data administrator uses this function to remove unnecessary characters from the database in one go.

Syntax

SQL LTRIM function syntax is:

LTRIM(Input_String, [Trim_Characters])

Parameters

  • Input_String: String from which you want to remove leading spaces.
  • Trim_Characters: [optional] specified characters you want to remove.

Note: If we don’t specify any characters to remove, the LTRIM function removes the white spaces for data cleaning and manipulation in SQL.

SQL LTRIM function Working

The following diagram shows how the SQL LTRIM function affects strings in the table.

LTRIM Workflow

Workflow of LTRIM function

The function LTRIM in SQL analyzes the input string from the left side and removes any empty spaces or the mentioned character found on that string. The function does not alter the original string, it returns a new string where the leading whitespaces are removed. 

This function doesn’t deal with whitespaces present between the strings. For example if the string ‘ JOHN’ was written as ‘ JO HN’ then only the leading white spaces on the left side will be removed by this function and the spaces in between will remain as they were earlier.

SQL LTRIM Function Examples

Now let us see some examples of LTRIM function in different use cases to understand it’s working better:

Remove Leading White Spaces Using LTRIM function Example

In this example, we will remove the leading white spaces from a string using SQL LTRIM function.

Query:

Select LTRIM('     GeeksforGeeks.') AS trimmedString;

Output:

remove leading white spaces using ltrim function

Output

Remove Specific Characters Using LTRIM function Example

In this example, we will remove specific characters using LTRIM function.

Query:

SELECT LTRIM('----GeeksforGeeks', '-') AS TrimmedString;

Output:

remove specific characters using ltrim function

Output

Remove a Substring from a String Using LTRIM function Example

In this example, we will remove a substring from a string using the LTRIM function.

Query:

SELECT LTRIM('GeeksforGeeks', 'Geeks') AS TrimmedString;

Output:

remove substring using ltrim function

Output

Use LTRIM on a Table Column Example

Let us consider the following table where the names of the users are stored with whitespaces. If we want to remove these whitespaces then we can use the LTRIM in SQL to remove them in one go.

input table

Input Table

To create this table, write the following SQL query:

CREATE TABLE Names (
Name VARCHAR(45)
);
INSERT INTO Names (Name) VALUES
(' Alice'), (' Bob'), (' Charlie'), (' David'), (' Eve');

Now to remove whitespace from this column, we can use the SQL query:

SELECT LTRIM(name) FROM users AS TrimmedNames;

Output:

output table

Output Table

After executing the LTRIM function on the column of the above table, you can observe that the leading whitespaces are removed in one go from all the records of the table.

Important Points About SQL LTRIM Function

  • SQL LTRIM is useful inbuilt function which can be used to remove the whitespaces on left our data in the databases.
  • Not only whitespaces it can also remove a specific character or a subtring from the data.
  • LTRIM function in SQL can be used on entire column/field of table to remove whitespace.
  • LTRIM function only works for the left side of data, and will not affect the middle or right side of data.

Previous Article
Next Article

Similar Reads

LTRIM() Function in SQL Server
The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string, SyntaxThe LTRIM function for SQL Server syntax is: LTRIM(string, [trim_string]) Parameter: string - The string from which the leading space character would be removed. trim_string - It is an opt
2 min read
LTRIM() Function in MySQL
LTRIM() : This function in MySQL is used to remove leading spaces from a string. Syntax : 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 lea
2 min read
PLSQL | LTRIM Function
The PLSQL LTRIM function is used for removing all specified characters from the left-hand side of a string. The PLSQL LTRIM function accepts two parameters which are input_string and trim_string. If the user does not specify trim_string, it defaults to a single blank. If char is a character literal, then you must enclose it in single quotes. Oracle
2 min read
LTrim() and RTrim() Function in MS Access
1. LTrim() Function : In MS Access LTrim() function remove all the leading spaces from the given string. In LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. Syntax : LTrim(string) Example-1 : SELECT LTrim(" GEEKSFORGEEKS") AS LeftTrimmedString; Output - LeftTrimmedString GEEKSFORGEEKS Examp
1 min read
LPAD, LOWER and LTRIM Function in MariaDB
1. LPAD Function : In MariaDB, the LPAD Function is used for the string that is left-padded with a specified string to a given length. In this function, it will take the first parameter a string, the second parameter length and the third parameter will be pad string.it will return left-padded with a specified string to a given length. If the string
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures. In this article let us look into the details abou
6 min read
Dynamic SQL and Temporary Tables in SQL Server
In SQL Server, creating and using Temp Tables using dynamic SQL is a good feature when we need to temporarily create tables at run time and delete automatically all within a session. They can be very useful when we need to store temporary data in a structured format and do data manipulation using Data Manipulation Language in SQL. In this article l
6 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
QUOTENAME() Function in SQL Server
QUOTENAME() function : This function in SQL Server is used to return a Unicode string with delimiters added in order to make the string a valid SQL Server delimited identifier. Features : This function is used to find a Unicode string with delimiters added. This function accepts only strings and delimiters. This function add delimiters by default i
3 min read
Conversion Function in SQL
When you define expressions and local variables then you should specify what type of data will be stored in those objects such as text data, money, dates, numbers, or characters. Strings Data types such as CHAR and VARCHAR.Decimal values such as FLOAT and REAL.Binary String such as BINARY.Date and Time Data Types such as DATE, TIME, TIMESTAMP, and
4 min read
REVERSE() Function in SQL Server
REVERSE() function : This function in SQL Server is used to reverse the stated string and return the output. Features : This function is used to reverse the stated string. This function accepts strings as parameter. This function always returns string. This function can also take set of integers and reverse it. This function can even reverse float
1 min read
How SYSDATE function works in PL/SQL
The PLSQL SYSDATE function will returns current system date and time on your database. There is no any parameter or argument for the SYSDATE function. The SYSDATE function returns a date value. Note that the SYSDATE function return date and time as "YYYY-MM-DD HH:MM:SS" (string) or as YYYYMMDDHHMMSS (numeric). Syntax: SYSDATE Example-1: SELECT Sysd
2 min read
SESSIONPROPERTY() Function in SQL Server
SESSIONPROPERTY() function : This function in SQL Server is used to return the settings of the session that is specified in the argument section. Features : This function is used to find the settings of the session state.This function comes under Advanced Functions.This function accepts only one parameter namely option. Syntax : SESSIONPROPERTY(opt
2 min read
SQL | ENCRYPT Function
The SQL Encrypt function is used to encrypt a string using UNIX crypt(). The function is based on Unix crypt() system call, hence it returns NULL on Windows systems. The Encrypt function accepts two parameters which are the string and the salt to be encrypted. The Encrypt function returns a binary string. Syntax: ENCRYPT(string, salt) Parameters Us
2 min read
TRANSLATE() Function in SQL Server
TRANSLATE() function : This function in SQL Server is used to return the translated string of the string stated in the first argument of this function, when the characters stated in the characters argument of the above function are converted into the characters stated in the last argument i.e, translations. Features : This function is used to find
2 min read
RAWTOHEX Function in PL/SQL
The objective is to use the function RAWTOHEX in PL/SQL code. Purpose : This inbuilt function is beneficial for the conversion of a raw value into a character value in its hexadecimal format. Syntax : RAWTOHEX(x) where x - raw value to be converted to hexadecimal value and hexadecimal equivalent in the form of a string value is returned. Here may b
2 min read