Why This Error Happens
Error 2285 occurs when SQL Server’s XML parser encounters a numeric character entity reference (such as &#x) that does not correspond to a valid Unicode code point, often due to malformed data passed into FOR XML, OPENXML, or an XML-typed column/variable. This is frequently caused by corrupted or improperly encoded source data (e.g., control characters, surrogate pairs, or binary data mistakenly cast as text) being inserted into an XML structure. It can also surface after a disorderly migration or ETL import where character encoding (UTF-8 vs. UTF-16) was mismatched, introducing invalid byte sequences that SQL Server interprets as broken entity references.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query or Data Source
- Enable SQL Server Profiler or Extended Events to capture the exact statement raising Error 2285.
- Check Application/SQL Server logs in Event Viewer (
Windows Logs > Application) filtering for Event ID 2285 to correlate the timestamp with the failing query. - Note whether the error originates from
FOR XML PATH,OPENXML, or anXMLdata type insert/update.
Step 2: Sanitize the Source Data
- Run a validation query to detect control characters or invalid code points in the source table:
SELECT * FROM YourTable
WHERE YourColumn LIKE '%[' + CHAR(0) + '-' + CHAR(31) + ']%'
- Strip or replace invalid characters using
REPLACE()or a cleansing function before casting to XML. - If data originates from an external file, re-verify its encoding (UTF-8 without BOM is recommended) using Notepad++ or PowerShell’s
Get-Content -Encoding.
Step 3: Correct the Encoding Configuration
- On Windows Server 2025 / Windows 11, confirm the regional and system locale settings under
Control Panel > Region > Administrative > Change system localematch the expected collation of your SQL Server instance. - Verify the database collation (
SELECT DATABASEPROPERTYEX('YourDB', 'Collation')) supports the character set being ingested. - If using SSIS or bulk import tools, explicitly set the
Code Pageproperty to65001 (UTF-8)in the data flow task.
Step 4: Rebuild the XML Conversion Logic
- Replace direct casts (
CAST(Column AS XML)) with a safer conversion that usesTRY_CASTorTRY_CONVERTto isolate failing rows:
SELECT *, TRY_CAST(YourColumn AS XML) AS ParsedXML
FROM YourTable
WHERE TRY_CAST(YourColumn AS XML) IS NULL AND YourColumn IS NOT NULL
- Quarantine and manually review rows returning
NULLto identify the specific invalid entity reference.
Step 5: Apply Patches and Restart Services
- Ensure SQL Server 2025 has the latest Cumulative Update installed via
SQL Server Setup > Check for Updates, as XML parser bug fixes are periodically released. - Restart the SQL Server service from
services.mscor run:
Restart-Service -Name 'MSSQLSERVER'
Alternative or Verification Steps
- Re-run the original failing query/import job and confirm no Error 2285 appears in SQL Server Logs or Event Viewer.
- As a fallback, import data as
NVARCHAR(MAX)first, cleanse it usingREPLACE/regex-based functions, and only convert toXMLtype afterward to avoid parser-level rejection during ingestion.