LEFT (Transact-SQL)

LEFT (Transact-SQL)

 

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

Returns the left part of a character string with the specified number of characters.

Topic link icon Transact-SQL Syntax Conventions


LEFT ( character_expression , integer_expression )
-- Azure SQL Data Warehouse and Parallel Data Warehouse
LEFT (character_expression , integer_expression )

character_expression

Is an expression of character or binary data. character_expression can be a constant, variable, or column. character_expression can be of any data type, except text or ntext, that can be implicitly converted to varchar or nvarchar. Otherwise, use the CAST function to explicitly convert character_expression.

integer_expression

Is a positive integer that specifies how many characters of the character_expression will be returned. If integer_expression is negative, an error is returned. If integer_expression is type bigint and contains a large value, character_expression must be of a large data type such as varchar(max).

The integer_expression parameter counts a UTF-16 surrogate character as one character.

Returns varchar when character_expression is a non-Unicode character data type.

Returns nvarchar when character_expression is a Unicode character data type.

When using SC collations, the integer_expression parameter counts a UTF-16 surrogate pair as one character. For more information, see Collation and Unicode Support.

The following example returns the five leftmost characters of each product name in the Product table of the AdventureWorks2012 database.

SELECT LEFT(Name, 5) 
FROM Production.Product
ORDER BY ProductID;
GO

The following example uses LEFT to return the two leftmost characters of the character string abcdefg.

SELECT LEFT('abcdefg',2);
GO

Here is the result set.

-- 
ab 

(1 row(s) affected)

The following example returns the five leftmost characters of each product name.

-- Uses AdventureWorks

SELECT LEFT(EnglishProductName, 5) 
FROM dbo.DimProduct
ORDER BY ProductKey;

The following example uses LEFT to return the two leftmost characters of the character string abcdefg.

-- Uses AdventureWorks

SELECT LEFT('abcdefg',2) FROM dbo.DimProduct;

Here is the result set.

-- 
ab

Community Additions

ADD
Show:
© 2016 Microsoft