Beginner

56. SUBSTRING Function

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

📘 Lesson 56 of 728 ⏱ 8 min 🎯 Beginner

The SUBSTRING function extracts a specified portion of a text value. The exact syntax can vary between database systems, but the general idea is to specify the original string, starting position, and required length or ending position.

Business fields sometimes contain multiple pieces of information in a single text value. SUBSTRING can help extract a required portion for analysis or transformation.

Suppose an employee code contains a branch identifier at the beginning of the value. SUBSTRING can be used to extract that portion of the code.

SQL
SELECT
    EmployeeID,
    SUBSTRING(EmployeeID, 1, 3) AS CodePrefix
FROM Employees;

SUBSTRING is useful for employee codes, customer IDs, product codes, account numbers, addresses, and text transformation.

  • SUBSTRING extracts part of a string.
  • The syntax can vary between database systems.
  • A starting position is commonly required.
  • A length or ending position may also be required.
  • It is useful for text transformation.
What is SUBSTRING used for?

SUBSTRING is used to extract a portion of text from a larger string.

Does SUBSTRING syntax vary between databases?

Yes. Different database systems can use slightly different syntax or related string functions.

Have you finished this lesson?

Introduction to SQL – All Lessons