Last active
February 6, 2019 18:23
-
-
Save pimbrouwers/88fe4c9cbaf51d48d12766e1749ca11d to your computer and use it in GitHub Desktop.
Parse GeoLite2-City-Locations-en.csv into SQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE [master]; | |
GO | |
IF OBJECT_ID('tempdb..#GeoLite2CityLocations') IS NOT NULL | |
DROP TABLE [tempdb]..[#GeoLite2CityLocations]; | |
CREATE TABLE [#GeoLite2CityLocations] | |
(PRIMARY KEY ([geoname_id]) | |
, [geoname_id] INT NOT NULL | |
, [locale_code] NVARCHAR(16) NULL | |
, [continent_code] NVARCHAR(16) NULL | |
, [continent_name] NVARCHAR(64) NULL | |
, [country_iso_code] NVARCHAR(16) NULL | |
, [country_name] NVARCHAR(64) NULL | |
, [subdivision_1_iso_code] NVARCHAR(64) NULL | |
, [subdivision_1_name] NVARCHAR(64) NULL | |
, [subdivision_2_iso_code] NVARCHAR(64) NULL | |
, [subdivision_2_name] NVARCHAR(64) NULL | |
, [city_name] NVARCHAR(128) NULL | |
, [metro_code] NVARCHAR(32) NULL | |
, [time_zone] NVARCHAR(64) NULL | |
, [is_in_european_union] NVARCHAR(32) NULL); | |
GO | |
BULK INSERT [tempdb]..[#GeoLite2CityLocations] | |
FROM 'C:\...PATH TO FILE..\GeoLite2-City-Locations-en.csv' | |
WITH (FIRSTROW = 2 | |
, FIELDTERMINATOR = ',' | |
, ROWTERMINATOR = '0x0a'); | |
GO | |
-- strip double quotes | |
UPDATE [#GeoLite2CityLocations] | |
SET [continent_name] = REPLACE([continent_name], '"', ''); | |
UPDATE [#GeoLite2CityLocations] | |
SET [country_name] = REPLACE([country_name], '"', ''); | |
UPDATE [#GeoLite2CityLocations] | |
SET [city_name] = REPLACE([city_name], '"', ''); | |
UPDATE [#GeoLite2CityLocations] | |
SET [subdivision_1_name] = REPLACE([subdivision_1_name], '"', ''); | |
UPDATE [#GeoLite2CityLocations] | |
SET [subdivision_2_name] = REPLACE([subdivision_2_name], '"', ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment