Open In App

INET_ATON() function in MySQL

Last Updated : 14 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

INET_ATON() :

This function in MySQL takes the dotted-quad representation of an IPv4 address as a string and returns the numeric value of the given IP address in form of an integer. If the input address is not a valid IPv4 address this function returns NULL. The return address is calculated by the following formula :

If the given input IPv4 address is a.b.c.d then the return value a×2563+ b×2562+ c×2561 + d

Syntax :

INET_ATON(expr)

Parameter : 

This method accepts only one parameter.

  • expr – Input IPv4 address represented by a string.

Returns : 

It returns the numeric value of a given IPv4 address.

Example-1 :

Checking the equivalent integer representation for the following address “1.6.5.0” with the help of INET_ATON Function. As it is a valid IPv4 address we will get results in an integer.

SELECT INET_ATON('1.6.5.0')  AS AddressInInteger ;

Output :

ADDRESSININTEGER
17171712

Example-2 :

Checking the equivalent integer representation for the following address “::1.6” with the help of INET_ATON Function. As it is not a valid IPv4 address we will get NULL.

 SELECT INET_ATON('::1.6')  AS AddressInInteger ;

Output :

ADDRESSININTEGER
NULL

Example -3 :

Checking the equivalent integer representation for the following address “115.16.55.255” with the help of INET_ATON Function. As it is a valid IPv4 address we will get results in an integer.

 SELECT INET_ATON('15.16.55.255')  AS AddressInInteger ;

Output :

ADDRESSININTEGER
252721151

Example-4 :

Checking the equivalent integer representation for the following IPv6 address “fdfe::5a55:caff:fefa:9089” with the help of INET_ATON Function. 

SELECT INET_ATON('fdfe::5a55:caff:fefa:9089')  AS AddressInInteger ;

Output :

ADDRESSININTEGER
NULL

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads