Beginner

311. SUBSTRING Function

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

📘 Lesson 311 of 728 ⏱ 9 min 🎯 Beginner

The SUBSTRING function extracts a specified portion of characters from a string starting at a defined position.

Business identifiers often contain multiple pieces of information inside one text value. SUBSTRING allows analysts to extract the required portion.

Extract the middle portion of an employee code.

SQL
SELECT
    EmployeeCode,
    SUBSTRING(EmployeeCode, 4, 3) AS BranchCode
FROM Employees;

SUBSTRING is useful for extracting product codes, account segments, branch identifiers, and other structured text values.

  • SUBSTRING extracts part of a string.
  • A starting position is specified.
  • A length can normally be specified.
  • It is useful for structured identifiers.
  • Syntax varies between database systems.
What does SUBSTRING do?

SUBSTRING extracts a specified portion of characters from a string.

When would you use SUBSTRING?

It is useful when a text value contains multiple pieces of information and only one portion is required.

Have you finished this lesson?

Introduction to SQL – All Lessons