MARIADB
MySQL
SQL

Parsing Data with SUBSTRING_INDEX: A Complete Guide

intro

SUBSTRING_INDEX has a lot of use cases, and one of those use cases pertains to parsing data using SQL. Here’s what you need to know!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

If you’re a seasoned developer and especially if you deal with projects necessitating data analysis in MySQL, you will surely know your way around SUBSTRING_INDEX. This MySQL function returns a substring (hence the name) of a given string.

We’ve covered the internals of SUBSTRING_INDEX in MySQL in some of our previous blogs, but not once have we told you about how to deal with parsing data using this very function. That’s what this blog is about!

A Quick Introduction to SUBSTRING_INDEX

Coming back to the basics for a brief second, the core function of SUBSTRING_INDEX in MySQL is that it allows us to extract a specific subset of a string based on a delimiter.

The function’s syntax looks like so:

Copy
        
1 SUBSTRING_INDEX(string, delimiter, count)

Where:

  • string is the original string (text) that we need parts of data from (we’re acting on this string.)
  • delimiter is the delimiter character or string. Based on this delimiter, we split data and act on it.
  • count determines which part of the string to return. If count is positive, it returns the substring before the count-th appearance of the delimiter. If count is negative, it returns the substring after the count-th appearance of the delimiter.

Understanding the syntax is vital to parse data using this SQL function!

How to Parse Data with SUBSTRING_INDEX

See some real-world examples of using the SUBSTRING_INDEX function for data parsing in MySQL.

Parsing Email Addresses

To parse email addresses, you’d need to extract the username (left-hand side) part of the email address and your query would then look like so:

Copy
        
1 SELECT `email`, SUBSTRING_INDEX(`email`, '@', 1) AS `username` FROM `users`;

This SQL query would return the username part of the email address from the users table. For some analysis projects, that may be useful, but suppose you run a data breach search engine: in that case, you would likely need to return the domain part of the email address and count how many times that domain has appeared in a data breach, too. You could do it like so:

Copy
        
1 SELECT SUBSTRING_INDEX(email, '@', -1) AS `domain`, 2 COUNT(*) AS `domain_count` 3 FROM `users` 4 GROUP BY `domain` 5 ORDER BY `domain_count` DESC;

Then, you could run the query on a multi-database client like DbVisualizer:

Analyzing email addresses from a data breach in DbVisualizer
Analyzing email addresses from a data breach in DbVisualizer

Extracting a String Between Delimiters

You can also use SUBSTRING_INDEX to extract a string between two delimiters like so:

Copy
        
1 SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(purchase, 'Order ID:', -1), ';', 1) 2 AS order_id FROM orders;

This would help to extract an Order ID from a column that’s formatted like Order ID:12345;Status:Ordered;.... The above query would extract the 12345 ID out of such a string.

Extracting Strings from CSV Formatted Files

If you have an Excel/CSV file with fields formatted like John,Doe,30, you can also do something like this:

Copy
        
1 SELECT 2 SUBSTRING_INDEX(`toparse`, ',', 1) AS `first_name`, 3 SUBSTRING_INDEX(SUBSTRING_INDEX(`toparse`, ',', 2), ',', -1) AS `last_name`, 4 SUBSTRING_INDEX(`toparse`, ',', -1) AS `age` 5 FROM 6 (SELECT 'John,Doe,30' AS `toparse`) AS `data`;

The above query allows you to split a CSV string (which represent sample CSV-structured data) into separate columns in MySQL. In this example:

  • first_name extracts the first value before the first comma.
  • last_name extracts the value between the first and second commas.
  • age extracts the last value after the final comma.

Combining SUBSTRING_INDEX with Other Functions

Finally, you can also combine the SQL SUBSTRING_INDEX function with something else like so:

Copy
        
1 SELECT *, SUBSTRING_INDEX(`email`, '@', -1) AS `username` 2 FROM `customers` 3 WHERE `email` IS NOT NULL;

This SQL query would return everything (*), as well as the username part of the email column as a username column where the email column is not NULL. Note that NULL and empty aren’t the same thing, so in this case, everything that’s not NULL (including empty values), would be returned back to the user.

Dealing with SUBSTRING_INDEX in DbVisualizer

SUBSTRING_INDEX queries can become long and complex, which is why you generally want to execute them in a dedicated database tool. Aside from SUBSTRING_INDEX, you would work with your database using SQL clients, and DbVisualizer is one of them.

While DbVisualizer won’t tell you how best to use SUBSTRING_INDEX (you would have to turn to TheTable for that), your SQL client will help you ensure that you run nicely formatted SQL queries as well as run them safely and securely:

Permissions in DbVisualizer
Permissions in DbVisualizer

Also, you can format SQL queries and execute them as buffers;

Maknig SQL Queries look nice with DbVisualizers SQL formatting capabilities
Maknig SQL Queries look nice with DbVisualizer’s SQL formatting capabilities

There are only two of the long list of features offered by DbVisualizer.

Take DbVisualizer for a spin today, and we’ll see you in the next guide about SQL subtleties!

Summary

The SUBSTRING_INDEX function in SQL is a function that helps you return a part of a string in a column. That makes this function very useful when conducting analysis operations and especially when parsing data.

We hope that this blog has helped you understand how this function works on a deeper level when analyzing data pertaining to data breach search engines and other use cases, and we’ll see you in the next one!

FAQ

What does SUBSTRING_INDEX do?

SUBSTRING_INDEX enables you to extract a specific subset of a string based on a delimiter and looks like so:

Copy
        
1 SUBSTRING_INDEX(string, delimiter, count)

How to use SUBSTRING_INDEX when parsing data?

To use SUBSTRING_INDEX when parsing data, consider the delimiter and the count part of the function. delimiter means the delimiter for your data and the count means “either return the first or the second part after the delimiter.”

Where can I learn more about SQL functions and other things?

To learn more about SQL functions and related things, closely follow the documentation of the database management system of your choice, follow blogs like TheTable and YouTube channels on database management systems, as well as read books on the database management systems that interest you. Hacking MySQL: Breaking, Optimizing, and Securing MySQL for Your Use Case is a good place to start.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL DROP TABLE Statement: Everything You Need To Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-06-01
title

How to Upgrade MySQL in WHM? Step-By-Step Guide

author Lukas Vileikis tags MARIADB MySQL 6 min 2026-05-25
title

Performance Optimization Strategies for Real-World Workloads

author Lukas Vileikis tags SQL 7 min 2026-05-18
title

MySQL 8.0 EOL: What Happens Next?

author Lukas Vileikis tags MARIADB MySQL SQL 4 min 2026-04-30
title

Best Tools for Role-Based Access Control (RBAC) in SQL Databases in 2026

author Lukas Vileikis tags SQL SQL clients 6 min 2026-04-27
title

The Best MySQL GUI for macOS: Top 4 Alternatives to Workbench

author Leslie S. Gyamfi tags SQL SQL clients 7 min 2026-04-20
title

Database Clients: A Security Comparison of the Most Popular Tools

author Lukas Vileikis tags SQL 5 min 2026-04-09
title

SQL Interview Questions and Answers: Part 2 — Problems & Solutions

author Lukas Vileikis tags MARIADB MySQL SQL 7 min 2026-04-06
title

Comparing Git Support in Popular SQL Clients

author Leslie S. Gyamfi tags SQL SQL clients 7 min 2026-04-02
title

SQL Interview Questions and Answers: Part 1 — The Basics

author Lukas Vileikis tags DBMS MARIADB MySQL POSTGRESQL SQL SQL SERVER 10 min 2026-03-30

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.