SQL Server LEFT() Function: Returns Substring from Left
In SQL Server, the LEFT()
function returns the specified number of characters from the left side of the specified string.
LEFT(character_expression, number_of_characters)
Parameters
- character_expression: A string of type character or binary data. It can be a constant, variable, or column. It can be of any data type except text or ntext.
- number_of_characters: A positive integer. The number of characters that will be extracted from the left side of the input string.
Return Value
Returns varchar
data when the input string is a non-Unicode character data type and returns nvarchar
when the input string is unicode.
Example 1:
The following example uses the LEFT()
function to extract five characters from the left of an input string.
SELECT LEFT('Hello world', 5);
Example 2:
The following uses the LEFT()
function with the column of the database table.
SELECT LEFT(FirstName, 3) AS Tag FROM Employee;
Example 3:
In the following example, the LEFT()
function is used to extract the left three digits of the phone number as the Area Code.
SELECT FirstName, LEFT(phone, 3) AS AreaCode FROM Employee;