SQL Keywords

SQL LEADING

What is SQL LEADING?

Directive used inside TRIM to remove characters from the start of 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 LEADING: PostgreSQL, MySQL, MariaDB, Oracle, SQL Server 2017+, SQLite 3.8+, Snowflake, BigQuery, Redshift

SQL LEADING Full Explanation

LEADING is one of three trim specifiers (LEADING, TRAILING, BOTH) defined by the SQL standard. When used inside the TRIM function it tells the database to strip the specified trim characters only from the beginning (left side) of the input string. If no trim character list is supplied, spaces are assumed. LEADING never alters characters that appear after the first non-match; trimming stops as soon as a character not in the trim list is encountered. LEADING is not a stand-alone statement; it is valid only as part of TRIM, and its behavior can differ slightly between dialects that also support the shorter LTRIM(string) form.

SQL LEADING Syntax

TRIM(LEADING [trim_character_list] FROM source_string);

SQL LEADING Parameters

  • trim_character_list (string) - Optional list of characters to remove. Defaults to space if omitted.
  • source_string (string) - The value to be trimmed.

Example Queries Using SQL LEADING

-- Remove leading spaces
SELECT TRIM(LEADING FROM '   Galaxy  ')   AS cleaned;

-- Remove leading zeros from a text column
SELECT TRIM(LEADING '0' FROM '000123')    AS no_leading_zero;

-- Strip protocol from URL
SELECT TRIM(LEADING 'http://' FROM 'http://getgalaxy.io') AS url_clean;

Expected Output Using SQL LEADING

  • Returns the input string minus any leading characters found in the trim list
  • Internal or trailing characters remain untouched

Use Cases with SQL LEADING

  • Standardize user input by stripping unwanted spaces at the start.
  • Clean numeric text fields that store padded zeros.
  • Remove known prefixes such as protocols, country codes, or currency symbols before further processing.

Common Mistakes with SQL LEADING

  • Omitting the FROM keyword: TRIM(LEADING 'x' 'abc') is invalid.
  • Expecting LEADING to remove characters in the middle of a string.
  • Forgetting that the default trim character is a space when no list is provided.
  • Mixing up SQL standard syntax with dialect-specific LTRIM(string) shorthand.

Related Topics

TRIM, TRAILING, BOTH, LTRIM, RTRIM, SUBSTRING

First Introduced In

SQL:1999

Frequently Asked Questions

What databases support LEADING?

Most mainstream engines including PostgreSQL, MySQL, MariaDB, Oracle, SQL Server 2017+, SQLite, Snowflake, BigQuery, and Redshift implement LEADING inside TRIM.

How do I remove only leading spaces?

Use the default behavior: `TRIM(LEADING FROM my_col)` or simply `TRIM(LEADING my_col)` in MySQL. This strips spaces from the start of `my_col`.

Can I trim multiple characters at once?

Yes. Provide a list of characters: `TRIM(LEADING 'xyz' FROM 'xxygalaxy')` returns `galaxy`.

Why is my middle text not removed?

LEADING stops trimming at the first character not in the trim list. It never deletes internal characters; use REPLACE or REGEXP_REPLACE for that.

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!