Beginner

98. TRIM Function

Learn how to remove unwanted spaces from the beginning and end of text values.

📘 Lesson 98 of 728 ⏱ 8 min 🎯 Beginner

The TRIM function removes leading and trailing spaces from a string. It is commonly used when cleaning text data imported from external systems.

Unwanted spaces can cause inconsistent comparisons, duplicate-looking values, and data-quality problems.

A department value stored as ' Finance ' can be cleaned using TRIM so that the leading and trailing spaces are removed.

SQL
SELECT
    TRIM(Department) AS CleanDepartment
FROM Employees;

TRIM is useful when cleaning customer names, addresses, product names, imported CSV data, employee records, and other text fields.

  • TRIM removes leading and trailing spaces.
  • It is useful for cleaning imported data.
  • It can improve text consistency.
  • It does not normally remove spaces between words.
  • Other functions may be used for more specific whitespace operations.
What does TRIM do?

TRIM removes leading and trailing spaces from a string.

Why is TRIM useful in data analysis?

It helps clean inconsistent text values that contain unwanted spaces.

Have you finished this lesson?

Introduction to SQL – All Lessons