Beginner

201. SUBSTRING Function

Learn how to extract a portion of text from a string.

📘 Lesson 201 of 728 ⏱ 9 min 🎯 Intermediate

The SUBSTRING function extracts a specified portion of a text value based on a starting position and, depending on the database system, a specified length.

Many business fields contain structured text where only a specific part of the value is required for analysis.

Extract the first five characters of a product code.

SQL
SELECT
    SUBSTRING(ProductCode, 1, 5) AS ShortCode
FROM Products;

SUBSTRING is useful for extracting codes, prefixes, identifiers, account segments, and portions of text fields.

  • SUBSTRING extracts part of a text value.
  • It commonly uses a starting position.
  • A length can define how many characters to extract.
  • Syntax varies between database systems.
  • It is useful for structured text.
What does SUBSTRING do?

SUBSTRING extracts a portion of a text value based on a specified position and length.

Is SUBSTRING syntax identical across databases?

No. The function syntax can vary between database systems.

Have you finished this lesson?

Introduction to SQL – All Lessons