TRIM()

SQL · String Functions

Theory

TRIM(string) removes spaces from both the start and the end of a string; spaces in the middle are kept. TRIM(' Asha ') returns 'Asha', and TRIM(' Asha Rao ') returns 'Asha Rao'. The standard form also lets you say which side and which character: TRIM(LEADING '0' FROM code), TRIM(TRAILING '.' FROM x) or TRIM(BOTH '*' FROM x).

Dialects differ in syntax. Postgres, MySQL, Oracle and SQL Server accept the standard TRIM(... FROM ...) form. SQLite and Postgres also accept TRIM(string, characters), where every character listed is stripped, so TRIM('**Asha**', '*') returns 'Asha'. Plain TRIM removes only the ordinary space, not tabs or line breaks in most databases, which is a common reason why values still look padded after trimming. Invisible padding makes 'Asha ' and 'Asha' different values, so joins and GROUP BY quietly fail until you TRIM both sides.

Why we use it

Spaces sneak in from Excel uploads, forms and fixed-width files. They are invisible on screen but make equal values compare as different, break joins and inflate distinct counts. TRIM removes them.

When to use it

Use TRIM when cleaning imported or user-entered text, and on both sides of a join key that might be padded. Use LTRIM or RTRIM when only one side should be cleaned. For tabs and newlines, list them explicitly or use REGEXP_REPLACE.

How it works

  1. Take the string (and optional characters to strip)
  2. Remove matching characters from the start until a different one appears
  3. Remove matching characters from the end the same way
  4. Characters in the middle are never touched
  5. Return the trimmed string

Syntax

TRIM(string)

Example

SELECT TRIM('  Asha  ');
-- 'Asha'

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic