FAQs for IP2Location™ IP-Country-Region-City-ISP-Domain Database [DB7]

Expand All | Collapse All

General

The database format is known as Comma Separated Values (CSV). All fields are separated by a comma and each individual line is a record by itself.

IP2Location is also available in binary format which works together with the IP2Location API in several programming languages.

The IP2Location will display the "-" in country field when the IP address range is still unallocated to any countries. It is also known as reserved IP address range.

You can purchase it from here. We will generate an unique login/password to allow you to download the database for one year after we have processed your order.

You will receive your login and password through email immediately after payment is authorized. You can use your credentials to download the database from our website at anytime. The database is in a ZIP compressed format to save your bandwidth and downloading time.

Ownership of IP addresses change hands from time to time. Therefore, a small percentage of IP address blocks needs to be updated every year. Our database is updated monthly to make sure it is always accurate.

The IP2Location is supporting 249 countries as recognized in ISO 3166. The IP2Location database has over 99.5% accuracy in country level detection and >75% of accuracy in city level. The inaccuracy is due to the dynamic IP address allocation by large ISPs such as AOL, MSN TV and other proxies. Due to the fact that AOL uses a network that routes all of the company's Internet traffic through Reston, Virginia, all IP-based geo-location services, including IP2Location, are unable to determine the state and city for people who dial into the AOL network. You can get the complete accuracy and coverage from data accuracy report page.

We are monitoring all new IP address range assigned. We will scan through all new IP address range once reported for its network name and location. This is a manual process and requires actual usage from any IP address in order for us to determine the related ISP name and location. Therefore, there might be some update delays in the database for any newly assigned IP address range. We strive to provide the information as accurate as possible.

Yes, we will deliver the notification via email when the update is available in the download area. We usually release the update on the first day of the calendar month.

The IP2Location database will be released on the 1st day of the calendar month and uploaded to the customer download area for immediate download.

2016 2017 2018 2019
1st January, 2016 1st January, 2017 1st January, 2018 1st January, 2019
1st February, 2016 1st February, 2017 1st February, 2018 1st February, 2019
1st March, 2016 1st March, 2017 1st March, 2018 1st March, 2019
1st April, 2016 1st April, 2017 1st April, 2018 1st April, 2019
1st May, 2016 1st May, 2017 1st May, 2018 1st May, 2019
1st June, 2016 1st June, 2017 1st June, 2018 1st June, 2019
1st July, 2016 1st July, 2017 1st July, 2018 1st July, 2019
1st August, 2016 1st August, 2017 1st August, 2018 1st August, 2019
1st September, 2016 1st September, 2017 1st September, 2018 1st September, 2019
1st October, 2016 1st October, 2017 1st October, 2018 1st October, 2019
1st November, 2016 1st November, 2017 1st November, 2018 1st November, 2019
1st December, 2016 1st December, 2017 1st December, 2018 1st December, 2019

You need to have the right type of license if you want to redistribute IP2Location database in your application. If the number of client installations are less than 20 servers, you can purchase the standard server license for them under your account. If you redistribute the software with database to more than 20 users, please visit licensing and contact us for more information about Redistribution License (On Premise).

You need to have the right type of license if you want to redistribute IP2Location geolocation in your SaaS application. Please visit licensing and contact us for more information about Redistribution License (SaaS).

If you are an existing subscriber of IP2Location database, you can upgrade to a higher package. You just need to pay for the difference in price instead of the full amount again. The subscription period will remain unchanged based on the old subscription package. Please login and click on the upgrade button inside the customer account area.

NOTE: If your current subscription has less than 6 months left, it will be renewed first before the upgrade is performed.

IP2Location licensing is based on the number of physical machine installation. If you plan to install the IP2Location databases or software in more than one server, please acquire additional licenses during checkout. Please visit licensing http://www.ip2location.com/licensing for more information.

You can login to the account area and click on the "Add" button right beside the license. You will need to pay for any additional license (full year) by credit card to complete the transaction.

 

Technical

