Beginner

99. SUBSTRING Function

Learn how to extract part of a text value using substring functions.

📘 Lesson 99 of 728 ⏱ 8 min 🎯 Beginner

A substring function extracts a portion of a string based on a starting position and, depending on the database system, a specified length. Syntax can vary between SQL platforms.

Business identifiers often contain multiple pieces of information inside one text field. Extracting part of a string can help analysts prepare and categorize data.

If a customer code contains a region prefix, a substring operation can extract the prefix for regional analysis.

SQL
SELECT
    SUBSTRING(CustomerCode, 1, 3) AS RegionCode
FROM Customers;

Substring operations are useful for extracting codes, prefixes, suffixes, product identifiers, account references, and parts of addresses.

  • SUBSTRING extracts part of a string.
  • A starting position is commonly required.
  • A length may also be specified.
  • Syntax differs between database systems.
  • It is useful for parsing structured text.
What does SUBSTRING do?

SUBSTRING extracts a portion of a text value.

Why use SUBSTRING?

It can extract meaningful portions such as prefixes, suffixes, or codes from a larger text value.

Have you finished this lesson?

Introduction to SQL – All Lessons