SUBSTRING_INDEX() function in MySQL — with examples.

Josh Otwell
9 min readApr 1, 2020

I recently used the MySQL SUBSTRING_INDEX(), function for a requirement in a PHP/MySQL reporting dashboard I am developing, and thought to write about SUBSTRING_INDEX() general usage. Although this post has arbitrary examples, I’m sure you can find a use for this function in specific cases. Continue reading and see basic queries including SUBSTRING_INDEX in the SELECT clause…

Photo by Denny Müller on Unsplash

OS, Software, and DB used:

  • OpenSuse Leap 15.1
  • MySQL 8.0.19

Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!

SUBSTRING_INDEX() accepts 3 arguments:

  1. A string to search.
  2. A delimiter to search on.
  3. A number of occurrences of the specified delimiter. In other words, which occurrence of the delimiter to split on should multiple be present in the string to search.

I will use a simple table, ‘file_names’ for the examples throughout the post:

MySQL localhost:33060+ ssl learning SQL > DESC file_names;
+ — — — — — — -+ — — — + — — — + — — -+ — — — — -+…

--

--

Josh Otwell
Josh Otwell

Written by Josh Otwell

SQL/PHP | Photography | Technical Consultant. Sign-up for my free developer newsletter, OpenLampTech, here: openlamptech.substack.com

No responses yet