IP address (IPv4) is divided into 4 sub-blocks. Each sub-block has a different weight number each powered by 256. IP number is being used in the database because it is more efficient to search between a range of numbers in a database.

The Beginning IP number and Ending IP Number are calculated based on the following formula:

IP Number = 16777216*w + 65536*x + 256*y + z     (1)

where

IP Address = w.x.y.z


For example, if the IP address is "202.186.13.4", then its IP Number will be "3401190660", based on the formula (1).

IP Address = 202.186.13.4

So, w = 202, x = 186, y = 13 and z = 4

IP Number = 16777216*202 + 65536*186 + 256*13 + 4
          = 3388997632 + 12189696 + 3328 + 4
          = 3401190660


To reverse IP number to IP address,

w = int ( IP Number / 16777216 ) % 256
x = int ( IP Number / 65536    ) % 256
y = int ( IP Number / 256      ) % 256
z = int ( IP Number            ) % 256


where
%
is the modulus operator and
int
returns the integer part of the division.
Example ASP Function To Convert IP Address to IP Number
Function Dot2LongIP (ByVal DottedIP)
Dim i, pos
Dim PrevPos, num
If DottedIP = "" Then
    Dot2LongIP = 0
Else
    For i = 1 To 4
        pos = InStr(PrevPos + 1, DottedIP, ".", 1)
        If i = 4 Then
            pos = Len(DottedIP) + 1
        End If
        num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
        PrevPos = pos
        Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
    Next
End If
End Function
Example PHP Function To Convert IP Address to IP Number
function Dot2LongIP ($IPaddr) {
	if ($IPaddr == "") {
		return 0;
	}
	else {
		$ips = split ("\.", "$IPaddr");
		return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
	}
}
Example Java Function To Convert IP Address to IP Number
long Dot2LongIP(String ipstring) {
	String[] ipAddressInArray = ipstring.split("\.");
	long result = 0;
	long ip = 0;
	for (int x = 3; x >= 0; x--) {
		ip = Long.parseLong(ipAddressInArray[3 - x]);
		result |= ip << (x << 3);
	}
	return result;
}
Example ColdFusion Function To Convert IP Address to IP Number

	function Dot2LongIP(ipAddress) {
		if(arguments.ipAddress EQ "") {
			return 0;
		}
		else {
			ips = ListToArray( arguments.ipAddress, "." );
			return( ( 16777216 * ips[1] ) + ( 65536 * ips[2] ) + ( 256 * ips[3] ) + ips[4] );
		}
	}
Example C# Function To Convert IP Address to IP Number
public double Dot2LongIP(string DottedIP)
{
    int i;
    string [] arrDec;
    double num = 0;
    if (DottedIP == "")
    {
       return 0;
    }
    else
    {
       arrDec = DottedIP.Split(".");
       for(i = arrDec.Length - 1; i >= 0 ; i --)
       {
          num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
       }
       return num;
    }
}
Example VB.NET Function To Convert IP Address to IP Number
Public Function Dot2LongIP(ByVal DottedIP As String) As Double
    Dim arrDec() As String
    Dim i As Integer
    Dim intResult As Long
    If DottedIP = "" then
       Dot2LongIP = 0
    Else
       arrDec = DottedIP.Split(".")
       For i = arrDec.Length - 1 To 0 Step -1
          intResult = intResult + ((Int(arrDec(i)) Mod 256) * Math.Pow(256, 3 -i))
       Next
       Dot2LongIP = intResult
    End If
End Function
Example Perl Function To Convert IP Address to IP Number
use Socket;

sub dot2LongIP {
	my $ip_address = shift(@_);
	return unpack("N",inet_aton($ip_address));
}
Example Ruby Function To Convert IP Address to IP Number
require 'ipaddr'

def dot2LongIP(ip)
	ipnum = IPAddr.new(ip)
	return ipnum.to_i
end
Example Python Function To Convert IP Address to IP Number
import ipaddress

def dot2LongIP(ip):
	return int(ipaddress.IPv4Address(ip))
