Skip to content

Latest commit

 

History

History
125 lines (93 loc) · 4.9 KB

left-transact-sql.md

File metadata and controls

125 lines (93 loc) · 4.9 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
LEFT (Transact-SQL)
LEFT (Transact-SQL)
markingmyname
maghan
03/13/2017
sql
t-sql
reference
LEFT
LEFT_TSQL
character strings [SQL Server], LEFT
characters [SQL Server], leftmost
LEFT function
leftmost character of expression
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

LEFT (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

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

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

LEFT ( character_expression , integer_expression )  

Arguments

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.

Note

If string_expression is of type binary or varbinary, LEFT will perform an implicit conversion to varchar, and therefore will not preserve the binary input.

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.

Return Types

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

Returns nvarchar when character_expression is a Unicode character data type.

Remarks

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.

Examples

A. Using LEFT with a column

The following example returns the five leftmost characters of each product name in the Product table of the [!INCLUDEssSampleDBnormal] database.

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

B. Using LEFT with a character string

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

SELECT LEFT('abcdefg',2);  
GO  

[!INCLUDEssResult]

--   
ab   
  
(1 row(s) affected)  

Examples: [!INCLUDEssazuresynapse-md] and [!INCLUDEssPDW]

C. Using LEFT with a column

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

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

D. Using LEFT with a character string

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;  

[!INCLUDEssResult]

--   
ab  

See Also

LTRIM (Transact-SQL)
RIGHT (Transact-SQL)
RTRIM (Transact-SQL)
STRING_SPLIT (Transact-SQL)
SUBSTRING (Transact-SQL)
TRIM (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)