Troubleshooting Guide: SQL Server Error 2212
Why This Error Happens
Error 2212 occurs when the SQL Server parser encounters a character in a T-SQL identifier (table name, column name, variable, etc.) that falls outside the permitted character set defined by the SQL Server collation and Unicode identifier rules. This is typically caused by copy-pasting code from external sources (like Word documents, PDFs, or web pages) that introduces invisible or non-standard characters such as smart quotes, non-breaking spaces, zero-width characters, or BOM (Byte Order Mark) markers. It can also happen when a script file is saved with an incompatible encoding (e.g., UTF-8 with BOM misread as ANSI) on Windows Server 2025 or Windows 11 systems.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Character
- Look at the exact error message output, which includes the invalid character and its hex code, e.g.
Invalid source character ' ' (0xA0) found in an identifier near 'MyTable'. - Open the script in a hex-aware editor like Notepad++ or VS Code and enable “Show Symbol” / whitespace rendering to visually locate the character near the reported identifier.
- Use
Find > Find in Hex(Notepad++) and search for the reported hex value (e.g.,0x02or0xA0) to pinpoint its exact location.
Step 2: Clean and Re-Encode the Script File
- Re-type the problematic identifier manually instead of pasting it, to strip hidden Unicode artifacts.
- In VS Code, use the bottom status bar to check encoding; convert the file via
File > Save with Encoding > UTF-8(without BOM) if it currently shows “UTF-8 BOM” or “Windows-1252.” - Run a quick PowerShell cleanup pass to strip non-ASCII characters from the script before execution:
(Get-Content "C:\Scripts\query.sql" -Raw) -replace '[^\x00-\x7F]', '' | Set-Content "C:\Scripts\query_clean.sql"
Step 3: Validate Identifier Naming Rules in SQL Server 2025
- Ensure identifiers follow standard rules: must start with a letter, underscore,
@, or#, followed only by letters, digits,@,$,#, or_. - If using extended characters intentionally (e.g., non-English table names), wrap the identifier in square brackets
[ ]or double quotes (withQUOTED_IDENTIFIER ON) to force SQL Server to treat it as a delimited identifier rather than a raw token. - Re-run the corrected script in SQL Server Management Studio (SSMS) 21 or Azure Data Studio against your SQL Server 2025 instance to confirm the parser accepts it.
Step 4: Check Source Control / File Transfer Pipeline
- If the script is pulled from Git, Azure DevOps, or a CI/CD pipeline on Windows Server 2025, verify the
.gitattributesfile isn’t forcing an encoding conversion (e.g.,text=autocausing CRLF/UTF-8 BOM mismatches). - Confirm any automated deployment tool (e.g., SSDT, DACPAC deployment, or PowerShell
Invoke-Sqlcmd) is reading the.sqlfile with explicit-Encoding UTF8flags rather than defaulting to system locale.
Alternative or Verification Steps
- Re-execute the cleaned script via
sqlcmd -i query_clean.sql -bfrom an elevated Command Prompt; a clean exit code (0) confirms the identifier issue is resolved. - If the error persists only in SSMS but not
sqlcmd, disable SSMS IntelliSense auto-formatting (Tools > Options > Text Editor > Transact-SQL) since it can sometimes reinsert smart-quote characters during auto-complete.