SQL Keywords

SQL TRIM

What is SQL TRIM?

Removes specified leading and/or trailing characters (default is spaces) from a string.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL TRIM: Supported by PostgreSQL, MySQL, MariaDB, SQL Server (2017+), Oracle, SQLite, Snowflake, BigQuery, Redshift, DB2, and all ANSI-compliant engines.

SQL TRIM Full Explanation

TRIM is a standard SQL string function that cleans up text by deleting unwanted characters from the start, the end, or both ends of a string. By default it targets space characters, but you can supply any single-byte or multibyte literal or expression. The optional LEADING, TRAILING, or BOTH keyword specifies which side to operate on; if omitted, BOTH is assumed. TRIM never alters characters inside the middle of the string and returns a value of the same data type as the input (CHAR, VARCHAR, TEXT, etc.). Most databases also offer shorthand forms such as TRIM(string) or dialect-specific variants like LTRIM and RTRIM.Important caveats:- Case sensitivity, collation, and Unicode normalization can affect what qualifies as a match.- When trimming multibyte characters, make sure the literal matches the exact byte sequence.- Some dialects (e.g., MySQL) allow only one character in the specification, while others (e.g., PostgreSQL) accept a set of characters.- Attempting to TRIM NULL returns NULL.

SQL TRIM Syntax

-- ANSI/ISO standard form
TRIM([ {BOTH | LEADING | TRAILING} [characters] FROM ] string);

-- Common shorthand
TRIM(string);
TRIM(characters FROM string);

SQL TRIM Parameters

  • BOTH | LEADING | TRAILING (keyword) - Side of the string to trim. Default BOTH.
  • characters (string) - The character(s) to remove. Default is space.
  • string (string) - The source expression to be trimmed.

Example Queries Using SQL TRIM

-- Remove spaces from both ends (default)
SELECT TRIM('   Galaxy   ');

-- Remove only trailing dashes
SELECT TRIM(TRAILING '-' FROM 'Galaxy---');

-- Remove leading zeros from an invoice number
SELECT TRIM(LEADING '0' FROM '00012345');

-- Shorthand: trim all commas and periods from both ends
SELECT TRIM('.,' FROM '..,Galaxy,.,');

Expected Output Using SQL TRIM

  • Each query returns the cleaned string:1) 'Galaxy'2) 'Galaxy'3) '12345'4) 'Galaxy'

Use Cases with SQL TRIM

  • Standardize user inputs by stripping extra spaces before comparison
  • Clean CSV imports that contain trailing delimiters
  • Remove padding characters added for fixed-width file formats
  • Strip leading zeros from numeric codes for display
  • Prepare keys for joins where inconsistent whitespace exists

Common Mistakes with SQL TRIM

  • Forgetting the FROM keyword in the full syntax
  • Expecting TRIM to remove characters in the middle of a string
  • Supplying multiple trim characters in MySQL, which only trims the first character of the set
  • Assuming TRIM('') changes the string (empty literal has no effect)
  • Confusing TRIM with REPLACE when internal characters must be removed

Related Topics

LTRIM, RTRIM, SUBSTRING, REPLACE, REGEXP_REPLACE, CONCAT

First Introduced In

SQL:1999

Frequently Asked Questions

What characters can TRIM remove?

TRIM can remove any literal or expression that evaluates to one or more characters. If omitted, it trims ordinary spaces.

How do I trim newline or tab characters?

Pass the specific character in a literal, e.g., TRIM('\n' FROM column) or TRIM('\t' FROM column). Ensure escape sequences match your dialect.

Does TRIM work on NULL values?

No. If the input expression is NULL, TRIM returns NULL.

Difference between TRIM and LTRIM/RTRIM?

LTRIM removes characters only from the left side, RTRIM from the right side, while TRIM can handle either side or both.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!