How to import DB24 (IPv4) into MSSQL 2017

Intro

This guide aims to demonstrate how to load data from the IP2Location DB24 CSV file into a Microsoft SQL Server 2017 table.

If you’re using an earlier version of the SQL Server, you will need to refer to our FAQ page at https://www.ip2location.com/faqs/db24-ip-country-region-city-latitude-longitude-zipcode-timezone-isp-domain-netspeed-areacode-weather-mobile-elevation-usagetype#database for technical information about creating the table and importing the data.

Downloading the DB24 database for IPv4

You can subscribe for the DB24 database at https://www.ip2location.com/database/ip2location if you don’t have the subscription yet. If you’re already subscribed, just login to https://www.ip2location.com/log-in and download the DB24 CSV file.

Once you have downloaded the zipped file containing the CSV, extract the CSV and store it somewhere on your computer.

Creating the database and table

Run the following SQL commands to create the database called ip2location and a table called ip2location_db24.

CREATE DATABASE ip2location

GO

USE ip2location

GO

CREATE TABLE [ip2location].[dbo].[ip2location_db24](

   [ip_from] bigint NOT NULL,

   [ip_to] bigint NOT NULL,

   [country_code] char(2) NOT NULL,

   [country_name] nvarchar(64) NOT NULL,

   [region_name] nvarchar(128) NOT NULL,

   [city_name] nvarchar(128) NOT NULL,

   [latitude] float NOT NULL,

   [longitude] float NOT NULL,

   [zip_code] nvarchar(30) NOT NULL,

   [time_zone] nvarchar(8) NOT NULL,

   [isp] nvarchar(256) NOT NULL,

   [domain] nvarchar(128) NOT NULL,

   [net_speed] nvarchar(8) NOT NULL,

   [idd_code] nvarchar(5) NOT NULL,

   [area_code] nvarchar(30) NOT NULL,

   [weather_station_code] nvarchar(10) NOT NULL,

   [weather_station_name] nvarchar(128) NOT NULL,

   [mcc] nvarchar(256) NOT NULL,

   [mnc] nvarchar(256) NOT NULL,

   [mobile_brand] nvarchar(128) NOT NULL,

   [elevation] int(10) NOT NULL,

   [usage_type] nvarchar(11) NOT NULL

) ON [PRIMARY]

GO

CREATE CLUSTERED INDEX [ip_to] ON [ip2location].[dbo].[ip2location_db24]([ip_to]) ON [PRIMARY]

GO

Importing the data

Run the following SQL commands to import data from the CSV file into the table. Modify the below to reflect the path where you saved the extracted CSV file earlier.

BULK INSERT ip2location_db24

FROM 'C:\your_folder_name\IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.CSV'

WITH

(

FORMAT = 'CSV',

FIELDQUOTE = '"',

FIELDTERMINATOR = ',',

ROWTERMINATOR = '0x0D0A',

TABLOCK

)

GO

That’s all. Now you can query the table with an IP number.

Was this article helpful?

Related Articles