I am using bulk insert to be able to import multiple CSV files from a folder into my table.
the sql im using is.
set @sql = 'BULK INSERT RawActivity FROM ''' + @path + @filename + ''' '
+ ' WITH (
FIELDTERMINATOR = '','',
FORMATFILE = ''C:\\Users\\pllaguno\\Desktop\\Febrero\\format.fmt'',
ROWTERMINATOR = ''\n'',
FIRSTROW = 2
) '
my CSV file is as following, i will include some lines where sql is sending me errors and where not(line number first).
2 Unknown Driver,2/1/2019,4651,Seat Belt Alert Start,12:00:06 AM,46,km/h,0.4,km,,,,,"3 Sur 256, Sevilla, 15840 Ciudad de México, CDMX, Mexico",19.40908,-99.12303
146 Unknown Driver,2/1/2019,9804,Seat Belt Alert End,12:12:36 AM,,-,,-,,,,,"[Cedis Saltillo], Perif. Luis Echeverría 3951, Centenario, 25050 Saltillo, Coah., Mexico",25.40517,-100.98308
152 Unknown Driver,2/1/2019,10319,Seat Belt Alert End,12:12:58 AM,,-,,-,,,,,"[Cedis Monterrey Centro], Calle Pedro María Anaya 1210, Industrial, 64440 Monterrey, N.L., Mexico",25.69608,-100.32383
153 Unknown Driver,2/1/2019,13042,Seat Belt Alert End,12:12:59 AM,,-,25.0,km,,,,,"Av. Eugenio Garza Sada Sur SN-S""OXXOEXPRES"", Roma, 64700 Monterrey, N.L., Mexico",25.65527,-100.29421
the error given is (EDIT: the line should be 153, the problem is some lines have more quotes inside)
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 152, column 15 (Latitude).
and my .fmt file is like this:
11.0
16
1 SQLCHAR 0 8000 "," 1 Driver SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 40 "," 2 ActivityDate SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 8000 "," 3 Vehicle SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 40 "," 4 Event SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 8000 "," 5 EventTime SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 40 "," 6 Speed SQL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 8000 "," 7 SpeedUnit SQL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 40 "," 8 TripDistance SQL_Latin1_General_CP1_CI_AS
9 SQLCHAR 0 8000 "," 9 TripUnit SQL_Latin1_General_CP1_CI_AS
10 SQLCHAR 0 40 "," 10 OnSiteTime SQL_Latin1_General_CP1_CI_AS
11 SQLCHAR 0 8000 "," 11 HDOP SQL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 40 "," 12 NumSatellites SQL_Latin1_General_CP1_CI_AS
13 SQLCHAR 0 8000 ",\"" 13 Channel SQL_Latin1_General_CP1_CI_AS
14 SQLCHAR 0 40 "\"," 14 Location SQL_Latin1_General_CP1_CI_AS
15 SQLCHAR 0 8000 "," 15 Lattitude SQL_Latin1_General_CP1_CI_AS
16 SQLCHAR 0 40 "\r\n" 16 Longitude SQL_Latin1_General_CP1_CI_AS
The column for Latitude is decimal(9,6)