Example C Function To Convert IP Address to IP Number
uint32_t Dot2LongIP(char* ipstring)
{
	uint32_t ip = inet_addr(ipstring);
	uint8_t *ptr = (uint8_t *) &ip;
	uint32_t a = 0;

	if (ipstring != NULL) {
		a =  (uint8_t)(ptr[3]);
		a += (uint8_t)(ptr[2]) * 256;
		a += (uint8_t)(ptr[1]) * 256 * 256;
		a += (uint8_t)(ptr[0]) * 256 * 256 * 256;
	}
	return a;
}
Example MySQL Function To Convert IP Address to IP Number
CREATE FUNCTION Dot2LongIP (ip text)
RETURNS bigint
BEGIN
	DECLARE ipnum bigint;
	SET ipnum = (SELECT INET_ATON(ip));
	RETURN ipnum;
END
Example MS SQL Function To Convert IP Address to IP Number
Create FUNCTION [dbo].[Dot2LongIP]( @IP VarChar(15) )
RETURNS BigInt
AS
BEGIN
   DECLARE @ipA BigInt,
         @ipB Int,
         @ipC Int,
         @ipD Int,
         @ipI BigInt
   SELECT @ipA = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )
   SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipA) - 1 )

   SELECT @ipB = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )
   SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipB) - 1 )

   SELECT @ipC = LEFT(@ip, PATINDEX('%.%', @ip) - 1 )
   SELECT @ip = RIGHT(@ip, LEN(@ip) - LEN(@ipC) - 1 )

   SELECT @ipD = @ip
   RETURN ( @ipA * 256*256*256 ) + ( @ipB * 256*256 ) + ( @ipC * 256 ) + @ipD
END
   RETURN @ipI
END
Example PostgreSQL Function To Convert IP Address to IP Number
CREATE OR REPLACE FUNCTION Dot2LongIP(text) RETURNS bigint AS '
SELECT
split_part($1,''.'',1)::int8*(256*256*256)+
split_part($1,''.'',2)::int8*(256*256)+
split_part($1,''.'',3)::int8*256+
split_part($1,''.'',4)::int8;
'
LANGUAGE 'SQL';
Example Microsoft(r) Excel Function To Convert IP Address to IP Number
Convert IPv4 IP Address to IP Number in Decimal Integer (IPv4 IP Address is in cell A1):
=((VALUE(LEFT(A1, FIND(".", A1)-1)))*256^3)+((VALUE(MID(A1, FIND(".", A1)+1, FIND(".", A1, FIND(".", A1)+1)-FIND(".", A1)-1)))*256^2)+((VALUE(MID(A1, FIND(".", A1, FIND(".", A1)+1)+1, FIND(".", A1, FIND(".", A1, FIND(".", A1)+1)+1)-FIND(".", A1, FIND(".", A1)+1)-1)))*256)+(VALUE(RIGHT(A1, LEN(A1)-FIND(".", A1, FIND(".", A1, FIND(".", A1)+1)+1))))

Convert IP Number in Decimal Integer to IPv4 IP Address (Decimal Integer is in cell A2):
=IF(A2<>"", CONCATENATE(INT(A2/256^3), ".", INT(MOD(A2, (256^3))/(256^2)), ".", INT(MOD(MOD(A2, 256^3), 256^2)/256), ".", MOD(MOD(MOD(A2, 256^3), 256^2), 256)), "")

function Dot2LongIP($ipv6) {
	$int = inet_pton($ipv6);
	$bits = 15;

	$ipv6long = 0;

	while($bits >= 0) {
		$bin = sprintf("%08b", (ord($int[$bits])));

		if($ipv6long){
			$ipv6long = $bin . $ipv6long;
		}
		else{
			$ipv6long = $bin;
		}
		$bits--;
	}

	$ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
	return $ipv6long;
}
java.math.BigInteger Dot2LongIP(String ipv6) {
	java.net.InetAddress ia = java.net.InetAddress.getByName(ipv6);
	byte byteArr[] = ia.getAddress();

	if (ia instanceof java.net.Inet6Address) {
		java.math.BigInteger ipnumber = new java.math.BigInteger(1, byteArr);
		return ipnumber;
	}
}
Example ColdFusion Function To Convert IP Address to IP Number
<cffunction name="Dot2LongIP" access="public" returntype="numeric">
	<cfargument name="IPAddress" type="string" required="true" />
	<cfif arguments.IPAddress EQ "">
		<cfreturn 0 />
	</cfif>
	<cfset IPV6Long = CreateObject("java","java.math.BigInteger") />
	<cfset IPV6NetAddress = CreateObject("java","java.net.InetAddress") />
	<cfset newIp = IPV6NetAddress.getByName(arguments.IPAddress) />
	<cfset bytes = newIp.getAddress() />
	<cfset bigInt = IPV6Long.init(1, bytes).toString() />
	<cfreturn bigInt />
