LEFT function is used to get left part of the specified string with the specified number of characters.
Syntax of LEFT Function :
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.
integer_expression is a positive integer that specifies how many characters of the character_expression will be returned. It returns an error if integer_expression is negative.
Return type of LEFT function is varchar or nvarchar.
Examples of LEFT Function :
Example 1 : Use of LEFT function in select clause
SELECT LEFT('SYNTAX-EXAMPLE',6)
Output
SYNTAX
Above example returns leftmost 6 characters of specified string.
Example 2 : Use of LEFT function to display leftmost 10 characters of table field.
SELECT LEFT(ShipName,10)
FROM Orders
Output
Vins et al
Toms Spezi
Hanari Car
Victuaille
Suprêmes d
Hanari Car
Above example displays leftmost 10 characters from field ShipName of orders table.
Example 3 : Use of LEFT function in where clause
SELECT DISTINCT ShipName
FROM Orders
WHERE LEFT(ShipName,2) = 'to'
Output
ShipName
Toms Spezialitäten
Tortuga Restaurante
Above example displays unique ship name starting with characters 'to' in order table.