I have a flat file witch I would like to bulk insert into SQL server (2016).
BULK INSERT table FROM 'path\flatfile.dat'
WITH
(
FORMATFILE='path\fileformat.fmt',
CODEPAGE = '65001',
DATAFILETYPE = 'Char'
);
Flat file has no field terminator:
9999030000000001001ab Baujahr 0
9999030000000001004from construction year 0
9999030000000001006à partir d'année de construction 0
9999030000000001007da anno 0
9999030000000001008año construcción desde 0
9999030000000001009Vanaf bouwjaar 0
9999030000000001010fra byggeår 0
9999030000000001011från årsmodell 0
9999030000000001012fra årsmodell 0
9999030000000001013Vuosimallista 0
but in format file I set the lenght of field
12.0
7
1 SQLCHAR 0 22 "" 1 Field1 ""
2 SQLCHAR 0 4 "" 2 field2 ""
3 SQLCHAR 0 3 "" 3 field3 ""
4 SQLCHAR 0 9 "" 4 field4 ""
5 SQLCHAR 0 3 "" 5 field ""
6 SQLCHAR 0 60 "" 6 Textfield ""
7 SQLCHAR 0 1 "\r\n" 7 flag ""
The other files I have with similar format works great except this one. The error I'm getting is truncation of rows which contains caracters áåóñ...
Msg 4863, Level 16, State 1, Line 6
Bulk load data conversion error (truncation) for row 3, column 7 (LFlag).
Msg 4863, Level 16, State 1, Line 6
Bulk load data conversion error (truncation) for row 5, column 7 (LFlag).
Msg 4863, Level 16, State 1, Line 6
Bulk load data conversion error (truncation) for row 7, column 7 (LFlag).
Msg 4863, Level 16, State 1, Line 6
any ideas ?
It's unclear whether the use of format files is fully supported by BCP when working with UTF-8 - your experience suggests that it isn't. It appears that there's some confusion between byte count and character count when importing a UTF-8 file. I think this is possibly a bug in BCP.
With the following test table
I was able to import your file using the format file in the question, and the same
BULK INSERT
command.However, what's notable is that the final column (
f7
in my table) is left-padded with a number of leading spaces equal to the number double-byte characters in column 6.On row 3 (column 6 value
à partir d'année de construction
), column 7 is padded with two spaces. The same is true of rows 5 and 8, which also have two double-byte characters.On row 9 (column 6 value
fra årsmodell
), column 7 is padded with one space. The same is true of row 7.It looks like the byte-count and character-count are not being tracked correctly by BCP.
Assuming the format of your input file is fixed, the only option I can see to work around this issue would be to import the data file into a table with single
nvarchar(max)
column, then useLEFT
,RIGHT
andSUBSTRING
statements to extract data values from the rows:Edit
Tested an XML format file, which doesn't work correctly either. Although using an XML format file allows the file to load without error, the value for column 7 is lost and replaced by a space.
Format file:
(The
BULK INSERT
command is the same as in the question, other than the format file name.)