SQL Server LTRIM Function

Summary: in this tutorial, you will learn how to use the SQL Server LTRIM() function to remove leading blanks from a string.

SQL Server LTRIM() function overview

The LTRIM() function returns a string after removing leading blanks. The following shows the syntax of the LTRIM() function:

LTRIM(input_string)
Code language: SQL (Structured Query Language) (sql)

In this syntax, the input string is an expression of character or binary data. It can be a literal string, variable, or column.

The input_string must evaluate to a value of a data type, except for TEXT, NTEXT and IMAGE, that can be implicitly convertible to VARCHAR.

Otherwise, you must use the CAST() function to convert it to a character string explicitly.

SQL Server LTRIM() function examples

Let’s take some examples of using the LTRIM() function.

A) Using LTRIM() function with literal strings

This example uses the LTRIM() function to remove leading blanks of the string ' SQL Server LTRIM function':

SELECT 
    LTRIM('   SQL Server LTRIM Function') result;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

result
----------------------------
SQL Server LTRIM Function

(1 row affected)Code language: PHP (php)

B) Using LTRIM() function to clean up spaces

The following example uses LTRIM() function to remove the leading blank after splitting the strings into substrings:

SELECT 
    LTRIM(value) part
FROM 
    STRING_SPLIT('Doe, John', ',');
Code language: SQL (Structured Query Language) (sql)

Here is the output:

part
---------
Doe
John

(2 rows affected)

Note that if you don’t use the LTRIM() function, the second row will have a leading space.

In this tutorial, you have learned how to use the SQL Server LTRIM() function to remove the leading blanks from a string.

Was this tutorial helpful?