SQL LEFT Function

LEFT returns the left part from a string, given a number of characters.

The number of characters must be a positive number, or an error will occur.

Example

#

This example returns the first 8 characters from the input string.

SELECT LEFT('American Express', 8) AS 'Left'
Result:  1 record
Left
American

Syntax

Syntax of the LEFT function.

LEFT(string, number)

string -- a string or column name.

number -- the number of characters to return.


More Examples

LEFT. First 2 characters.

SUPPLIER
Id
CompanyName
ContactName
City
Country
Phone
Fax
Problem: List suppliers with abbreviated, two-letter countries.
SELECT CompanyName, City, 
       UPPER(LEFT(Country, 2)) AS Country
  FROM Supplier
Result:  29 records
CompanyName City Country
Exotic Liquids London UK
New Orleans Cajun Delights New Orleans US
Grandma Kelly's Homestead Ann Arbor US
Tokyo Traders Tokyo JA
Cooperativa de Quesos 'Las Cabras' Oviedo SP
Mayumi's Osaka JA
Pavlova, Ltd. Melbourne AU

LEFT. First character

CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List customers with last name initials.
SELECT FirstName + ' ' + LEFT(LastName,1) + '.' AS Name, 
       City, Country
  FROM Customer
Result:  91 records
Name City Country
Maria A. Berlin Germany
Ana T. México D.F. Mexico
Antonio M. México D.F. Mexico
Thomas H. London UK
Christina B. Luleå Sweden

You may also like



Guides


vsn 3.1