Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Concatenation
Simple text functions
How to modify Strings
Summary

Instruction

Great! Let's look at some functions that remove leading or trailing spaces from a string.

One of these is LTRIM(). It removes the leading or LEFT spaces from a string.

SELECT LTRIM(N' Sophie ');

This query returns 'Sophie '. The leading space was removed.

The RTRIM() function removes the trailing or RIGHT spaces from a string. It works in a similar way:

SELECT RTRIM(N' Sophie ');

This returns ' Sophie'. There is a leading space, but no trailing space.

Exercise

Some of the item names in the Item table have leading and/or trailing spaces. Show two columns:

  • the item name with added prefix N'No trim:' to see the leading spaces. Name the column Before.
  • the item name without leading spaces. Prefix the value with N'Trim:' to make sure that the spaces have been removed. Name the column After.

Stuck? Here's a hint!

Use LTRIM(Name) to remove leading spaces from the item names. In order to add the prefixes, simply use the concatenation operator, +.