Tip of the Day : Example Uses of the LTRIM String Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - November 30, 2023

Example Uses of the LTRIM String Function

The LTRIM string function returns a character expression after it removes the leading blanks or spaces.   The syntax of the LTRIM string function is as follows:

LTRIM ( < character_expression > )

The < character_expression > is an expression of character or binary data and can be a constant, variable or column.  The < character_expression > must be of data type, except TEXT, NTEXT and IMAGE, that is implicitly convertible to VARCHAR.  Otherwise, the CAST function must be used to explicitly convert the < character_expression >

Usage #1 : Remove or Trim Leading Zeros in a String

DECLARE @Input          VARCHAR(20)
SET @Input = '00001240-01010'
SELECT REPLACE(LTRIM(REPLACE(@Input, '0', ' ')), ' ', '0') AS [Output]
Output
-----------
1240-01010

Usage #2 : Remove or Trim Trailing Spaces Without Using RTRIM

DECLARE @Input          VARCHAR(100)
SET @Input = 'The quick brown fox jumps over the lazy dog.    '

SELECT REVERSE(LTRIM(REVERSE(@Input))) AS [Output]
Output
-----------
The quick brown fox jumps over the lazy dog.

Usage #3 : Determine the Position of the First Non-Space Character

DECLARE @Input          VARCHAR(100)
SET @Input = '      SQL Server Helper'
SELECT LEN(@Input) - LEN(LTRIM(@Input)) + 1 AS [Location]
Location
-----------
7

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions