How to use LTRIM in Sql Server



SQL Server provides the LTRIM function to remove leading white space. In this article, we will learn how to use LTRIM in SQL Server.

The Syntax

The basic syntax of a LTRIM is as follows:

  • string: The string or varchar you want to remove leading white space.

Getting Setup

For this, we will be using docker. This is recommended for more than just using SQL Server. To find how to install docker go here:

Now create a file called docker-compose.yml and add the following.

version: "3.9"
    image: ""
      - 1433:1433
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

Open a terminal and go to the folder the file is located. Then run the following.

docker-compose up

If you are looking for another good reference, you can check here:

Basic Example

The simple example is to remove leading spaces from a string literal.

select ltrim('   Hello World!') as result;
Hello World!

Lower on a Table Column

We can also run lower on a column. Below we create an employee table where the names have extra white space.

CREATE TABLE employees (
    first_name VARCHAR (50) NOT NULL,
    last_name VARCHAR (50) NOT NULL

insert into employees (first_name, last_name) 
	(' Keith', 'Holliday'),
	('   Jon', 'Doe'),
	('  Jane', 'Doe');

We can now run the ltrim function on the columns.

  ltrim(first_name) as FirstName,
	ltrim(last_name) as LastName
from employees e;
FirstName LastName
Keith Holliday
Jon Doe
Jane Doe