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

Trimming Strings in Transact-SQL

The Transact SQL LTrim and RTrim functions can be used to remove spaces from the left and right sides of a string, respectively. As such it can be used to ensure that data entered into a table does not contain any additional whitespace that could cause future issues of data quality.

For example, this stored procedure removes trailing whitespace from the input RegionDescription using RTrim:

CREATE PROCEDURE InsertRegion
(
    @RegionDescription nchar(50)
)

AS


INSERT INTO
    Regions
    (
    RegionDescription
    )

VALUES
    (
    RTrim(@RegionDescription)
    )

Unlike many programming languages, Transact-SQL does not have a function to remove whitespace from both left and right sides of the string simultaneously. If this is required, it is possible to use a nested function calling both LTrim and RTrim:

LTrim(RTrim(@RegionDescription))

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