decimal ip converter
Example of the script: Decimal IP Converter
Scroll down for an example using the stored fucntion.
To Select the ip and its decimal counterpart:
SELECT
ip,
(
(POW(256,3))*
(substring(ip,1,(LOCATE('.',ip)-1)))
) +
(
(POW(256,2))*
(substring(ip,(LOCATE('.',ip)+1),(((LOCATE('.',ip,(LOCATE('.',ip)+1))) - (LOCATE('.',ip)) - 1))))
) +
(
(256)*
(substring(ip,((LOCATE('.',ip,(LOCATE('.',ip)+1)))+1),(((LOCATE('.',ip,(LOCATE('.',ip,(LOCATE('.',ip)+1))+1))) - (LOCATE('.',ip,(LOCATE('.',ip)+1))) - 1))))
) +
(substring(ip,((LOCATE('.',ip,(LOCATE('.',ip,(LOCATE('.',ip)+1))+1)))+1))) AS decimal_ip
FROM visitors
To UPDATE your table (visitors in the example), with the decimal values of the ips
UPDATE visitors
SET decimal_ip=
(
(POW(256,3))*
(substring(ip,1,(LOCATE('.',ip)-1)))
) +
(
(POW(256,2))*
(substring(ip,(LOCATE('.',ip)+1),(((LOCATE('.',ip,(LOCATE('.',ip)+1))) - (LOCATE('.',ip)) - 1))))
) +
(
(256)*
(substring(ip,((LOCATE('.',ip,(LOCATE('.',ip)+1)))+1),(((LOCATE('.',ip,(LOCATE('.',ip,(LOCATE('.',ip)+1))+1))) - (LOCATE('.',ip,(LOCATE('.',ip)+1))) - 1))))
) +
(substring(ip,((LOCATE('.',ip,(LOCATE('.',ip,(LOCATE('.',ip)+1))+1)))+1)))
In Order for this to work you might have to create a new column first, like this:
ALTER TABLE visitors ADD decimal_ip INT(10) UNSIGNED AFTER ip
And if you've decided that you have no use anymore for the column that holds the ip, you can drop it like this:
ALTER TABLE visitors DROP ip
Stored Function Example
SELECT toDecimalIp('192.168.0.1')
This will output:
3232235521