AppDividend
Latest Code Tutorials

SQL LTRIM Function Example | LTRIM In MySQL And SQL Server

0

SQL LTRIM Function Example is today’s topic. SQL LTRIM function is used for removing all the leading whitespaces or specified characters from the string, i.e. from the left-hand side of the string. In this tutorial, you will learn how to use a MySQL and SQL Server LTRIM() function to remove leading blanks from a string. Oracle/PLSQL LTRIM function removes all specified characters from the left-hand side of a string.

SQL LTRIM Function

The LTRIM() function returns the string after removing leading blanks.

The following shows the syntax of the LTRIM() function.

SELECT LTRIM (input_string, [trim_string])

PARAMETERS

Input_string: It is the string from where the characters have to be removed from the left-hand side.

Trim_string: It is completely optional. It is mainly used for specifying the characters to be removed. If this parameter is omitted, then all the leading spaces will be removed.

Examples of trim() function in SQL

Query 1

SELECT LTRIM (‘    SQL’);

Output

SQL

Here all the leading spaces are removed from the string.

Query 2

SELECT LTRIM (‘000123’, ‘0’);

Output

123

As Trim_string was specified as 0. So, all leading 0 were removed from the string.

Query 3

SELECT LTRIM (‘00012300’, ‘0’);

Output

12300

As Trim_string was specified as 0. So, all leading 0 were removed from the string but not the trailing zeroes because the function used here was LTRIM which removes only the leading zeroes.

Query 4

SELECT LTRIM (‘2145AppDividend’, ‘012345’);

Output

AppDividend

In the above example, all combinations of 0 to 5 are listed. By doing this, all the order of the leading numbers from the string will be removed.

Query 5

SELECT LTRIM (‘aabbabcSQLabc’, ‘abc’);

Output

SQLabc

In the above example, ‘abc’ is declared as Trim_string. So, ‘a,’ ‘b,’ ‘c’ is considered as an individual character. So, all leading characters of ‘abc’ are removed from the string.

Above were the common examples to make you clear how the LTRIM function works.

Let’s apply this function to a table.

Table: Employee

Emp_id Emp_name City State Salary
#101 Rohit Patna Bihar 30000
#201 Shivam Jalandhar Punjab 20000
#301 Karan Allahabad Uttar Pradesh 40000
#401 Suraj Kolkata West Bengal 60000
#501 Akash Vizag Andhra Pradesh 70000

 

Previously, in the employee table, ‘#’ was added before the emp_id. Now we want to remove this character from the emp_id and display only the number. So, to do this following query has to be written.

Query

Select LTRIM (Emp_id, ‘#’) AS Emp_id, Emp_name, City, State, Salary 
from Employee;

Output

Emp_id Emp_name City State Salary
101 Rohit Patna Bihar 30000
201 Shivam Jalandhar Punjab 20000
301 Karan Allahabad Uttar Pradesh 40000
401 Suraj Kolkata West Bengal 60000
501 Akash Vizag Andhra Pradesh 70000

 

Here, you can see that the new table is created by removing leading ‘#’ unique character from the Emp_id.

LTRIM() Function in SQL Server

See the following example in SQL Server.

Query

SELECT  LTRIM('   AppDividend Uses Cloud Hosting') result;

Output

result
----------------------------
SQL Server LTRIM Function
 
(1 row affected)

LTRIM() Function in MySQL

The function works onwards from  MySQL 4.0. See the example in MySQL.

Query

SELECT LTRIM("     MySQL LTRIM() Tutorial") AS LeftTrimmedString;

Output

MySQL LTRIM() Tutorial

Finally, SQL LTRIM Function Example | LTRIM In MySQL And SQL Server is over.

Recommended Posts

Lower() Function Example In MySQL and SQL Server

SQL BETWEEN AND IN OPERATOR Tutorial

SQL Triggers Tutorial With Example

SQL Wildcard Operators Tutorial

SQL Distinct Keyword Tutorial

SQL Aggregate Functions Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.