How the LEFT() Function Works in SQL Server (T-SQL)

In SQL Server, you can use the T-SQL LEFT() function to return a given number of characters from the left part of a string.

The function accepts two arguments; the string itself, and the number of characters you’d like to be returned from that string.

Syntax

The official syntax for both functions goes like this:

LEFT ( character_expression , integer_expression )

Where character_expression is the string, and integer_expression is the number of characters you want returned from that string.

If the second argument is type bigint and contains a large value, the first argument (i.e. the string) must be of a large data type such as varchar(max).

Example 1 – Basic Usage

Here’s an example of selecting the 3 leftmost characters from a string:

SELECT LEFT('Catastrophic', 3) AS Result;

Result:

+----------+
| Result   |
|----------|
| Cat      |
+----------+

Example 2 – Negative Integer Expression

The second argument must be a positive value. If it’s a negative value, an error is returned:

SELECT LEFT('Catastrophic', -3) AS Result;

Result:

Invalid length parameter passed to the left function. 

Example 3 – Database Query

Here’s an example of using the LEFT() function on data returned from a database:

USE Music;
SELECT TOP 7
    ArtistName AS Original, 
    LEFT(ArtistName, 5) AS Modified
FROM Artists;

Result:

+------------------+------------+
| Original         | Modified   |
|------------------+------------|
| Iron Maiden      | Iron       |
| AC/DC            | AC/DC      |
| Allan Holdsworth | Allan      |
| Buddy Rich       | Buddy      |
| Devin Townsend   | Devin      |
| Jim Reeves       | Jim R      |
| Tom Jones        | Tom J      |
+------------------+------------+