Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » String FunctionsRSS Feeds

LEFT Function

Posted By: Sarita Patel     Category: SQL Server     Views: 3643

This article explains the use of LEFT function in sql server with examples.

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. 
 
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on LEFT Function Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of LEFT Function is from United States.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!