Troubleshooting Guide: Error 2333 — “Invalid source character 0x%02x”
Why This Error Happens
Error 2333 occurs when SQL Server’s character conversion engine encounters a byte sequence in the source data that does not map to a valid character in the specified codepage or collation. This is typically caused by mismatched encoding between the source file (e.g., a flat file used in BCP, BULK INSERT, or SSIS) and the collation/codepage configured on the destination table or server. It can also result from file corruption, an incorrect -C codepage flag during bulk operations, or binary/non-text data accidentally being parsed as character data.
Step-by-Step Resolution Guide
Step 1: Identify the Actual Source Encoding
- Open the offending source file in a hex editor (e.g.,
HxD) or VS Code with encoding detection enabled to confirm whether it is UTF-8, UTF-16LE, ANSI, or a legacy DOS codepage. - Check for a Byte Order Mark (BOM) at the start of the file — SQL Server’s bulk tools can misinterpret files with or without an expected BOM.
- Run
file.exe(via WSL or Git Bash) against the file for a quick encoding guess:file -i sourcefile.csv.
Step 2: Correct the Codepage/Collation Mismatch
- If using BCP, explicitly specify the correct codepage flag, e.g.,
-C 65001for UTF-8 or-C RAWto bypass conversion entirely. - If using
BULK INSERT, set theCODEPAGEoption explicitly in T-SQL:
BULK INSERT dbo.TargetTable
FROM 'C:\Data\sourcefile.csv'
WITH (CODEPAGE = '65001', DATAFILETYPE = 'char');
- Verify the destination column collation matches the expected character set using
sp_help 'dbo.TargetTable'orSELECT name, collation_name FROM sys.columns WHERE object_id = OBJECT_ID('dbo.TargetTable');.
Step 3: Re-save or Convert the Source File
- Use PowerShell to re-encode the file into a SQL Server–friendly format:
Get-Content 'C:\Data\sourcefile.csv' -Raw -Encoding UTF8 | Set-Content 'C:\Data\sourcefile_fixed.csv' -Encoding Default
- If the file originated from a non-Windows system (Linux/macOS export), strip any stray non-ASCII control characters using a text editor’s “Clean” or “Normalize Encoding” function.
- Re-run the import job after saving the corrected file.
Step 4: Repair or Rebuild the Import Format File (If Applicable)
- If using a
.fmtformat file for BCP, confirm the field terminators and row terminators match the actual file structure rather than assumed defaults. - Regenerate the format file using:
bcp dbo.TargetTable format nul -c -x -f C:\Data\TargetTable.xml -S ServerName -T. - Confirm Windows Server 2025 regional settings under
Control Panel > Region > Administrative > Change system localealign with the expected source codepage.
Alternative or Verification Steps
- Re-run the bulk import or SSIS package and confirm Error 2333 no longer appears in the SQL Server Error Log (
EXEC sp_readerrorlog 0, 1, '2333';). - As an alternative, load the file into a staging table with an
NVARCHAR(MAX)catch-all column first, then useTRY_CONVERTin T-SQL to isolate and clean the specific invalid characters before moving data to the final target table.