I have an example data file with following contents and saved with UTF8 encoding.
oab~opqr
öab~öpqr
öab~öpqr
The format of this file is fixed width with columns 1 to 3 each being allocated 1 character and column 4 reserved 5 characters.
I have created an XML format file as below
<?xml version = "1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD xsi:type="CharFixed" ID="Col1" LENGTH="1"/>
<FIELD xsi:type="CharFixed" ID="Col2" LENGTH="1"/>
<FIELD xsi:type="CharFixed" ID="Col3" LENGTH="1"/>
<FIELD xsi:type="CharFixed" ID="Col4" LENGTH="5"/>
<FIELD xsi:type="CharTerm" ID="LINE_BREAK" TERMINATOR="\n"/>
</RECORD>
<ROW>
<COLUMN SOURCE="Col1" NAME="Col1" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="Col2" NAME="Col2" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="Col3" NAME="Col3" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="Col4" NAME="Col4" xsi:type="SQLNVARCHAR"/>
</ROW>
</BCPFORMAT>
Disappointingly running the following SQL...
SELECT *
FROM OPENROWSET
(
BULK 'mydata.txt',
FORMATFILE = 'myformat_file.xml',
CODEPAGE = '65001'
) AS X
Produces the following results
Col1 Col2 Col3 Col4
---- ---- ---- -----
o a b ~opqr
� � a b~öp
� � a b~öp
from which I conclude the LENGTH
is counting bytes rather than characters.
Is there any way I can get this working correctly for fixed character widths with UTF8 encoding?
(Target environment is Azure SQL Database reading from Blob storage)
NB: It was suggested in the comments that adding COLLATION="LATIN1_GENERAL_100_CI_AS_SC_UTF8"
to the FIELD
elements might help but the results remain unchanged with this.