
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we split an IP Address into four respective octets by using MySQL SUBSTRING_INDEX() function?
Suppose we have a table named ‘ipaddress’ which contains the IP addresses as its values in column ‘IP’ as follows −
mysql> Select * from ipaddress; +-----------------+ | ip | +-----------------+ | 192.128.0.5 | | 255.255.255.255 | | 192.0.255.255 | | 192.0.1.5 | +-----------------+ 4 rows in set (0.10 sec)
Now with the help of SUBSTRING_INDEX() function in the following query, we can divide the IP address in four octets −
mysql> Select IP, SUBSTRING_INDEX(ip,'.',1)AS '1st Part', -> SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',2),'.',-1)AS '2nd Part', -> SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',-2),'.',1)AS '3rd Part', -> SUBSTRING_INDEX(ip,'.',-1)AS '4th Part' from ipaddress; +-----------------+----------+----------+----------+----------+ | IP | 1st Part | 2nd Part | 3rd Part | 4th Part | +-----------------+----------+----------+----------+----------+ | 192.128.0.5 | 192 | 128 | 0 | 5 | | 255.255.255.255 | 255 | 255 | 255 | 255 | | 192.0.255.255 | 192 | 0 | 255 | 255 | | 192.0.1.5 | 192 | 0 | 1 | 5 | +-----------------+----------+----------+----------+----------+ 4 rows in set (0.05 sec)
- Related Articles
- How can we split the name string into two parts by using MySQL SUBSTRING_INDEX() function?
- How can we split the name string into three parts by using MySQL SUBSTRING_INDEX() function?
- How can we insert data into an existing MySQL table by using PHP script?
- How can we upload data into MySQL tables by using mysqlimport?
- How to hide an IP address
- How can we upload data into multiple MySQL tables by using mysqlimport?
- How can we import CSV files into MySQL tables by using mysqlimport?
- How we can split Python class into multiple files?
- How can we get the client's IP address in ASP.NET MVC C#?
- Which MySQL datatype to used to store an IP address?
- How can we fetch all the data from MySQL table by using mysql_fetch_array() function, returning an array with the numeric index, in PHP script?
- How to get IP address settings using PowerShell?
- How to validate the IP address using PowerShell?
- How can we set up a MySQL User account by using INSERT INTO statement?
- Defanging an IP Address in Python

Advertisements