LEFT (Transact‑SQL)
Returns the leftmost n characters from a string.
Syntax
LEFT ( string_expression , integer_expression )
Parameters
| Parameter | Description |
|---|---|
string_expression |
Expression of character data. Can be a literal, column, or variable. |
integer_expression |
Number of characters to return. If negative, an error is raised. |
Return Value
Returns a varchar, nvarchar, or varbinary value depending on the data type of string_expression. The length of the result is the smaller of integer_expression or the length of string_expression.
Examples
SELECT LEFT('AdventureWorks', 5) AS Result; -- Returns 'Adven'
DECLARE @name nvarchar(50) = N'International';
SELECT LEFT(@name, 12) AS ShortName; -- Returns N'International'
Remarks
- If
integer_expressionis 0, the function returns an empty string. - If
string_expressionis NULL, the result is NULL. - When the input is of type
varbinary, the function returns avarbinaryresult.