Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > Articles > Transact SQL Programming Articles

SQL Server Help: Selecting a Left and Right SubString of a String in Transact-SQL

The Transact SQL Left and Right functions can be used to truncate strings after a certain number of characters from the left and right of the string, respectively. Each of these functions requires two arguments:

  • The string from which the substring should be extracted.
  • The length of the string to return.

The example below for the Northwind sample SQL Server database uses the Left function to create a three character ProductCode column from the first three characters of the ProductName column:

SELECT ProductName, LEFT(ProductName, 3) AS ProductCode FROM Products

Similarly, the Right function truncates strings at a certain length from the right hand side of that string. The following example produces a three character ProductCode column from the final three characters of the ProductName column:

SELECT ProductName, RIGHT(ProductName, 3) AS ProductCode FROM Products

Useful Links

  • The SQL Documentation Tool builds technical documentation for Microsoft SQL Server databases. A free trial version is available for download.

  Site Map | Privacy Policy

All content is 1995 - 2012