</cffunction>
public System.Numerics.BigInteger Dot2LongIP(string ipv6)
{
	System.Net.IPAddress address;
	System.Numerics.BigInteger ipnum;

	if (System.Net.IPAddress.TryParse(ipv6, out address)) {
		byte[] addrBytes = address.GetAddressBytes();

		if (System.BitConverter.IsLittleEndian) {
			System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
			byteList.Reverse();
			addrBytes = byteList.ToArray();
		}

		if (addrBytes.Length > 8) {
			//IPv6
			ipnum = System.BitConverter.ToUInt64(addrBytes, 8);
			ipnum <<= 64;
			ipnum += System.BitConverter.ToUInt64(addrBytes, 0);
		} else {
			//IPv4
			ipnum = System.BitConverter.ToUInt32(addrBytes, 0);
		}
		return ipnum;
	}
}
Public Function Dot2LongIP(ByVal ipv6 As String) As System.Numerics.BigInteger
	Dim address As System.Net.IPAddress
	Dim ipnum As System.Numerics.BigInteger

	If System.Net.IPAddress.TryParse(ipv6, address) Then
		Dim addrBytes() As Byte = address.GetAddressBytes()

		If System.BitConverter.IsLittleEndian Then
			Dim byteList As New System.Collections.Generic.List(Of Byte)(addrBytes)
			byteList.Reverse()
			addrBytes = byteList.ToArray()
		End If

		If addrBytes.Length > 8 Then
			'IPv6
			ipnum = System.BitConverter.ToUInt64(addrBytes, 8)
			ipnum <<= 64
			ipnum += System.BitConverter.ToUInt64(addrBytes, 0)
		Else
			'IPv4
			ipnum = System.BitConverter.ToUInt32(addrBytes, 0)
		End If
	End If
	Dot2LongIP = ipnum
End Function
use NetAddr::IP;

sub dot2LongIP {
	my $ip_address = shift(@_);
	my $ip_number = NetAddr::IP->new($ip_address) or die;
	return $ip_number->bigint;
}
require 'ipaddr'

def dot2LongIP(ipv6)
	ipnum = IPAddr.new(ipv6)
	return ipnum.to_i
end
import ipaddress

def dot2LongIP(ipv6):
	return int(ipaddress.IPv6Address(ipv6))
#include <arpa/inet.h>
#include <inttypes.h>

typedef unsigned __int128 uint128_t;

uint128_t Dot2LongIP(const char* ipv6) {
	struct sockaddr_in6 sa;
	inet_pton(AF_INET6, ipv6, &(sa.sin6_addr));
	uint128_t ipnum = 0;
	uint128_t octet = 0;
	int i;
	for (i = 0; i < (sizeof(sa.sin6_addr.s6_addr) / sizeof(sa.sin6_addr.s6_addr[0])); i++) {
		octet = ((uint128_t)sa.sin6_addr.s6_addr[i] << ((uint128_t)(15 - i) * 8));
		ipnum = ipnum + octet;
	}
	return ipnum;
}

Firstly, convert the IP address to IP number format. Search IP-Country-Region-City-ISP-Domain Database using IP number to match a record that has the IP Number between the Beginning IP Number and the Ending IP Number.

For example, IP Address "72.77.138.60" is "1213041212" in IP Number. It matched the following recordset in the database.

