SQL LTRIM Function Example | LTRIM In MySQL And SQL Server
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.
- 1 SQL LTRIM Function
- 2 Examples of trim() function in SQL
- 3 LTRIM() Function in SQL Server
- 4 LTRIM() Function in MySQL
- 5 Recommended Posts
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])
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
SELECT LTRIM (‘ SQL’);
Here all the leading spaces are removed from the string.
SELECT LTRIM (‘000123’, ‘0’);
As Trim_string was specified as 0. So, all leading 0 were removed from the string.
SELECT LTRIM (‘00012300’, ‘0’);
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.
SELECT LTRIM (‘2145AppDividend’, ‘012345’);
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.
SELECT LTRIM (‘aabbabcSQLabc’, ‘abc’);
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.
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.
Select LTRIM (Emp_id, ‘#’) AS Emp_id, Emp_name, City, State, Salary from Employee;
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.
SELECT LTRIM(' AppDividend Uses Cloud Hosting') result;
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.
SELECT LTRIM(" MySQL LTRIM() Tutorial") AS LeftTrimmedString;
MySQL LTRIM() Tutorial
Finally, SQL LTRIM Function Example | LTRIM In MySQL And SQL Server is over.