Published on Jun 26, 2025 5 min read

Understanding the SUBSTRING Function in SQL with Simple Examples

Understanding SQL often feels like learning how to talk to a very literal computer. Every function has a job, and it does exactly what it’s told—nothing more, nothing less. Among these tools, the SUBSTRING function is one of the most useful when you’re working with text. Whether you’re pulling just the first few characters from a name, slicing out a code from a product label, or breaking apart data to make it more readable, SUBSTRING helps you shape strings exactly how you need. Let’s walk through how it works and what you can do with it, with clear and direct examples.

How SUBSTRING Works in SQL

SQL code on a computer screen

At its core, the SUBSTRING function in SQL extracts a section of a string starting at a specified position and continuing for a set number of characters. The structure is simple:

SUBSTRING(expression, start_position, length)

Here’s what each part does:

  • expression: The original string (this could be a column or a direct text value).
  • start_position: Where the extraction begins (1-based index, meaning it starts counting from 1).
  • length: The number of characters you want to extract.

For example:

SELECT SUBSTRING('Sunflower', 1, 3);

This returns 'Sun', because it starts at the first character and grabs three letters.

If you choose a start_position that’s larger than the length of the string, SQL returns an empty string. If the length is longer than the remaining characters from your starting point, SQL just goes to the end of the string without raising an error.

Common Scenarios Where SUBSTRING Is Useful

The SUBSTRING function becomes especially handy when dealing with formatted data stored in a single column. This often happens when systems export data in a fixed format or when columns are overloaded with combined information.

Extracting Area Codes from Phone Numbers

If you have a table where phone numbers are stored in a fixed pattern like (123)456-7890, and you want just the area code, SUBSTRING can help:

SELECT SUBSTRING('(123)456-7890', 2, 3) AS AreaCode;

It skips the first character (the opening parenthesis) and takes the next three, returning '123'.

Pulling Year from Date Strings

Sometimes, especially in older systems or exports, dates are stored as text rather than proper DATE types. For instance, '2025-06-22' is stored as a string. If you want to get just the year:

SELECT SUBSTRING('2025-06-22', 1, 4) AS Year;

The result would be '2025'.

Working with Product Codes

Imagine you have a product code like 'PRD-AX456-Z9', and you want to extract only the middle part (i.e., 'AX456'). If the structure is consistent, you can apply SUBSTRING with a fixed start and length:

SELECT SUBSTRING('PRD-AX456-Z9', 5, 5) AS ProductID;

This gives you 'AX456'.

Combining SUBSTRING with Other SQL Functions

While SUBSTRING is powerful on its own, it becomes even more flexible when used alongside other SQL string functions like CHARINDEX, LEN, or RIGHT. These help when the structure of the string isn’t predictable and you can’t rely on fixed positions.

Using SUBSTRING with CHARINDEX

Let’s say you have a string like 'Name: John Smith', and you want to extract just the name portion after the colon. You can use CHARINDEX to find where the colon is and SUBSTRING to slice from that point:

SELECT SUBSTRING('Name: John Smith', CHARINDEX(':', 'Name: John Smith') + 2, LEN('Name: John Smith')) AS NameOnly;

This returns 'John Smith'. The + 2 skips the colon and the space after it. LEN ensures that you receive the entire string, regardless of its length.

Getting the Last Characters

If you need the last few characters of a string and the length varies, you can combine LEN with SUBSTRING. Suppose you want the last 4 characters of a product ID:

SELECT SUBSTRING(ProductID, LEN(ProductID) - 3, 4) FROM Products;

This pulls the final 4 characters from any product ID.

Cleaning Up Unwanted Characters

Cleaning up characters in SQL

Let’s say you want to extract a certain segment and then remove surrounding quotes or special characters. You could use REPLACE with SUBSTRING to do that in one go:

SELECT REPLACE(SUBSTRING('"SKU123"', 2, 6), '"', '') AS CleanSKU;

This gives you 'SKU123', without the quotation marks or any extra unwanted symbols.

Practical Tips for Using SUBSTRING Efficiently

SUBSTRING is a simple function, but a few best practices help make it more reliable in real-world queries:

  • Always make sure you know what kind of data you’re dealing with. If you’re not sure whether the format is consistent, consider checking it first using LEN() or CHARINDEX() to prevent off-by-one errors or null results.
  • Avoid hardcoding positions when the string varies in format or length. Instead, combine SUBSTRING with CHARINDEX or PATINDEX to make it dynamic.
  • Use table aliases and column aliases to keep your queries readable, especially if you’re working on longer reports or scripts with multiple steps.
  • When working with large datasets, remember that using string functions can add significant overhead. In performance-sensitive scenarios, consider storing pre-parsed values in dedicated columns if they’re used often.

Conclusion

The SUBSTRING function in SQL is like a scalpel for strings—it lets you carve out just the parts you want from any chunk of text. Whether you’re working with names, dates, codes, or anything stored as plain text, it’s one of those small functions that quietly does a lot. When combined with other SQL string functions, it becomes a versatile tool for shaping and refining data to fit your needs. Simple in form but flexible in function, SUBSTRING is one of the first things you’ll reach for when cleaning or dissecting text in SQL. It saves time, improves accuracy, and keeps your queries clean, especially when dealing with complex datasets.

For more insights on SQL functions, you can explore W3Schools SQL Tutorials for comprehensive guides and examples.

Related Articles

Popular Articles