"1213041208","1213041215","US","United States","Florida","Sarasota","Verizon Internet Services Inc","verizon.net"

ASP without Proxy detection
<%
    ipaddress = Request.ServerVariables("REMOTE_ADDR")
%>

ASP with Proxy detection
<%
    ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    if ipaddress = "" then
        ipaddress = Request.ServerVariables("REMOTE_ADDR")
    end if
%>
PHP without Proxy detection
<?
    $ipaddress = getenv(REMOTE_ADDR);
?>

PHP with Proxy detection
<?
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
    }
?>
JSP without Proxy detection
<%
    String ipaddress = request.getRemoteAddr();
%>

JSP with Proxy detection
<%
    if (request.getHeader("X_FORWARDED_FOR") == null) {
        String ipaddress = request.getRemoteAddr();
    } else {
        String ipaddress = request.getHeader("X_FORWARDED_FOR");
    }
%>
ColdFusion without Proxy detection
<CFCOMPONENT>
<CFSET ipaddress="#CGI.Remote_Addr#">
</CFCOMPONENT>

ColdFusion with Proxy detection
<CFCOMPONENT>
<CFIF #CGI.HTTP_X_Forwarded_For# EQ "">
<CFSET ipaddress="#CGI.Remote_Addr#">
<CFELSE>
<CFSET ipaddress="#CGI.HTTP_X_Forwarded_For#">
</CFIF>
</CFCOMPONENT>
ASP.NET (C#) without Proxy detection
public string IpAddress()
{
    return Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}

ASP.NET (C#) with Proxy detection
public string IpAddress()
{
    string strIp;
    strIp = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (strIp == null)
    {
       strIp = Request.ServerVariables["REMOTE_ADDR"];
    }
    return strIp;
}
ASP.NET (VB.NET) without Proxy detection
Public Function IpAddress()
    IpAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
End Function

ASP.NET (VB.NET) with Proxy detection
Public Function IpAddress()
    Dim strIp As String
    strIp = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If strIp = "" Then
       strIp = Request.ServerVariables("REMOTE_ADDR")
    End If
    IpAddress = strIp
End Function

You need to download the latest monthly database from our server using your subscription account and password. The database is available in complete format. Therefore, you just need to drop the old database and replace it with the new one.

Our subscribers can automate the download process using the free command-line script written in Perl which can be downloaded from our Web site. Please visit the following Web page for more information such as command syntax.
http://www.ip2location.com/free/downloader

Since the database is being updated every month at the beginning of the month, please download the database only once a month.

Yes. If you already have an AWS account, you can simply set up the database in just a few steps.

Yes. You may download the image at the below link:

Yes. You may refer it at:

LocaProxy.com. It provides multi-location HTTP proxies to help businesses test geolocation applications. This solution reduces the total cost of testing by supplying the Distributed Infrastructure as a Service.

The IP2Location databases supply the nearest Weather Station Codes and Names only. It does NOT provide live weather data or forecast data feed. However, it is possible to collect the weather information by using paid subscription from 3rd-party Web sites such as Weather.com. Please refer to their license agreement and contact them if you have any questions regarding XML weather data feed.

Weather.com XML Data Feed Registration (Free for Personal Use or Paid for Commercial use)
https://registration.weather.com/ursa/profile/new?

For example, if the nearest Weather Station Code for one IP address is AAXX0001 in Aruba. The following are some sample sites with custom URL to retrieve the weather information and forecast.

Sample XML Data Feed using Weather.com
http://xoap.weather.com/weather/local/AAXX0001?cc=*&dayf=1&unit=m

Web-based Weather Information from Other Sites
https://weather.com/weather/today/l/AAXX0001
http://www.theweathernetwork.com/index.php?product=weather&placecode=aaxx0001
http://www.intellicast.com/Global/Satellite/Infrared.aspx?location=AAXX0001&lien=8
http://weather.aol.com/main.adp?location=AAXX0001
http://weather.msn.com/tenday.aspx?wealocations=wc:AAXX0001
http://weather.yahoo.com/forecast/AAXX0001.html

 

Database

First, import this database into your MS-SQL, MS-ACCESS, PL/SQL, MYSQL or other RDMS. Use an SQL query to get the matching recordset.

SELECT TOP 1 [ip_from], [ip_to], [country_code], [country_name], [region_name], [city_name], [isp], [domain]
FROM [ip2location_db7]
WHERE [SEARCH IP NO] <= ip_to
SELECT `ip_from`, `ip_to`, `country_code`, `country_name`, `region_name`, `city_name`, `isp`, `domain`
FROM `ip2location_db7`
WHERE INET_ATON([IP ADDRESS]) <= ip_to LIMIT 1

First, import this database into your MS-SQL, MS-ACCESS, PL/SQL, MYSQL or other RDMS. Use an SQL query to get the matching recordset. The IPv4 is in IPv4-mapped IPv6 address form. Learn more at https://www.ip2location.com/articles/ip2location-ipv4-mapped-ipv6-address.

SELECT TOP 1 [ip_from], [ip_to], [country_code], [country_name], [region_name], [city_name], [isp], [domain]
FROM [ip2location_db7]
WHERE [SEARCH IP NO] <= ip_to
SELECT `ip_from`, `ip_to`, `country_code`, `country_name`, `region_name`, `city_name`, `isp`, `domain`
FROM `ip2location_db7`
WHERE [SEARCH IP NO] <= ip_to LIMIT 1

CREATE DATABASE ip2location
GO

USE ip2location
GO

CREATE TABLE [ip2location].[dbo].[ip2location_db7](
	[ip_from] bigint NOT NULL,
	[ip_to] bigint NOT NULL,
	[country_code] nvarchar(2) NOT NULL,
	[country_name] nvarchar(64) NOT NULL,
	[region_name] nvarchar(128) NOT NULL,
	[city_name] nvarchar(128) NOT NULL,
	[isp] nvarchar(256) NOT NULL,
	[domain] nvarchar(128) NOT NULL,
) ON [PRIMARY]
GO

CREATE CLUSTERED INDEX [ip_to] ON [ip2location].[dbo].[ip2location_db7]([ip_to]) ON [PRIMARY]
GO 
CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db7`(
	`ip_from` INT(10) UNSIGNED,
	`ip_to` INT(10) UNSIGNED,
	`country_code` CHAR(2),
	`country_name` VARCHAR(64),
	`region_name` VARCHAR(128),
	`city_name` VARCHAR(128),
	`isp` VARCHAR(256),
	`domain` VARCHAR(128),
	PRIMARY KEY (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE DATABASE ip2location WITH ENCODING 'UTF8';
\c ip2location
CREATE TABLE ip2location_db7(
	ip_from integer(10) NOT NULL,
	ip_to integer(10) NOT NULL,
	country_code character(2) NOT NULL,
	country_name character varying(64) NOT NULL,
	region_name character varying(128) NOT NULL,
	city_name character varying(128) NOT NULL,
	isp character varying(256) NOT NULL,
	domain character varying(128) NOT NULL,
	CONSTRAINT ip2location_db7_pkey PRIMARY KEY (ip_from, ip_to)
);

CREATE DATABASE ip2location
GO

USE ip2location
GO

CREATE TABLE [ip2location].[dbo].[ip2location_db7](
	[ip_from] char(39) NOT NULL,
	[ip_to] char(39) NOT NULL,
	[country_code] nvarchar(2) NOT NULL,
	[country_name] nvarchar(64) NOT NULL,
	[region_name] nvarchar(128) NOT NULL,
	[city_name] nvarchar(128) NOT NULL,
	[isp] nvarchar(256) NOT NULL,
	[domain] nvarchar(128) NOT NULL,
) ON [PRIMARY]
GO

CREATE CLUSTERED INDEX [ip_to] ON [ip2location].[dbo].[ip2location_db7]([ip_to]) ON [PRIMARY]
GO 
CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db7`(
	`ip_from` DECIMAL(39,0) UNSIGNED,
	`ip_to` DECIMAL(39,0) UNSIGNED,
	`country_code` CHAR(2),
	`country_name` VARCHAR(64),
	`region_name` VARCHAR(128),
	`city_name` VARCHAR(128),
	`isp` VARCHAR(256),
	`domain` VARCHAR(128),
	PRIMARY KEY (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE DATABASE ip2location WITH ENCODING 'UTF8';
\c ip2location
CREATE TABLE ip2location_db7(
	ip_from decimal(39,0) NOT NULL,
	ip_to decimal(39,0) NOT NULL,
	country_code character(2) NOT NULL,
	country_name character varying(64) NOT NULL,
	region_name character varying(128) NOT NULL,
	city_name character varying(128) NOT NULL,
	isp character varying(256) NOT NULL,
	domain character varying(128) NOT NULL,
	CONSTRAINT ip2location_db7_pkey PRIMARY KEY (ip_from, ip_to)
);

BULK INSERT [ip2location].[dbo].[ip2location_db7]
    FROM 'C:\[path to your CSV file]\IP-COUNTRY-REGION-CITY-ISP-DOMAIN.CSV'
    WITH
    (
        FORMATFILE = 'C:\[path to your DB7.FMT file]\DB7.FMT'
    )
GO
NOTE: You will need to copy the FMT code below and save it as a file named DB7.FMT on your computer.
The first line of the FMT code indicates the version of bcp. Please change the version as according to your MS-SQL installed.
SQL Server 201612.0
SQL Server 201412.0
SQL Server 201211.0
SQL Server 2008/2008R210.0
SQL Server 20059.0
SQL Server 20008.0
SQL Server 7.07.0
SQL Server 6.56.5

IPv4 database

10.0
9
1 SQLCHAR 0 1 "\"" 0 first_double_quote  Latin1_General_CI_AI
2 SQLCHAR 0 20 "\",\"" 1 ip_from ""
3 SQLCHAR 0 20 "\",\"" 2 ip_to ""
4 SQLCHAR 0 2 "\",\"" 3 country_code Latin1_General_CI_AI
5 SQLCHAR 0 64 "\",\"" 4 country_name Latin1_General_CI_AI
6 SQLCHAR 0 128 "\",\"" 5 region_name Latin1_General_CI_AI
7 SQLCHAR 0 128 "\",\"" 6 city_name Latin1_General_CI_AI
8 SQLCHAR 0 256 "\",\"" 7 isp Latin1_General_CI_AI
9 SQLCHAR 0 128 "\"\r\n" 8 domain Latin1_General_CI_AI

IPv6 database

10.0
9
1 SQLCHAR 0 1 "\"" 0 first_double_quote  Latin1_General_CI_AI
2 SQLCHAR 0 39 "\",\"" 1 ip_from ""
3 SQLCHAR 0 39 "\",\"" 2 ip_to ""
4 SQLCHAR 0 2 "\",\"" 3 country_code Latin1_General_CI_AI
5 SQLCHAR 0 64 "\",\"" 4 country_name Latin1_General_CI_AI
6 SQLCHAR 0 128 "\",\"" 5 region_name Latin1_General_CI_AI
7 SQLCHAR 0 128 "\",\"" 6 city_name Latin1_General_CI_AI
8 SQLCHAR 0 256 "\",\"" 7 isp Latin1_General_CI_AI
9 SQLCHAR 0 128 "\"\r\n" 8 domain Latin1_General_CI_AI

NOTE: Due to the fact that SQL Server does not support a number with more than 38 digits, we have to store the IP From and IP To fields as zero padded strings to enable sorting. Please visit this tutorial on how to add the padding, enable cluster index and make the query.

LOAD DATA LOCAL
	INFILE 'IP-COUNTRY-REGION-CITY-ISP-DOMAIN.CSV'
INTO TABLE
	`ip2location_db7`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';
COPY ip2location_db7 FROM 'IP-COUNTRY-REGION-CITY-ISP-DOMAIN.CSV' WITH CSV QUOTE AS '"';
Click here to view the step-by-step tutorials.

We provide tutorials on how to import the database into MongoDB, Redis, Amazon SimpleDB and CouchDB at http://www.ip2location.com/tutorials.