DEV Community

Cover image for SUBSTRING_INDEX in MySQL: A Simple Guide to String Extraction
DbVisualizer
DbVisualizer

Posted on

SUBSTRING_INDEX in MySQL: A Simple Guide to String Extraction

Extracting substrings from text is a common task in SQL. MySQL provides SUBSTRING_INDEX, a function that allows you to retrieve parts of a string before or after a specific delimiter. Let’s break it down.

Understanding SUBSTRING_INDEX

The function takes three parameters:

SUBSTRING_INDEX(string, delimiter, count)
Enter fullscreen mode Exit fullscreen mode

string the full text to extract from.

delimiter the character to split the string by.

count the occurrence of the delimiter to reference.

SELECT SUBSTRING_INDEX('apple.orange.banana', '.', -2);
Enter fullscreen mode Exit fullscreen mode

The output from this query is:

'orange.banana'
Enter fullscreen mode Exit fullscreen mode

This retrieves everything after the first period.

Extracting email domains

To separate domains from emails use:

SELECT email, SUBSTRING_INDEX(email, '@', -1) AS domain 
FROM users;
Enter fullscreen mode Exit fullscreen mode

Extracting file extensions

To retrieve file extensions from filenames use:

SELECT filename, SUBSTRING_INDEX(filename, '.', -1) AS extension 
FROM files;
Enter fullscreen mode Exit fullscreen mode

Extracting URL domains

To get the domain from a URL write:

SELECT url, SUBSTRING_INDEX(SUBSTRING_INDEX(url, '://', -1), '/', 1) AS domain 
FROM websites;
Enter fullscreen mode Exit fullscreen mode

FAQ

Is SUBSTRING_INDEX available in all SQL databases?

No, it is exclusive to MySQL.

How can I achieve this in SQL Server?

SQL Server uses a mix of CHARINDEX, LEFT, and RIGHT.

Does PostgreSQL have an equivalent?

Yes, SPLIT_PART() performs similar tasks.

Can this handle multiple delimiters?

Yes, the count parameter controls how many occurrences to consider.

Conclusion

Mastering string functions in SQL can greatly enhance data extraction capabilities, and SUBSTRING_INDEX is a powerful tool in MySQL that simplifies this process. Whether you’re working with email addresses, file paths, or URLs, this function allows you to retrieve structured data with minimal effort.

By leveraging MySQL’s built-in string functions like SUBSTRING_INDEX, developers can write more efficient queries that reduce processing time and improve readability. Understanding this function will help you optimize SQL queries for better performance and maintainability.

For more in-depth examples and best practices, read the full guide A Complete Guide to SUBSTRING_INDEX in SQL.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️

Build With Me: AI-Powered Adaptive Web Scraper with LLMs

Join us for a hands-on session with Zia Ahmad where we build an AI-driven web scraper that adapts to site changes in real-time. Code along and level up your automation skills.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️