SELECT SUBSTRING_INDEX('../my_dir/dir_name/page1.php','/',1)
Output is ..
SELECT SUBSTRING_INDEX('../my_dir/dir_name/page1.php','/',2)
Output is ../my_dir
SELECT SUBSTRING_INDEX('../my_dir/dir_name/page1.php','/',-1)
Output is page1.php
SELECT SUBSTRING_INDEX('../my_dir/dir_name/page1.php','/',-2)
Output is dir_name/page1.php
substring_index(string,'delimiter',count )
We will apply this to our newsletter subscriber table where we stored email address of our subscribers. Here we will apply substring_index to separate domain part and userid part and maintain a list. Here is a sample of email address.
userid@domainname.com
Here is the query to get the
select email,substring_index(email,"@",-1) from newsletter_table
This will give output as
userid@aol.com aol.com
onemore@gmail.com gmail.com
Like this the full list can be displayed. We can use count and group by command to generate a query by which we can tell number of subscribers using email address of different domains. Say how many have yahoo account, how many have gmail account etc.
SELECT substring_index(email,"@",-1), count(substring_index(email,"@",-1) ) as no FROM `newsletter_table` group by substring_index(email,"@",-1) order by no desc
gmail.com 63SELECT SUBSTRING_INDEX(name, ' ', 1) FROM `student`
Now here is the query to get the last name from the full name.
SELECT SUBSTRING_INDEX(name, ' ', -1) FROM `student`
SELECT SUBSTRING_INDEX(dir,'/',2) FROM `table_name`
To remove from starting ( left side )
SELECT SUBSTRING_INDEX(dir,'/',-2)FROM `table_name`
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.