sql
  1. sql-ltrim-function

SQL String Functions: LTRIM Function

The LTRIM function in SQL is used to remove all the leading spaces from a string. It returns a string after removing all the leading spaces on the left-hand side of the string.

Syntax

The syntax for the LTRIM function in SQL is as follows:

LTRIM(string)
  • string: The string that you want to remove the leading spaces from.

Example

Let's consider an example to understand how the LTRIM function is implemented in SQL.

SELECT LTRIM('   Hello World!') AS trimmed_string;

Output

The output of the above query after execution will be as follows:

trimmed_string
---------------
Hello World!

Explanation

In the above example query, the LTRIM function is used to remove all of the leading spaces before the string "Hello World!". The output of the query returns the string without any leading spaces.

Use

The LTRIM function in SQL is useful in cases where you want to remove the leading white spaces from a string. This function is perfect when you are working with user inputs, which may have some unnecessary spaces at the start.

Important Points

  • The LTRIM function only removes the leading spaces from the string, so any spaces in the middle or end of the string will not be affected.
  • The LTRIM function is not case-sensitive, so it can remove the leading space irrespective of the case.

Summary

  • The LTRIM function in SQL is used to remove all the leading spaces from a string.
  • The function syntax takes the string as an input.
  • SQL LTRIM() function is very useful to deal with a large amount of data and to remove unwanted spaces from strings.
Published on: