How to use SUBSTRING_INDEX() MySQL

Jahtra Genio
Aug 29, 2022

The SUBSTRING_INDEX() function returns a substring of a string before a specified number of delimiter occurs.

mysql> SELECT ip, SUBSTRING_INDEX(ip,’.’,1) AS part1,

SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',2),'.',-1) AS part2, 
SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',-2),'.',1) AS part3,
SUBSTRING_INDEX(ip,'.',-1) AS part4 FROM log_file;
+-----------------+-------+-------+-------+-------+
| ip | part1 | part2 | part3 | part4 |
+-----------------+-------+-------+-------+-------+
| 127.0.0.1 | 127 | 0 | 0 | 1 |
| 192.128.0.15 | 192 | 128 | 0 | 15 |
| 255.255.255.255 | 255 | 255 | 255 | 255 |
+-----------------+-------+-------+-------+-------+
3 rows in set (0.00 sec)

--

--